OWIN Service resolution Using Autofac

asp.net-web-api, autofac, dependency-injection, owin

Solution

The `IOwinContext.Get` uses the `Environment` dictionary, resolving objects registered directly with Owin, it does not take into account Autofac container.

I managed to do it by accessing the Autofac OwinLifetimeScope in the Environment property and using the scope to resolve the service.

You can access the LifetimeScope using this code

var scope=OwinContext.Get<Autofac.Core.Lifetime.LifetimeScope>("autofac:OwinLifetimeScope"); 

and then

scope.GetService(type)

You should check for nulls and write it in a better way, as Extension method maybe.

Problem

I have an WebApi application using OWIN and Autofac. Although controllers and parameters get resolved correctly, I would like to be able to use `OwinContext.Get<type>` to resolve types registered with Autofac. Is that posible? Already set`app.UseAutofacMiddleware(container);` and `config.DependencyResolver = new AutofacWebApiDependencyResolver(container);` By example, I registered `builder.Register<IAppConfiguration>(c => new AppConfig());` and I'd like to resolve it using `owinContext.Get<IAppConfiguration>()`.

Original source