Register your HTTP modules at runtime without config

comments edit

In ASP.NET 4, we added the concept of a PreApplicationStart method that an assembly can use to execute code early on in the appdomain without any configuration. Phil Haack covered it a while back in this post. It's pretty simple to use. You just define a class that looks like:

public class PreApplicationStartCode {
    public static void Start() {
        // Your startup code here
    }
}

And then you add an assembly level attribute pointing to it:

[assembly: PreApplicationStartMethod(typeof(PreApplicationStartCode), "Start")]

With the release of MVC3 and ASP.NET Web Pages, we added another little gem: a RegisterModule() API that lets you dynamically register an IHttpModule without touching config. Sadly, the method is hidden so deep that it is hard to find by accident (it'll get cleaned up in the next framework version).

By combining the two techniques, you have everything you need to register a module dynamically, e.g.

public class PreApplicationStartCode {
    public static void Start() {
        // Register our module
        Microsoft.Web.Infrastructure.DynamicModuleHelper.DynamicModuleUtility.RegisterModule(typeof(MyModule));
    }
}

I warned you it was well hidden! :)

Update: in recent versions of the framework, the method is much easier to find. Just call HttpApplication.RegisterModule.

The module type that you pass in to that is just a standard IHttpModule, e.g. here is a basic module that writes to the response on every request:

class MyModule : IHttpModule {
    public void Init(HttpApplication context) {
        context.BeginRequest += (sender, e) => {
            var response = ((HttpApplication)sender).Response;
            response.Write("MyModule.BeginRequest");
        };
    }

    public void Dispose() { }
}

The beauty of this is that it allows you to create fully encapsulated assemblies that you can just drop into a web app's bin folder and have them light up without having to add any ugly registration to the app.

And yes, all this works fine in partial trust!

You can download a minimal sample from here.

Comments