ASP.NET Web API HttpContext Response 在 IOwinContext Response 之前发回

ASP.NET Web API HttpContext Response is sent back before IOwinContext Response

我们在 IIS 中托管的 ASP.NET Web API 2 项目中使用 Owin 中间件。

我目前遇到一个奇怪的现象,即 IOwinContext.Response.Body 没有被写入,实际上,即使我在唤醒 Next.Invoke() 后在中间件中设置了断点,并且中了,还没继续,响应已经发回服务器了。

当我查看 IOwinContext 上的响应主体时,它是空的。但是,我可以从 HttpContext.Response.Filter 得到响应。当我使用 HttpContext 并到达断点时,在我继续之前不会发回响应。以下是我们 Startup.cs class.

中使用的当前配置方法
public async void Configuration(IAppBuilder app)
{
    try
    {
        // Global Config
        var config = GlobalConfiguration.Configuration;

        // configure dependency injection
        UnityConfig.RegisterComponents();

        // configure log for net
        log4net.Config.XmlConfigurator.Configure();

        // turn around all requests right here
        app.Use(async (context, next) =>
        {
            if (context.Request.Path.ToString() == "/")
            {
                string text = "UP";
                context.Response.StatusCode = 200;
                context.Response.ReasonPhrase = text;
                await context.Response.WriteAsync(text);
                return;
            }

            await next.Invoke();
        });

        // Handle exceptions in the OWIN layer here
        app.UseUncaughtExceptionHandler();

        // add cors headers
        app.Use(async (context, next) => { });

        // some UI stuff
        app.Use(async (context, next) => { });

        // Log Request Metrics
        app.UseLogRequestMetrics();

        // Evaluate Partner Key
        app.MapWhen(context => Regex.IsMatch(context.Request.Uri.PathAndQuery.ToLower(), @"/api"), newApp =>
        {
#if !DEBUG
            newApp.Use<Middleware1>();
#endif
            newApp.Use<Middleware2>();

            newApp.Use<Middleware3>(); // On the response path back, the IOwinResponse body is already empty
        });

        WebApiConfig.Register(config);

        app.UseWebApi(config); // It seems like I'm losing the response in here, but I don't really know

        config.EnsureInitialized();

        // Configure object mapping
        AutoMapperConfig.Configure();
    }
    catch (Exception ex)
    {
        await LogForNetErrorLogger.LogError(ex);
    }
}

我很确定我的中间件搞砸了,但是在等待 Next.Invoke()

之后返回到我的第一个中间件 (Middleware3) 之前,响应已经消失了

任何见解或发人深省的想法都将不胜感激。另外,如果这还不够,请告诉我。

因此,正如我上面的 post 一样,我认为问题是 HttpResponse 在 IOwinResponse 之前被发回。事实证明,我完全忽略了映射部分:

app.MapWhen(context => Regex.IsMatch(context.Request.Uri.PathAndQuery.ToLower(), @"/api"), newApp =>
{
#if !DEBUG
    newApp.Use<Middleware1>();
#endif
    newApp.Use<Middleware2>();

    newApp.Use<Middleware3>();
});

当您使用 app.Map() 时,它会分支中间件。因此,如果路径匹配“/api”,它就会分支。但是,它还在使用 app.UseWebApi() 组件,所以这就是为什么我有两个不同的响应以及为什么我期望的响应没有写入 Middleware3 组件的 IOwinContext。 我通过删除 app.MapWhen() 方法来修复它,将其更改为:

app.MapWhen(context => Regex.IsMatch(context.Request.Uri.PathAndQuery.ToLower(), @"/site"), newApp =>
{
#if !DEBUG
    newApp.Use<Middleware1>();
#endif
    newApp.Use<Middleware2>();

    newApp.Use<Middleware3>(); // On the response path back, the IOwinResponse body is already empty
});

对此:

#if !DEBUG
newApp.Use<Middleware1>();
#endif
newApp.Use<Middleware2>();

newApp.Use<Middleware3>();

并将这段代码放在中间件组件的开头Middleware1Middleware2Middleware3:

public override async Task Invoke(IOwinContext context)
{
    if (!context.Request.Path.ToString().StartsWith("/api/"))
    {
        await Next.Invoke(context);

        return;
    }

    // stuff I want to run if the above doesn't match
    await Next.Invoke(context);

    ...
}

好吧,至少修复很简单,即使我花了三个星期才找到它。如果您想阅读 IAppBuilder.MapWhen 扩展方法,这里有一些文档 https://msdn.microsoft.com/en-us/library/owin.mapwhenextensions.mapwhen(v=vs.113).aspx.