title: Using Autofac for DI into WCF service hosted in ASP.NET application
body: <p>I'm having trouble injecting services dependencies into my WCF service using Autofac 1.4.5. I've read and followed the <a href="http://code.google.com/p/autofac/wiki/WcfIntegration" rel="noreferrer">Autofac wiki page on WcfIntegration</a> but my debugging shows me that my WCF service is created by the <code>System.ServiceModel.Dispatcher.InstanceBehavior.GetInstance()</code> method and not by the <code>AutofacWebServiceHostFactory</code>. What am I doing wrong?</p>
  
  <p>I've set up my <code>ajax.svc</code> file to look like the one in the example for use with <code>WebHttpBinding</code>:</p>
  
  <pre><code>&lt;%@ ServiceHost Language="C#" Debug="true"
      Service="Generic.Frontend.Web.Ajax, Generic.Frontend.Web"
      Factory="Autofac.Integration.Wcf.AutofacWebServiceHostFactory,
               Autofac.Integration.Wcf" %&gt;
  </code></pre>
  
  <p>My WCF service class <code>Ajax</code> is defined like this:</p>
  
  <pre><code>namespace Generic.Frontend.Web
  {
      [ServiceContract]
      [AspNetCompatibilityRequirements(
          RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
      public class Ajax
      {
          public MapWebService MapWebService { get; set;}
  
          public Ajax() {
              // this constructor is being called
          }
  
          public Ajax(MapWebService mapWebService)
          {
              // this constructor should be called
              MapWebService = mapWebService;
          }
  
          [WebGet(ResponseFormat = WebMessageFormat.Json)]
          [OperationContract(Name = "mapchange")]
          public MapChangeResult ProcessMapChange(string args)
          {
              // use the injected service here
              var result = MapWebService.ProcessMapChange(args);
              return result;
          }
      }
  }
  </code></pre>
  
  <p>Now I've used the wiring up in the <code>Global.asax.cs</code> as shown in the wiki mentioned above:</p>
  
  <pre><code>var builder = new ContainerBuilder();
  builder.RegisterModule(new AutofacModuleWebservice());
  var container = builder.Build();
  AutofacServiceHostFactory.Container = container;
  </code></pre>
  
  <p>with</p>
  
  <pre><code>class AutofacModuleWebservice : Module
  {
      protected override void Load(ContainerBuilder builder)
      {
          builder.Register&lt;Ajax&gt;();
          builder.Register&lt;MapWebService&gt;().ContainerScoped();
      }
  }
  </code></pre>
  
  <p>In my web.config I have</p>
  
  <pre><code>&lt;services&gt;
      &lt;service name="Generic.Frontend.Web.Ajax"&gt;
          &lt;endpoint address="http://mysite.com/ajax.svc/" binding="webHttpBinding"
                    contract="Generic.Frontend.Web.Ajax" /&gt;
      &lt;/service&gt;
  &lt;/services&gt;
  </code></pre>
  
  <p>.</p>
  
  <p>The service already works fine but I can't get the Autofac bits (read: creation/injection) to work. Any ideas?</p>
  
  <p><strong>Edit:</strong>
  Removing the default constructor unfortunately leads to the following exception:</p>
  
  <pre><code>System.InvalidOperationException:
  The service type provided could not be loaded as a service because it does not
  have a default (parameter-less) constructor. To fix the problem, add a default
  constructor to the type, or pass an instance of the type to the host.
  </code></pre>
  
  <p>Cheers, Oliver</p>