13

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.

Caveatrob
  • 12,667
  • 32
  • 107
  • 187

5 Answers5

13

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>
Jason Plank
  • 2,336
  • 5
  • 31
  • 40
Chris Ballance
  • 33,810
  • 26
  • 104
  • 151
12

Two common reasons for breakpoints not being hit in an HttpModule are:

The breakpoint is in code that is called during the module's construction.

This one always get's me. If you want to debug the module's construction you should manually attach the debugger. To do this on Windows 7 (it'll be similar on Vista):

  1. Debug | Attach to Process...
  2. Check Show processes in all sessions.
  3. Select the your worker process instance (w3wp.exe)
  4. Click Attach.

Refresh your browser and you should now hit your breakpoint.

The module is not actually being loaded because of a configuration issue.

If you can tell that the module code is definitely being executed then this is not the problem. But sometimes, while a module is loading fine on other machines it might not be loading on your dev box because you have a different version of IIS and you don't have the necessary config.

If this is the the case then make sure the module is wired up correctly for all required versions of IIS.

For IIS 5.1 and IIS 6...

<system.web>
  <httpModules>
    <add name="CustomHttpModule" type="MyCustomHttpModule"/>
  </httpModules>
</system.web>

...and for IIS 7+

<system.webServer>
  <modules runAllManagedModulesForAllRequests="true">
    <add name="CustomHttpModule" type="MyCustomHttpModule"/>
  </modules>
</system.webServer>
stucampbell
  • 6,383
  • 5
  • 26
  • 25
  • 1
    Had the same problem, My web config was set up for IIS 6, but should have been 7. I notices that I did not need the "runAllManagedModulesForAllRequests" attribute +1 thanks – Dai Bok Jun 01 '12 at 08:42
  • 1
    If you are working with the development server that is integrated with Visual Studio (that is IIS Express) then you should attach the debugger to iisexpress.exe and not to w3wp.exe. – pholpar Aug 03 '18 at 12:01
  • If you want to debug your module's construtor or its Init method with IIS, that code is typically invoked already by the time you see the process in Task Manager or in the process list in Visual Studio. The only method that seems to work in my case (Windows Server 2019 + VS2019) was `Debugger.Launch()`. [See details here.](https://weblog.west-wind.com/posts/2011/Dec/15/Debugging-ApplicationStart-and-Module-Initialization-with-IIS-and-Visual-Studio) Don't forget to remove that line from the code before rolling out into production, or use a condition to enable it only in the debug build! – pholpar Sep 07 '22 at 11:31
5

Take note that this doesn't appear to work out of the box. Make sure to not just include the new module's class name but instead explicitly define its namespace and (for the heck of completeness) assembly. If you fail to do that, there's a good chance you'll end up with IIS complaining about not being able to load that new HTTP module you just coded.

An example follows.

Instead of:

<add name="CustomHttpModule" type="MyCustomHttpModule"/>

Use:

<add name="CustomHttpModule" type="MyCustomRootNamespace.MyCustomHttpModule, MyCustomAssembly"/>

Fail to do that and your project may fail to load.

Specific errors you may receive depend on how explicitly you've defined the module. If you leave out the namespace, you'll see:

Could not load type 'MyCustomHttpModule'.

Leave out the namespace and include the assembly and you'll instead see:

Could not load type 'MyCustomHttpModule' from assembly 'MyCustomAssembly'. 

If you instead see another error message, you're suffering from a different problem than the one discussed here.

Geoff
  • 111
  • 3
  • 3
3

You can write the line

System.Diagnostics.Debugger.Break(); //Explicitly calling Break to debug

to explicitly call the break point during the HttpModule consturction .

It will ask to attach the process (e.g. w3wp.exe) then the debug point will hit .

Thanks

Sibananda
  • 31
  • 1
0

You may also want to make sure you are using web application projects, which is what you were using in VS2003, and not web site "projects", which are not projects.

John Saunders
  • 160,644
  • 26
  • 247
  • 397