Online Book Reader

Home Category

Programming Microsoft ASP.NET 4 - Dino Esposito [77]

By Root 5387 0
the request. The SimpleHandlerFactory class provides a good example of how a factory works. The factory is mapped to requests directed at .ashx resources. When such a request comes in, the factory determines the actual handler to use by looking at the @WebHandler directive in the source file.

In the .NET Framework, HTTP handler factories are used to perform some preliminary tasks on the requested resource prior to passing it on to the handler. Another good example of a handler factory object is an internal class named PageHandlerFactory, which is in charge of serving .aspx pages. In this case, the factory handler figures out the name of the handler to use and, if possible, loads it up from an existing assembly.

HTTP handler factories are classes that implement a couple of methods on the IHttpHandlerFactory interface—GetHandler and ReleaseHandler, as shown in Table 4-5.

Table 4-5. Members of the IHttpHandlerFactory Interface

Method

Description

GetHandler

Returns an instance of an HTTP handler to serve the request.

ReleaseHandler

Takes an existing HTTP handler instance and frees it up or pools it.

The GetHandler method has the following signature:

public virtual IHttpHandler GetHandler(

HttpContext context,

String requestType,

String url,

String pathTranslated);

The requestType argument is a string that evaluates to GET or POST—the HTTP verb of the request. The last two arguments represent the raw URL of the request and the physical path behind it. The ReleaseHandler method is a mandatory override for any class that implements IHttpHandlerFactory; in most cases, it will just have an empty body.

The following listing shows a sample HTTP handler factory that returns different handlers based on the HTTP verb (GET or POST) used for the request:

class MyHandlerFactory : IHttpHandlerFactory

{

public IHttpHandler GetHandler(HttpContext context,

String requestType, String url, String pathTranslated)

{

// Feel free to create a pool of HTTP handlers here

if(context.Request.RequestType.ToLower() == "get")

return (IHttpHandler) new MyGetHandler();

else if(context.Request.RequestType.ToLower() == "post")

return (IHttpHandler) new MyPostHandler();

return null;

}

public void ReleaseHandler(IHttpHandler handler)

{

// Nothing to do

}

}

When you use an HTTP handler factory, it’s the factory (not the handler) that you want to register in the ASP.NET configuration file. If you register the handler, it will always be used to serve requests. If you opt for a factory, you have a chance to decide dynamically and based on runtime conditions which handler is more appropriate for a certain request. In doing so, you can use the IsReusable property of handlers to implement a pool.

Asynchronous Handlers


An asynchronous HTTP handler is a class that implements the IHttpAsyncHandler interface. The system initiates the call by invoking the BeginProcessRequest method. Next, when the method ends, a callback function is automatically invoked to terminate the call. In the .NET Framework, the sole HttpApplication class implements the asynchronous interface. The members of the IHttpAsyncHandler interface are shown in Table 4-6.

Table 4-6. Members of the IHttpAsyncHandler Interface

Method

Description

BeginProcessRequest

Initiates an asynchronous call to the specified HTTP handler

EndProcessRequest

Terminates the asynchronous call

The signature of the BeginProcessRequest method is as follows:

IAsyncResult BeginProcessRequest(

HttpContext context,

AsyncCallback cb,

Object extraData);

The context argument provides references to intrinsic server objects used to service HTTP requests. The second parameter is the AsyncCallback object to invoke when the asynchronous method call is complete. The third parameter is a generic cargo variable that contains any data you might want to pass to the handler.

Note

An AsyncCallback object is a delegate that defines the logic needed to finish processing the asynchronous operation. A delegate is a class that holds a reference to a method. A delegate

Return Main Page Previous Page Next Page

®Online Book Reader