title: autofac - dependency injection into IHttpModule
body: <p>New MVC 4 web application using autofac 3.0 on IIS 7.5. How do I inject a dependency into an IHttpModule?</p>
  
  <p>I tried constructor injection which resulted in: </p>
  
  <blockquote>
    <p>Constructor on type 'AnonymousIdentityModule' not found</p>
  </blockquote>
  
  <p>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.</p>
  
  <p><strong>Registration</strong></p>
  
  <pre><code>builder.RegisterType&lt;AnonymousIdentityModule&gt;().As&lt;IHttpModule&gt;().PropertiesAutowired().InstancePerHttpRequest();
  </code></pre>
  
  <p><strong>IHttpModule Code</strong></p>
  
  <pre><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; }
      }
      ...
  }
  </code></pre>
  
  <p><strong>web.config</strong></p>
  
  <pre><code>  &lt;system.webServer&gt;
      &lt;validation validateIntegratedModeConfiguration="false" /&gt;
      &lt;modules&gt;
        &lt;add name="AnonymousIdentityModule" type="AnonymousIdentityModule" /&gt;
      &lt;/modules&gt;
    &lt;/system.webServer&gt;
  </code></pre>
  
  <p>I found this old <strong><a href="http://bugsquash.blogspot.com/2009/11/windsor-managed-httpmodules.html" rel="noreferrer">article</a></strong> related to Windsor but did not see an equivalent in autofac.</p>