0

我在 autofac 中注册了每个请求的生命周期中间件(它有一些依赖项)

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

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

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

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

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

我怎样才能为某个自动注册的中间件做到这一点?

4

1 回答 1

0

来自 autofac 文档: 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>();
于 2017-04-19T13:03:59.490 回答