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

时间:2017-06-16 15:31:52

标签: c# asp.net iis asp.net-web-api2 owin-middleware

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

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

当我查看IOwinContext上的响应主体时,它是空的。但是,我可以从HttpContext.Response.Filter获得响应。当我使用HttpContext并点击断点时,在我继续之前不会发回响应。下面是我们的Startup.cs类中使用的当前配置方法。

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)

任何见解或发人深省都会受到赞赏。另外,如果这还不够,请告诉我。

1 个答案:

答案 0 :(得分:0)

所以,正如我在上面的帖子中所说,我认为问题是在IOwinResponse之前发送回的HttpResponse。事实证明,我完全忽略了映射部分:

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()时,它会分支中间件。所以,如果路径匹配&#34; / api&#34;它会分支。但是,它仍然使用app.UseWebApi()组件,这就是为什么我有两个不同的响应,以及为什么我期待的响应没有写入Middleware3组件{{{ 1}}。 我通过删除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>(); Middleware1Middleware2的开头:

Middleware3

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