Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging an httpmodule on the asp.net development server

I want to integrate some http modules in my asp.net application (v 3.5, visual studio 2008) and I'm not sure how to debug or use such modules while debugging in the asp.net development server that fires when I run the web app.

Do I need to include the module source in the solution or can I just drop the DLL into BIN? I'm from the 1.1 world and am not yet used to the asp.net development server.

like image 817
Caveatrob Avatar asked Mar 24 '09 22:03

Caveatrob


People also ask

How do I debug an application deployed in IIS?

To start debugging, select IIS Express (<Browser name>) or Local IIS (<Browser name>) in the toolbar, select Start Debugging from the Debug menu, or press F5. The debugger pauses at the breakpoints. If the debugger can't hit the breakpoints, see Troubleshoot debugging.


1 Answers

Follow these steps to add an HTTP Module:

  1. Create a new Visual Studio .NET C# Class Library project named MyModule.
  2. Set a reference to the System.Web.dll assembly.
  3. Add the following directive to the class:

    using System.Web;                    
    
  4. Rename the class SyncModule.cs, and then change the class definition to reflect this.

  5. Implement the IHttpModule interface. Your class definition should appear as follows:

    public class SyncModule : IHttpModule                    
    
  6. Decide to which events you will subscribe. The following list outlines the available events from the HttpApplication object to which you can subscribe:

    • AcquireRequestState: Call this event to allow the module to acquire or create the state (for example, session) for the request.
    • AuthenticateRequest: Call this event when a security module needs to authenticate the user before it processes the request.
    • AuthorizeRequest: Call this event by a security module when the request needs to be authorized. Called after authentication.
    • BeginRequest: Call this event to notify a module that new request is beginning.
    • Disposed: Call this event to notify the module that the application is ending for some reason. Allows the module to perform internal cleanup.
    • EndRequest: Call this event to notify the module that the request is ending.
    • Error: Call this event to notify the module of an error that occurs during request processing.
    • PostRequestHandlerExecute: Call this event to notify the module that the handler has finished processing the request.
    • PreRequestHandlerExecute: Call this event to notify the module that the handler for the request is about to be called.
    • PreSendRequestContent: Call this event to notify the module that content is about to be sent to the client.
    • PreSendRequestHeaders: Call this event to notify the module that the HTTP headers are about to be sent to the client.
    • ReleaseRequestState: Call this event to allow the module to release state because the handler has finished processing the request.
    • ResolveRequestCache: Call this event after authentication. Caching modules use this event to determine if the request should be processed by its cache or if a handler should process the request.
    • UpdateRequestCache: Call this event after a response from the handler. Caching modules should update their cache with the response.
  7. Implement the Init and Dispose methods of the IHttpModule interface as follows:

    public void Init(HttpApplication app)
    {
       app.BeginRequest += new EventHandler(OnBeginRequest);
    }
    public void Dispose(){ }
    
  8. Create a delegate for an event as follows:

    public delegate void MyEventHandler(Object s, EventArgs e);    
    
  9. Define a private local variable of the type MyEventHandler to hold a reference to the event:

    private MyEventHandler _eventHandler = null;                    
    
  10. Create an event that hooks up the delegate to the method in the Global.asax file or class that inherits from the HttpApplication object:

    public event MyEventHandler MyEvent
    {
       add { _eventHandler += value; }
       remove { _eventHandler -= value; }
    }
    
  11. Create the OnBeginRequest method, which hooks up to the BeginRequest event of HttpApplication:

    public void OnBeginRequest(Object s, EventArgs e)
    {
       HttpApplication app = s as HttpApplication;
       app.Context.Response.Write("Hello from OnBeginRequest in custom module.<br>");
       if(_eventHandler!=null)
          _eventHandler(this, null);
    }        
    
  12. Compile the project

source: http://support.microsoft.com/kb/307996

Adding an HTTP Module to your web.config will look something like the following:

<system.web>
    <httpModules>
       <add name="CustomHttpModule" type="MyCustomHttpModule"/>
    </httpModules>
</system.web>
like image 80
Chris Ballance Avatar answered Sep 21 '22 20:09

Chris Ballance