将管道阶段标记用于autofac中间件

时间:2017-04-19 11:40:07

标签: asp.net-mvc autofac owin-middleware

我在autofac中注册了每个请求的终身中间件(它有一些依赖性)

builder.RegisterType<RequestLocaleSetter>().InstancePerRequest();

然后我在startup.cs注册所有autofac中间件

        app.UseAutofacMiddleware(resolver.ApplicationContainer);
        app.UseAutofacMvc();

我可以为在startup.cs中注册的中间件设置一个管道阶段,如

        app.Use(typeof(RequestLocaleSetter));
        app.UseStageMarker(PipelineStage.PostAcquireState);

如何为某个autofac注册的中间件执行此操作?

1 个答案:

答案 0 :(得分:0)

来自autofac docs: http://docs.autofac.org/en/latest/integration/owin.html#controlling-middleware-order

  

如果您想要更多地控制何时将DI启用的中间件添加到管道中,您可以使用UseAutofacLifetimeScopeInjector和UseMiddlewareFromContainer扩展。

 var builder = new ContainerBuilder();
 builder.RegisterType<MyCustomMiddleware>(); //... var container =
 builder.Build();

 // This adds ONLY the Autofac lifetime scope to the pipeline.
 app.UseAutofacLifetimeScopeInjector(container);

 // Now you can add middleware from the container into the pipeline //
 wherever you like. For example, this adds custom DI-enabled middleware
 // AFTER the Web API middleware/handling. app.UseWebApi(config);
 app.UseMiddlewareFromContainer<MyCustomMiddleware>();