【问题标题】:CORS vs Autofac vs Exception handling middleware in Owin, which one goes first?CORS vs Autofac vs Owin中的异常处理中间件,哪个先行?
【发布时间】:2018-02-02 16:29:25
【问题描述】:

我有一个使用 CORS 和 Autofac 的 owin 设置。 Autofac 文档说“首先注册 Autofac 中间件。”,很多人说 app.UseCors 应该是第一件事。

我还有一个异常处理中间件,很多人也说它应该是第一件事,这样“其他中间件(在堆栈跟踪中)将向上传播并被该中间件的 try/catch 块捕获。”,以及这很有意义,因为实现看起来像:

        try
        {
            await Next.Invoke(context);
        }
        catch...

哪一个应该是第一个? 这 3 个中间件组件的正确顺序是什么

我当前的启动配置如下所示:

    public void Configuration(IAppBuilder app) {
        // Add WebApi CORS handling to the OWIN pipeline
        app.UseCors(CorsOptions.AllowAll);

        // Create and register Owin HttpConfig instance
        var config = new HttpConfiguration
        {
            IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always // Enable error details in http responses
        };

        // Register the Autofac middleware FIRST. This also adds Autofac-injected middleware
        // registered with the container.
        var container = AutofacConfig.ConfigureContainer(config);
        app.UseAutofacMiddleware(container);
        app.UseAutofacWebApi(config);

        app.UseNLog();

        // Handle exceptions from OWIN middleware components globally
        app.UseExceptionHandling();

        app.UseOAuth(config);
        //... quite a bit more stuff after

【问题讨论】:

    标签: owin autofac startup owin-middleware


    【解决方案1】:

    我认为当文档说“首先注册 Autofac 中间件”时,它是在说明以下三个组件之间的正确顺序:1)Autofac 中间件,2)Autofac Web API 中间件,3)标准 Web API 中间件。

    CORS 不在此列表中,应首先对其进行配置:您必须在调用 .UseWebApi 或任何其他使用请求的中间件之前调用 .UseCors。由任何中间件管理的任何请求都应包含 CORS 访问控制标头,否则您可能会遇到跨域访问错误。我想这也适用于您的错误处理中间件。所以它应该在 CORS 之后配置(但在其他中间件组件之前,如果你想处理它们的异常)。

    this page 中有关 CORS 和 Web API 的更多信息。有关 CORS 详细信息的更多信息here

    事实上,确切的 Autofac 文档注释准确地说是“首先注册 Autofac 中间件,然后是 Autofac Web API 中间件,最后是标准 Web API 中间件”。在该文档的代码示例中,他们有标准的 web api 设置代码,然后是 OWIN web api 设置(这是“第一”适用的部分)。

    public class Startup
    {
      public void Configuration(IAppBuilder app)
      {
        var builder = new ContainerBuilder();
    
        // STANDARD WEB API SETUP:
    
        // Get your HttpConfiguration. In OWIN, you'll create one
        // rather than using GlobalConfiguration.
        var config = new HttpConfiguration();
    
        // Register your Web API controllers.
        builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
    
        // Run other optional steps, like registering filters,
        // per-controller-type services, etc., then set the dependency resolver
        // to be Autofac.
        var container = builder.Build();
        config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
    
        // OWIN WEB API SETUP:
    
        // Register the Autofac middleware FIRST, then the Autofac Web API middleware,
        // and finally the standard Web API middleware.
        app.UseAutofacMiddleware(container);
        app.UseAutofacWebApi(config);
        app.UseWebApi(config);
      }
    }
    

    我得到代码示例的文档页面是here(我猜你检查的那个)。

    【讨论】:

      猜你喜欢
      • 2018-06-15
      • 1970-01-01
      • 2016-04-01
      • 1970-01-01
      • 2016-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-11
      相关资源
      最近更新 更多