5

New MVC 4 web application using autofac 3.0 on IIS 7.5. How do I inject a dependency into an IHttpModule?

I tried constructor injection which resulted in:

Constructor on type 'AnonymousIdentityModule' not found

So it seems the internals require a parameterless constructor for http modules. I also tried property injection too but that resulted in no dependency actually being injected.

Registration

builder.RegisterType<AnonymousIdentityModule>().As<IHttpModule>().PropertiesAutowired().InstancePerHttpRequest();

IHttpModule Code

public class AnonymousIdentityModule : IHttpModule
{
    private readonly IServiceManager _serviceManager;

    // this causes "constructor not found" exception 
    public AnonymousIdentityModule(IServiceManager serviceManager)
    {
        _serviceManager = serviceManager;
    }

    // never assigned by autofac
    public IServiceManager ServiceManager
    {
        get { return _serviceManager; }
        set { _serviceManager = value; }
    }
    ...
}

web.config

  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <modules>
      <add name="AnonymousIdentityModule" type="AnonymousIdentityModule" />
    </modules>
  </system.webServer>

I found this old article related to Windsor but did not see an equivalent in autofac.

TugboatCaptain
  • 4,150
  • 3
  • 47
  • 79

1 Answers1

7

Check out this SO question: IoC Dependency injection into Custom HTTP Module - how? (ASP.NET)

and this post by Phil Haack: http://haacked.com/archive/2011/06/02/dependency-injection-with-asp-net-httpmodules.aspx

They both talk about providing DI to HttpModules by creating another HttpModule to initialize them. And PH has provided a nuget package of his HttpModuleMagic if you want it:

PM> Install-Package HttpModuleMagic

But because HttpModules are only created once they are a kind of singleton, and your dependency also has to be a singleton (or rather, a single instance).

So, if you need a per-request dependency, check out this post: http://blog.sapiensworks.com/post/2013/03/18/Http-Module-Dependecy-Injection-with-Autofac-Gotcha.aspx

This looks at using a Factory function to retrieve a properly scoped dependency when needed.

Community
  • 1
  • 1
Simon C
  • 9,458
  • 3
  • 36
  • 55
  • 1
    I tried HtpModuleMagic combined with that sapiensworks article and the first request worked but subsequent requests were throwing errors about the DbContext being disposed. My service manager has a long chain of dependencies including the entity framework that I'm assuming would all need factories to instantiate them correctly. I decided to just put my code in a base controller and have all my controllers inherit from it. Much cleaner than re-writing tons of code just to achieve injection in one silly http module. – TugboatCaptain May 29 '13 at 06:42
  • @TugboatCaptain, it's been a long time, but usually declaring a dependency on a factory for a given type (call it `MyService`) is as easy as depending on `Func>`. You don't have to write any factory code, Autofac does it for you. – Marc L. Sep 28 '15 at 18:36