Using a CallbackHandler HttpHandler for optimized Callbacks

You can also create a callback class as an Http Handler derived from CallbackHandler. Http handlers are more efficient than Page requests as they bypass the Page pipeline and are single focused on serving response data. The CallbackHandler class provides a base implementation of the JSON Callback engine that makes it easy for you to use an Http handler for callbacks and it works exactly the same way as callback methods in pages or controls with the difference that they do not have access to page code.

The West Wind Ajax library includes a CallbackHandler base class that you can inherit from - the handler manages the callback semantics of callbacks, all you have to do is inherit from it, 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 Generic Web Handler:

<%@ WebHandler Language="C#" Class="MyCallbackHandler" %>

using System;
using System.Web;
using System.Web.SessionState;
using Westwind.Web.Controls;

public class MyCallbackHandler : Westwind.Web.Controls.CallbackHandler 
{      
    [CallbackMethod]
    public string HelloWorld(string Name)
    {
        return "Hello " + Name + "! Time is: " + DateTime.Now;
    }
    [CallbackMethod]
    public decimal AddNumbers(decimal x, decimal y)
    {
        return x + y;
    }
}

I removed the stock interface members that the template creates since CallbackHandler implements them for you . Your handler only needs to implement the [CallbackMethod] methods and any support methods you might need. That's all there's to it.

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

<ww:AjaxMethodCallback ID="Proxy" runat="server" 
   ServerUrl="~/MyCallbackHandlerHandler.ashx" />

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)
{
     // if you want to get a Proxy generated specify the type of the proxy to generate
    this.Proxy.ClientProxyTargetType = typeof(MyCallbackHandler);
}

MyCallbackHandler is the type of our AjaxCallbackHander implementation (as shown above) and I assign it to the TargetCallback type to cause a proxy to be generated into the client HTML page.

To call the server now all you have to do is:

Proxy.HelloWorld("Rick",
            function(result) { alert(result); },
            function(error) { alert("Error: " + error.message); } 
);

If you rather not generate a proxy and use the client side AjaxCallbackMethod.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 AjaxCallbackMethod();
Callback.serverUrl = "MyCallbackHandler.ashx";
Callback.postbackMode = "PostMethodParametersOnly";
Callback.callMethod('Add',[x,y],CallbackHandler,ErrorHandler);

© West Wind Technologies, 1996-2016 • Updated: 04/10/13
Comment or report problem with topic