48

I'm writing an app where 3rd party vendors can write plugin DLLs and drop them into the web app's bin directory. I want the ability for these plugins to be able to register their own HttpModules if necessary.

Is there anyway that I can add or remove HttpModules from and to the pipeline at runtime without having a corresponding entry in the Web.Config, or do I have to programmatically edit the Web.Config when adding / removing modules? I know that either way is going to cause an AppDomain restart but I'd rather be able to do it in code than having to fudge the web.config to achieve the same effect.

alexandrul
  • 12,856
  • 13
  • 72
  • 99
jmcd
  • 4,269
  • 5
  • 36
  • 36

5 Answers5

54

It has to be done at just the right time in the HttpApplication life cycle which is when the HttpApplication object initializes (multiple times, once for each instance of HttpApplication). The only method where this works correct is HttpApplication Init().

To hook up a module via code you can run code like the following instead of the HttpModule definition in web.config:

  public class Global : System.Web.HttpApplication
  {
     // some modules use explicit interface implementation
     // by declaring this static member as the IHttpModule interface
     // we work around that
     public static IHttpModule Module = new xrnsToashxMappingModule();
     public override void Init()
     {
         base.Init();
         Module.Init(this);
     }
  }

All you do is override the HttpApplication's Init() method and then access the static instance's Init method. Init() of the module hooks up the event and off you go.

Via Rick Strahl's blog

John Leidegren
  • 59,920
  • 20
  • 131
  • 152
Aaron Fischer
  • 20,853
  • 18
  • 75
  • 116
  • One of the modules we are using is working correctly when referenced from web.config, but can't be instantiated directly (as shown in your code) because it's declared friend. Any idea of which context should I instantiate it from? – Patonza Dec 01 '09 at 11:20
29

Realize this is an old question, but asp.net 4 provides some new capabilities that can help here.

Specifically, ASP.NET 4 provides a PreApplicationStartMethod capability that can be used to add HttpModules programmatically.

I just did a blog post on that at http://www.nikhilk.net/Config-Free-HttpModule-Registration.aspx.

The basic idea is you create a derived HttpApplication that provides ability to add HttpModules dynamically at startup time, and it then initializes them into the pipeline whenever each HttpApplication instance is created within the app-domain.

Ian Kemp
  • 28,293
  • 19
  • 112
  • 138
Nikhil Kothari
  • 5,215
  • 2
  • 22
  • 28
  • 13
    Blog dead... wayback link: http://wayback.archive.org/web/20120719043729/http://www.nikhilk.net/Config-Free-HttpModule-Registration.aspx – felickz Jan 31 '13 at 16:58
13

The dll Microsoft.Web.Infrastructure.dll has a method for this inside the class DynamicModuleUtility. The dll is shipped with WebPages 1.0

public static class PreApplicationStartCode
{
    private static bool _startWasCalled;

    public static void Start()
    {
        if (_startWasCalled) return;

        _startWasCalled = true;
        DynamicModuleUtility.RegisterModule(typeof(EventTriggeringHttpModule));
    }
}
3

In new versions of ASP MVC you can use Package Manager to add a reference to WebActivatorX and then do something like this

using WhateverNameSpacesYouNeed;

[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(YourApp.SomeNameSpace.YourClass), "Initialize")]
namespace YourApp.SomeNameSpace
{
  public static void Initialize()
  {
    DynamicModuleUtility.RegisterModule( ... the type that implements IHttpModule ... );
  }
}
Peter Morris
  • 20,174
  • 9
  • 81
  • 146
3

This worked for me for dynamic registration.

RegisterModule(typeof(RequestLoggerModule));

public class RequestLoggerModule : IHttpModule
    { ... }

https://learn.microsoft.com/en-us/dotnet/api/system.web.httpapplication.registermodule?view=netframework-4.7.2

Jesse
  • 1,673
  • 1
  • 16
  • 22