Showing posts with label HTTP Handler. Show all posts
Showing posts with label HTTP Handler. Show all posts

April 16, 2011

Steps to create a HTTP Handler

Description:
    In this article I am just trying to writing down the steps with code example (C#) to create a simple custom HTTP handler. and configure it on IIS 5.1. The detail description about the HTTP Handler are described on this blog "HTTP Handlers (ASP.NET)"


Step 1: Creation of Handler:
     1. Create a web application (in this example i have created with name HTTPHandlerTest).
     2. Add a class to (I have used MyTestHandler in this example) your application and implement the IHttpHandler Interface. You can also implement the IRequireSessionState Interface if you want to access the session and IReadOnlySessionState if you want to access session data in read only mode.
     3. Implement the ProcessRequest() method and IsReusable Property. IsReusable property  determines whether the handler is pooled or not. If it returns true then the handler is kept on the pool for further user and the ProcessRequest() method to the processing.


    4. Modify the web.config file to register the handler. In this example i have registered the handler to process the the files having extension .test.



<httpHandlers>
    <add verb="*" path="*.test" type="HTTPHandlerTest.MyTestHandler, HTTPHandlerTest"/>
</httpHandlers>


Step 2: Configure the IIS 5.1
     1. Open the Internet Information Services (Control Panel--> Administrative Tool-->Internet Information Services).
     2. Go to "Default Web Site", Right click on it and open Properties.
     3. On the Properties window click on the "Home Directory" tab and click on the "Configuration" Button.
     4. Click on the "Add" button to register the new mapping. 
         In the Executable text box browse the path of the "aspnet_isapi.dll" files location on your machine (i.e. C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll) and in the Extension text box enter the extension of the file that is going to process (i.e. i have used .test extension). Press OK to save setting.

Step 3: Testing Your Handler
    1. Add a file with extension .test to your application to test the application (I have added a file called MyTest.test to test it).
    2. Run the application and type "http://localhost:4611/MyTest.test" as URL.

April 13, 2011

HTTP Handlers (ASP.NET)

Description:
    HTTP handlers are .net components that is used the process the incoming request based on the mapped file name extension  in the configuration files (machine.config or web.config). ASP.NET framework defines some built-in handlers for processing. 


Example:
     ASP.NET Page Handler: Handles the request from ASP.NET web pages having extention .aspx
      Web Service Handler: Handlers the web service files having extenstion (*.asmx)
      User Control Handler: Handlers user controls files with extension (*.ascx)
        
List of the mapped HTTP Handler you will find in the config file

HTTP handlers are divided in to two types depending upon the way of generating output of the incoming request.
          1. Synchronous HTTP Handler
          2. Asynchronous HTTP Handler

Synchronous handlers does not return it's processed output until it finishes complete processing of the request, while asynchronous handlers runs a separate process independently to send the response back to user asynchronously.



Creating Custom HTTP Handler:
    To create a custom synchronous HTTP handler you need to implement the IHttpHandler interface and similarly for creating a custom asynchronous HTTP handler it is required to implement IHttpAsyncHandler interface. In both the cases it is required to implement the IsReusable property and ProcessRequest() method. 


IsReusable property determines whether the handler is placed in pool for re-use  or it is re-created every time it is required and ProcessRequest() method do the processing of each request.


Note: Session state is not by default enabled for HTTP Handlers, to enable it you have to implement the IRequiresSessionState or IReadOnlySessionState interface. These two are marker interfaces and there is no method to implement.


Configuring Custom HTTP Handler:
    After creating custom HTTP handler it is required to configure the web server and application to map the incoming request to be handled by the handler.


Steps to configure:
    1. Map the file extension that is going to be processed by your custom handler to ASP.NET ISAPI dll  (aspnet_isapi.dll) in the Internet Service Manager.
    2. Modify the web.config or machine.config file to register your handler.


Note: I have described steps to create a HTTP Handler on this post "Steps to create HTTP Handler"