title: Autofac Injection of data into OWIN Startup class
body: <p>I've been looking around on SO for a while and can't find the answer to my particular question:</p>
  
  <p>How do you use Autofac to inject data (via constructor or property) into the OWIN Startup class?</p>
  
  <p>I'm using Autofac in a Windows service which also handles creation of back-end services etc. so I'm doing all the configuration reading and container building in there.</p>
  
  <p>I want to be able to register the Startup class so I can inject allowed origins for CORS but even when I register the object with a property like so:</p>
  
  <pre><code>var builder = new ContainerBuilder();
  // This will actually be read from config.
  var origins = new[]
                {
                    "http://localhost",
                    "http://localhost:8082",
                };
  builder.Register(c =&gt; new Startup(origins));
  var container = builder.Build();
  </code></pre>
  
  <p>At runtime when the Startup class is instantiatedthe callstack shows it comes from external code and my Autofac builder didn't push the property in.</p>
  
  <p>I'd ideally like to keep my Autofac registrations in a single place (the Windows service class) and just inject the required data to Startup so I can just do something like this below (where <code>allowedOrigins</code> is set as a property or injected via the constructor)</p>
  
  <pre><code> public void Configuration(IAppBuilder app)
   {
       var configuration = new HttpConfiguration();
       ...
       var origins = string.Join(",", allowedOrigins);
       var cors = new EnableCorsAttribute(origins, "*", "*") { SupportsCredentials = true };
       configuration.EnableCors(cors);
       ....
   }
  </code></pre>
  
  <p>Any ideas would be much appreciated.
  Thanks</p>
  
  <p>peteski</p>
  
  <p><em>UPDATE</em></p>
  
  <p>I should add that after attempting Autofac registration and building, I was kicking off the OWIN self host app by doing:</p>
  
  <pre><code>var webApp = WebApp.Start&lt;Startup&gt;(baseAddress);
  </code></pre>
  
  <p>From a conversation with a friend they suggested creating the <code>Startup</code> object and passing it to the <code>WebApp</code>:</p>
  
  <pre><code>var startup = new Startup(origins);
  var webApp = WebApp.Start(startup, baseAddress);
  </code></pre>
  
  <p>Here, I'd add the <code>IEnumerable&lt;string&gt;</code> origins to the ctor for the <code>Startup</code> class. This does actually work! But feels like it goes around using Autofac to handle giving the Startup class what it needs.</p>