using Autofac; using Microsoft.Owin; using Microsoft.Owin.Builder; using Owin; public class Startup { public void Configuration(IAppBuilder appBuilder) { var builder = new ContainerBuilder(); builder.RegisterType().As ().InstancePerLifetimeScope(); var container = builder.Build(); appBuilder.UseAutofacMiddleware(container); appBuilder.UseAutofacMvc(); appBuilder.UseAutofacWebApi(); appBuilder.Use((context, next) => { var lifetimeScope = context.GetAutofacLifetimeScope(); var myService = lifetimeScope.Resolve (); // Use myService here return next.Invoke(); }); // more middleware here } }
using Autofac; using Microsoft.Owin; using Microsoft.Owin.Builder; using Owin; public class Startup { public void Configuration(IAppBuilder appBuilder) { var builder = new ContainerBuilder(); builder.RegisterTypeThis example shows how to use the `GetAutofacLifetimeScope` method to resolve a service instance from the Autofac container. The `MyService` type is registered with the `InstancePerLifetimeScope()` configuration, which means that a new instance of the service will be created for each request. Package library: Autofac.Owin.().As ().InstancePerLifetimeScope(); var container = builder.Build(); appBuilder.UseAutofacMiddleware(container); appBuilder.Use((context, next) => { var lifetimeScope = context.GetAutofacLifetimeScope(); var myService = lifetimeScope.Resolve (); // Use myService here return next.Invoke(); }); // more middleware here } }