如何在netcore 3中多次读取http请求正文?

时间:2019-11-06 19:39:41

标签: c# asp.net-core asp.net-core-3.0

我有一个netcore 3 API应用程序,该应用程序记录传入的请求,然后将其传递给控制器​​操作。

我的代码如下:

public RequestLoggingHandler(IHttpContextAccessor httpContextAccessor)
{
    _httpContextAccessor = httpContextAccessor;
}


protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, RequestLoggingRequirement requirement)
{
    try
    {
        var httpContext = _httpContextAccessor.HttpContext;
        var request = httpContext.Request;

        _repository = (ICleanupOrderRepository)httpContext.RequestServices.GetService(typeof(ICleanupOrderRepository));
        _cache = (IMemoryCache)httpContext.RequestServices.GetService(typeof(IMemoryCache));

        httpContext.Items["requestId"] = SaveRequest(request);

        context.Succeed(requirement);

        return Task.CompletedTask;
    }
    catch (Exception ex)
    {

        throw ex;
    }
}

private int SaveRequest(HttpRequest request)
{
    try
    {
        // Allows using several time the stream in ASP.Net Core
        var buffer = new byte[Convert.ToInt32(request.ContentLength)];
       request.Body.ReadAsync(buffer, 0, buffer.Length);

        var requestContent = Encoding.UTF8.GetString(buffer);
        var requestId = _repository.SaveRawHandlerRequest($"{request.Scheme} {request.Host}{request.Path} {request.QueryString} {requestContent}");

        return requestId;
    }
    catch (Exception ex)
    {

        throw ex;
    }
}

但是,当此请求传递到Controller时,请求正文为null。

以前可以在Core2.x中完成

request.EnableRewind();

我的理解是,现在已替换为

httpContext.Request.EnableBuffering();

但是,即使使用

httpContext.Request.EnableBuffering();

读取请求正文后,请求正文仍为null。

我该如何解决?

1 个答案:

答案 0 :(得分:2)

它是github上的已知issue

临时的解决方法是在调用EnableBuffering之后立即拉出主体,然后将流倒回至0而不进行处理:

public class RequestLoggingHandler : AuthorizationHandler<RequestLoggingRequirement>
{
    private readonly IHttpContextAccessor _httpContextAccessor;
    public RequestLoggingHandler(IHttpContextAccessor httpContextAccessor)
    {
        _httpContextAccessor = httpContextAccessor;
    }
    protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, RequestLoggingRequirement requirement)
    {
        try
        {
            var httpContext = _httpContextAccessor.HttpContext;
            var request = httpContext.Request;
            request.EnableBuffering();

            httpContext.Items["requestId"] = SaveRequest(request);
            context.Succeed(requirement);

            return Task.CompletedTask;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    private int SaveRequest(HttpRequest request)
    {
        try
        {
            // Allows using several time the stream in ASP.Net Core
            var buffer = new byte[Convert.ToInt32(request.ContentLength)];
            request.Body.ReadAsync(buffer, 0, buffer.Length);
            var requestContent = Encoding.UTF8.GetString(buffer);
            var requestId = _repository.SaveRawHandlerRequest($"{request.Scheme} {request.Host}{request.Path} {request.QueryString} {requestContent}");

            request.Body.Position = 0;//rewinding the stream to 0
            return requestId;
        }
        catch (Exception ex)
        {

            throw ex;
        }
    }
}