Unveiled: there's a machine in the ghost!

Recently I blogged about the dropping of the Pfs HttpHandler, and about a new HttpModule that creates a PortalContext instance for each request that should (could) be served from the portal.

There are excellent ASP.NET developers without any knowledge about these modules and handlers. They use the Web Forms / Web Controls infrastucture, and enjoy the magic called ASP.NET that makes this all possibe.
It's a very good thing - likely your customers don't think in HTTP requests and responses, authentication headers and abstract factories. You neither have to, and that's a big gift.

But if you're a developer like me, who always wants to know what's behind the scenes: this post is for you!
So, what's the magic? How the ASP.NET pages work? Not too surprisingly, there's no real black magic: there's a machine in the ghost.

Check out this figure:

In this figure you can see how the ASP.NET engine handles a HTTP request.


After the request routed to the ASP.NET from the webserver, the first step is to create a managed object model based on the request data. This is an internal and automatic function, you should not (and most likely would not) modify this procedure.

After that, there are blocks called "HttpModules". An HttpModule is a class that implements the IHttpModule interface. To implement this interface you have to write a method called Init(), and in this method you can subscribe to the HttpApplication event like Begin- and EndRequest, authenticate and authorize request, and so on.
In the event handler you can access the current HttpContext instance via the sender parameter, so you can access and modify the properties of the Request, the Response, the Session, the User, the Cache, etc properties, you can rewrite the actual URL and so on.
Each module is executed for each request, so if you got a task that should be done every time a new request come - for example authenticate the user - it should be done in a HttpModule.

You can inject your custom HttpModules into the ASP.NET module pipeline in the web.config:

   <!-- SenseNet PortalEngine TNG Specific entries start -->
   <add name="PortalAuthentication" type="SenseNet.PortalEngine.Virtualization.PortalAuthenticationModule"/>
   <add name="PortalContext" type="SenseNet.PortalEngine.Virtualization.PortalContextModule" />
   <!-- SenseNet PortalEngine TNG Specific entries end -->

After the modules finished their work and prepared the context, a reply should be generated to the reqest. This task is done by a HttpHandler.
The ASP.NET runtime selects the appropiate handler type, creates an instace of that type and let the instance do the work. In most cases the runtime selects the correct handler based on the extension, but you also can map a handler to a given path or HTTP verb (GET, POST, etc).

You've got two options if you want to write a custom HttpHandler. You have to implement the IHttpHandler or the IHttpHandlerFactory interface.

To implement the IHttpHandler you have to write a method called ProcessRequest. The current HttpContext will be passed to this method and you can generate your reply and use the Response.Write() method to send that back to the client.

To implement the IHttpHandlerFactory you have to write a method called GetHandler which returns an IHttpHandler instance based on the request type, url, and the translated path.

Here's a sample how can you insert your HttpModules (since we don't need the PfsHandler anymore that is commented out): 

   <!-- SenseNet PortalEngine TNG Specific entries start -->
   <!--<add verb="*" path="*" validate="false" type="SenseNet.PortalEngine.Virtualization.PfsHandler"/>-->
   <!-- SenseNet PortalEngine TNG Specific entries end -->


If you're a WebForms developer and you say "I've never-ever developed a HttpHandler!" then you're not right: the ASP.NET Page class implemets the IHttpHandler. When your ASP.NET page is served, an IHttpHandlerFactory handles the request by instantiating the appropiate type (your class derived from Page) and gives the control to that.

As easy as ABC, isn't it? 

Comments (1) -

Zákányi Balázs
4/19/2009 12:42:11 AM #

Thank you.

Comments are closed

Welcome to the blog!

Sense/Net ECM is ever evolving. Community means the world to us so we want to keep you apprised on what’s happening on our side of the woods. Want to make us happy? Add a comment and tell us what you think!

Month List