Inside ASP.NET AJAX back end services

Page 2 of 4
  1. Services in ASP.NET
  2. section removes any HTTP handlers that are installed for ASMX resources, including the standard han
  3. Script Services
  4. REST in Brief

section removes any HTTP handlers that are installed for ASMX resources, including the standard han

This article was originally published in VSJ, which is now part of Developer Fusion.

So, how exactly? Each ASMX request is forwarded to a registered HTTP handler factory – the ScriptHandlerFactory class. An HTTP handler factory is used in ASP.NET when dynamic conditions determine the actual HTTP handler to be used for a particular request. In this case, a request for an ASMX resource can potentially be served in either of two ways: via a script service or a classic Web service. The handler factory assesses request information and loads up the proper HTTP handler. Figure 1 shows the complete flow diagram for ASMX requests in ASP.NET AJAX applications.

Figure 1
Figure 1: Flow diagram for ASMX requests in ASP.NET AJAX applications

As its first step, the ScriptHandlerFactory class checks whether the incoming request has a given content type. The following snippet shows some pseudo-code that illustrates what really happens in the ASP.NET AJAX runtime. Note that a HTTP handler factory class implements the IHttpHandlerFactory interface which counts on two methods – GetHandler and ReleaseHandler. The code below gives an idea of the implementation of the GetHandler method in the script handler factory of ASP.NET AJAX. Note that this code has been slightly adapted for clarity, but is closely related to real ASP.NET AJAX code. Class names, in particular, are real:

public IHttpHandler GetHandler(
    HttpContext context,
    string requestType,
    string url,
    string pathTranslated)
{
    IHttpHandlerFactory factory;
    if (RestHandlerFactory.
    	IsRestRequest(context))
    {
    	factory = new
    		RestHandlerFactory();
    }
    else
    {
    	factory = new
    		WebServiceHandlerFactory();
    }
    :
}

The WebServiceHandlerFactory class is defined in the System.Web.Services.Protocols namespace and has been part of the ASP.NET platform since the beginning. The RestHandlerFactory class is new in ASP.NET AJAX. For some reason the REST term is conventionally used to indicate a request that needs to be treated differently from a classic Web service request, although it has a similar endpoint. I feel the use of the term REST here is ambiguous, to say the least. On the other hand, these classes are not public and not meant to be public. So in theory, they could have christened, say, the RestHandlerFactory class with any arbitrary name. But they opted for a name with REST in it. To fix things up, let’s say that in this context REST just merely indicates a non-SOAP solution.

The key step in the GetHandler method of the ScriptFactoryHandler class is the check made through the IsRestRequest method. The method accepts the HTTP context of the request and returns a Boolean value. At the highest of abstraction, the goal of the method is determining whether the current request is coming from a script. More concretely, the method simply checks the content type of the request. Here’s some illustrative code:

internal static bool
    IsRestRequest(HttpContext context)
{
    HttpRequest request =
    	context.Request;
    return String.Equals(
    	request.ContentType,
    	“application/json”,
    	StringComparison.
    	OrdinalIgnoreCase);
}

Any ASMX request that doesn’t have a content type of application/json is automatically served by the classic ASP.NET Web service handler. In other words, any ASMX request with a non application/json content type is implicitly assumed to carry SOAP envelopes in the body. Will the condition “content type set to application/json” equals “request comes from script” always be valid? Of course, there’s no guarantee that all requests with that content type come uniquely from the script of an AJAX Web page. It’s too easy to use the classes in the System.Net namespace, and arrange a Windows Forms application that calls into the back end of an AJAX application. However, any ASMX requests that come from an ASP.NET AJAX Web page aimed at a back end service is guaranteed to have the proper content type set. This is hard-coded in the Microsoft AJAX Client Library that all ASP.NET AJAX pages use to place remote calls.

Once the handler in charge of serving the request has been identified, it takes up the responsibility of verifying whether the invoked service can be invoked from script. So the RestHandlerFactory instantiates the real HTTP handler, and has this check whether the service class is decorated with the ScriptService attribute. If no such attribute can be found, an exception is thrown – see Figure 2.

Figure 2
Figure 2: Script code from ASP.NET AJAX pages can’t call any type of Web service

You might also like...

Comments

About the author

Dino Esposito United Kingdom

Dino Esposito is an instructor for Solid Quality Mentors, and a trainer and consultant based in Rome. He is the author of various books, including Windows Shell Programming, Instant DHTML Script...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“The question of whether computers can think is just like the question of whether submarines can swim.” - Edsger W. Dijkstra