HTT prequestMessage.Content当它在ASP.Net的Web API测井DelegatingHandler读取丢失 [英] HttpRequestMessage.Content is lost when it is read in a logging DelegatingHandler in ASP.Net Web API

查看:250
本文介绍了HTT prequestMessage.Content当它在ASP.Net的Web API测井DelegatingHandler读取丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当在一个控制器的动作试图为一个对象零星它似乎为空。我发现,这是由于 ReadAsStringAsync() SendAsync()覆盖> DelegatingHandler 。该问题是与该内容。当我的客户端发送一个内容主体,它是在记录器读取它永远不会被控制器操作调用程序读取(或可能在某处 JsonFormatter )。我怀疑的后续调用Content.ReadAsStringAsync()犯规抛出一个异常,而且犯规未返回预期的内容主体(返回一些信息,说明该异步读取完毕)

When trying to an object in an Action in a Controller it sporadically seems to be null. I discovered that it is due to the ReadAsStringAsync() in the SendAsync() override of the DelegatingHandler. The issue is with the content. When my client sends a content body and it is read in the logger it is never read by the Controller Action Invoker (or may be somewhere in the JsonFormatter). I suspect the subsequent call to Content.ReadAsStringAsync() doesnt throw an exception but also doesnt not return the expected content body (some info is returned stating that the async read is completed).

但我的问题仍然因为我想读一个动作一个 [FromBody] 参数,它是空当的竞争危害 Content.ReadStringAsync 是由 DelegatingHandler 赢了。当 JsonFormatter 虽然赢了,我得到的对象,但是这是罕见的(仅在服务启动时)。

But my problem remains since I want to read a [FromBody] parameter in an action and it is null when the RaceCondition of Content.ReadStringAsync is won by the DelegatingHandler. When JsonFormatter wins it though, I get the object but that is rare (only at service startup).

下面是我的 DelegatingHandler code:

Here is my DelegatingHandler code:

public class LogHandler : DelegatingHandler
{

protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        var apiRequest = new WebApiUsageRequest(request);
        WriteLog(apiRequest);
        request.Content.ReadAsStringAsync().ContinueWith(t =>
        {
            apiRequest.Content = t.Result;
            WriteLog(apiRequest);
        });

        return base.SendAsync(request, cancellationToken).ContinueWith(task =>
        {
            var apiResponse = new WebApiUsageResponse(task.Result);
            apiResponse.Content = task.Result.Content != null ? task.Result.Content.ReadAsStringAsync().Result : null;
            WriteLog(apiResponse);
            return task.Result;
        });
    }
}

有没有人有一个线索,这个问题的解决?

Does anyone have a clue to the solution of this issue?

推荐答案

这是设计的。在ASP.NET的Web API的主体内容进行处理,只能读一次只进流。

This is by design. In ASP.NET Web API the body content is treated a forward-only stream that can only be read once.

您可以尝试使用的ASP.NET Web API跟踪,但我还没有与POST请求测试它没有,所以我不知道如何/如果追查请求主体(它是跟踪参数肯定GET请求)。你可以在这里阅读更多:

You might try utilising ASP.NET Web API Tracing but I haven't test it with POST request yet so I'm not sure how/if it is tracing the request body (it is tracing parameters for GET request for sure). You can read more here:

  • Tracing in ASP.NET Web API (Ron Cain)
  • ASP.NET Web API Tracing Preview (Ron Cain)
  • Tracing in ASP.NET Web API (Mike Wasson)

这篇关于HTT prequestMessage.Content当它在ASP.Net的Web API测井DelegatingHandler读取丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆