Using an wwCallbackHandler HttpHandler for optimized Callbac

You can also create a callback class as an Http Handler derived from wwCallbackHandler. Http handlers are more efficient than Page requests as they bypass the Page pipeline and are single focused on serving response data. The wwCallbackHandler class provides a base implementation of the JSON Callback engine that makes it easy for you to use an Http handler for callbacks.

The West Wind Ajax library includes a wwCallbackHandler base class that you can inherit from - the handler manages the callback semantics of callbacks, all you have to do is inherit from it and implement your methods and mark them with [CallbackMethod] to be exposed to callbacks.

You can implement your handler either using an ASHX file or a standard HttpHandler that's hooked through web.config. I'll use the .ASHX method here. To implement your handler you can create a new Web Handler :

<%@ WebHandler Language="C#" Class="Handler" %> using System; using System.Web; using System.Web.SessionState; using Westwind.Web.Controls; public class MyCallbackHandler : Westwind.Web.Controls.wwCallbackHandler // , IRequiresSessionState { // *** Implemented at the base level //public void ProcessRequest (HttpContext context) //{ // base.ProcessRequest(context); //} //public bool IsReusable { // get // { // return false; // } //} [CallbackMethod] public string HelloWorld(string Name) { return "Hello " + Name + "! Time is: " + DateTime.Now; } [CallbackMethod] protected decimal AddNumbers(decimal x, decimal y) { return x + y; } }

I removed the stock interface members as wwCallbackHandler implements them for you - you can just remove them. Your handler only needs to implement the [CallbackMethod] methods and any support methods you might need- the HttpHandler semantics that deal with parsing the incoming JSON data, calling your methods and returning the outbound JSON data are all taken care of.

The only thing left to do then is to hook it up on the page:

<ww:wwMethodCallback ID="Proxy" runat="server" ServerUrl="~/Handler.ashx" PostBackMode="PostMethodParametersOnly" ScriptLocationType="WebResource"> </ww:wwMethodCallback>

Notice that I need to specify the ServerUrl explicitly now since we're going to a separate URL for the callback rather than the default of the current page.

The last detail is to make sure that the Proxy class can be generated into the client automatically. In order for the control to be able to generate the proxy it has to know the type of the handler so it can parse the [CallbackMethod] handlers. To do this set the control's TargetCallbackType property:

protected void Page_Load(object sender, EventArgs e) { // *** Must assign the target type for the callback control // *** otherwise it can't create the proxy this.Proxy.TargetCallbackType = typeof(MyCallbackHandler); }

MyCallbackHandler is the type of our wwCallbackHander/HttpHandler implementation (as shown above) and I assign it to the TargetCallback type to cause a proxy to be generated into the client HTML page. If you rather not generate a proxy and use the client side wwCallbackMethod.callMethod() method, you can skip assigning the type - this will improve performance slightly when the control renders as it doesn't have to parse the methods of the target.

The client code to manually call the server without a proxy:

var Callback = new wwCallbackMethod(); Callback.serverUrl = "MyCallbackHandler.ashx"; Callback.postbackMode = "PostMethodParametersOnly"; Callback.callMethod('Add',[x,y],CallbackHandler,ErrorHandler);

  Last Updated: 12/29/2007 | © West Wind Technologies, 2008