添加用于请求日志记录的定制中间件时,模型绑定停止工作

时间:2019-08-14 21:39:19

标签: asp.net-core asp.net-core-webapi

我正在使用以下类来记录对我的API的所有请求和响应。该代码来自链接https://exceptionnotfound.net/using-middleware-to-log-requests-and-responses-in-asp-net-core/。问题是当我注册该中间件时,我的模型绑定停止工作。该请求始终为空。我认为问题出在方法“ FormatRequest”中,如果我删除对该方法的调用,它将开始起作用,但无法弄清为什么它破坏了模型绑定过程。

public class RequestResponseLoggingMiddleware
{
    private readonly RequestDelegate _next;

    public RequestResponseLoggingMiddleware(RequestDelegate next)
    {
        _next = next;
    }

    public async Task Invoke(HttpContext context)
    {
        //First, get the incoming request
        var request = await FormatRequest(context.Request);

        //Copy a pointer to the original response body stream
        var originalBodyStream = context.Response.Body;

        //Create a new memory stream...
        using (var responseBody = new MemoryStream())
        {
            //...and use that for the temporary response body
            context.Response.Body = responseBody;

            //Continue down the Middleware pipeline, eventually returning to this class
            await _next(context);

            //Format the response from the server
            var response = await FormatResponse(context.Response);

            //TODO: Save log to chosen datastore

            //Copy the contents of the new memory stream (which contains the response) to the original stream, which is then returned to the client.
            await responseBody.CopyToAsync(originalBodyStream);
        }
    }

    private async Task<string> FormatRequest(HttpRequest request)
    {
        var body = request.Body;

        //This line allows us to set the reader for the request back at the beginning of its stream.
        request.EnableRewind();

        //We now need to read the request stream.  First, we create a new byte[] with the same length as the request stream...
        var buffer = new byte[Convert.ToInt32(request.ContentLength)];

        //...Then we copy the entire request stream into the new buffer.
        await request.Body.ReadAsync(buffer, 0, buffer.Length);

        //We convert the byte[] into a string using UTF8 encoding...
        var bodyAsText = Encoding.UTF8.GetString(buffer);

        //..and finally, assign the read body back to the request body, which is allowed because of EnableRewind()
        request.Body = body;

        return $"{request.Scheme} {request.Host}{request.Path} {request.QueryString} {bodyAsText}";
    }

    private async Task<string> FormatResponse(HttpResponse response)
    {
        //We need to read the response stream from the beginning...
        response.Body.Seek(0, SeekOrigin.Begin);

        //...and copy it into a string
        string text = await new StreamReader(response.Body).ReadToEndAsync();

        //We need to reset the reader for the response so that the client can read it.
        response.Body.Seek(0, SeekOrigin.Begin);

        //Return the string for the response, including the status code (e.g. 200, 404, 401, etc.)
        return $"{response.StatusCode}: {text}";
    }
}

这是我的注册方式,

public class Startup
{
    //...

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        //Add our new middleware to the pipeline
        app.UseMiddleware<RequestResponseLoggingMiddleware>();

        app.UseMvc();
    }
}

1 个答案:

答案 0 :(得分:2)

对于此问题,您可以尝试request.Body.Seek(0, SeekOrigin.Begin);来重设正文,而不是request.Body = body;

private async Task<string> FormatRequest(HttpRequest request)
{

    var body = request.Body;
    //This line allows us to set the reader for the request back at the beginning of its stream.
    request.EnableRewind();

    //We now need to read the request stream.  First, we create a new byte[] with the same length as the request stream...
    var buffer = new byte[Convert.ToInt32(request.ContentLength)];

    //...Then we copy the entire request stream into the new buffer.
    await request.Body.ReadAsync(buffer, 0, buffer.Length);

    //We convert the byte[] into a string using UTF8 encoding...
    var bodyAsText = Encoding.UTF8.GetString(buffer);

    //..and finally, assign the read body back to the request body, which is allowed because of EnableRewind()
    //request.Body = body;
    request.Body.Seek(0, SeekOrigin.Begin);
    return $"{request.Scheme} {request.Host}{request.Path} {request.QueryString} {bodyAsText}";
}