title: Howto get injection working with Topshelf, Autofac and self hosted Owin
body: <p>No matter what i do i cannot get injection working with Topshelf, Autofac and self hosted Owin.</p>
  
  <p>I have followed the documentation in <a href="http://autofac.readthedocs.org/en/latest/integration/webapi.html#owin-integration" rel="nofollow noreferrer">http://autofac.readthedocs.org/en/latest/integration/webapi.html#owin-integration</a> and read <a href="https://stackoverflow.com/questions/23957038/autofac-webapi-2-owin-not-working">Autofac WebApi 2 OWIN Not Working</a>, but I am still unable to inject a simple class into my apicontroller.</p>
  
  <p>The 'almost' complete app is posted here.</p>
  
  <p>No matter what i do I cannot get an IEmail instance injected into the EmailController.  Can anyone suggest a solution</p>
  
  <pre><code>// topshelf startup code
  class Program
  {
      static void Main(string[] args)
      {
  
          HostFactory.Run(c =&gt;
          {
              //c.UseAutofacContainer(container);         
              c.RunAsNetworkService();
  
              c.Service&lt;SampleService&gt;(s =&gt;
              {
                  s.ConstructUsing(name =&gt; new SampleService());
                  s.WhenStarted((service, control) =&gt; service.Start());
                  s.WhenStopped((service, control) =&gt; service.Stop());
              });
  
          });
      }
  }
  
  // lifted from http://autofac.readthedocs.org/en/latest/integration/webapi.html#owin-integration
  public class StartupConfig
  {
      public void Configure(IAppBuilder appBuilder)
      {
          var config = new HttpConfiguration();
          config.MapHttpAttributeRoutes();    // using attribute based routing because I prefer it
  
          var builder = new Autofac.ContainerBuilder();                                    // Create the container builder.
          builder.RegisterApiControllers(Assembly.GetExecutingAssembly());                 // Register the Web API controllers.
          builder.RegisterWebApiFilterProvider(config);                                    // optional
  
          builder.RegisterType&lt;Email&gt;().As&lt;IEmail&gt;().InstancePerRequest();
  
  
          var container = builder.Build();
          config.DependencyResolver = new AutofacWebApiDependencyResolver(container);    
  
          appBuilder.UseAutofacMiddleware(container);
          appBuilder.UseAutofacWebApi(config);                                            // Make sure the Autofac lifetime scope is passed to Web API.
          appBuilder.UseWebApi(config);       // enable web-api
  
  
          string filedir = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "../../web");
          appBuilder.UseFileServer(new FileServerOptions
          {
              EnableDefaultFiles = true,
              DefaultFilesOptions =
              {
                  DefaultFileNames = new[] { "Index.html" }
              },
              EnableDirectoryBrowsing = true,
              FileSystem = new PhysicalFileSystem(filedir),
          });      
      }
  }
  
  // topshelf hosted service to start
  public class SampleService
  {
      public bool Start()
      {
          if (WebApplication == null)
          {
              WebApplication = WebApp.Start
              (
                   new StartOptions
                   {
                       Port = 1234
                   },
                   appBuilder =&gt;
                   {
                       new StartupConfig().Configure(appBuilder);
                   }
              );
          }
  
          return true;
      }
  
      public bool Stop()
      {
          return true;
      }
  
      protected IDisposable WebApplication
      {
          get;
          set;
      }
  }
  
  // sample controller
  public class EmailController : ApiController
  {
  
      public IEmail MyModel; /** always NULL **/
  
      [HttpGet]
      [Route("api/emails/{id}")]
      public IHttpActionResult get(int id)
      {
  
      }
  }
  </code></pre>
  
  <p>Nuget packages </p>
  
  <pre><code>&lt;packages&gt;
        &lt;package id="Autofac" version="3.5.0" targetFramework="net452" /&gt;
        &lt;package id="Autofac.Owin" version="3.1.0" targetFramework="net452" /&gt;
        &lt;package id="Autofac.WebApi" version="3.1.0" targetFramework="net452" /&gt;
        &lt;package id="Autofac.WebApi2" version="3.4.0" targetFramework="net452" /&gt;
        &lt;package id="Autofac.WebApi2.Owin" version="3.3.0" targetFramework="net452" /&gt;
        &lt;package id="EntityFramework" version="6.0.0" targetFramework="net452" /&gt;
        &lt;package id="Microsoft.AspNet.WebApi.Client" version="5.2.2" targetFramework="net45" /&gt;
        &lt;package id="Microsoft.AspNet.WebApi.Core" version="5.2.2" targetFramework="net45" /&gt;
        &lt;package id="Microsoft.AspNet.WebApi.Owin" version="5.2.2" targetFramework="net45" /&gt;
        &lt;package id="Microsoft.Owin" version="3.0.1" targetFramework="net452" /&gt;
        &lt;package id="Microsoft.Owin.FileSystems" version="3.0.1" targetFramework="net452" /&gt;
        &lt;package id="Microsoft.Owin.Host.HttpListener" version="3.0.1" targetFramework="net452" /&gt;
        &lt;package id="Microsoft.Owin.Hosting" version="3.0.0" targetFramework="net45" /&gt;
        &lt;package id="Microsoft.Owin.StaticFiles" version="3.0.1" targetFramework="net452" /&gt;
        &lt;package id="Newtonsoft.Json" version="6.0.4" targetFramework="net45" /&gt;
        &lt;package id="Owin" version="1.0" targetFramework="net45" /&gt;
        &lt;package id="Topshelf" version="3.1.4" targetFramework="net45" /&gt;
        &lt;package id="Topshelf.Autofac" version="1.0.0" targetFramework="net452" /&gt;
      &lt;/packages&gt;
  </code></pre>