AnKing
AnKing

Reputation: 2174

Read Body multiple times in ASP.Net Core 3.0

I need to be able to read the body of the request for logging purposes early in a pipeline but I am unable to rewind it back.

Sample code here (thanks to https://devblogs.microsoft.com/aspnet/re-reading-asp-net-core-request-bodies-with-enablebuffering/):

public async Task InvokeAsync(HttpContext context, RequestDelegate next)
{
    context.Request.EnableBuffering();

    // Leave the body open so the next middleware can read it.
    using (var reader = new StreamReader(
        context.Request.Body,
        encoding: Encoding.UTF8,
        detectEncodingFromByteOrderMarks: false,
        bufferSize: bufferSize,
        leaveOpen: true))
    {
        var body = await reader.ReadToEndAsync();
        // Do some processing with body…

        // Reset the request body stream position so the next middleware can read it
        context.Request.Body.Position = 0; <<---- throws exception
    }

    // Call the next delegate/middleware in the pipeline
    await next(context);
}

this throws the following exception when rewinding:

System.ObjectDisposedException: 'IFeatureCollection has been disposed. Object name: 'Collection'.'

Upvotes: 2

Views: 1591

Answers (1)

Anouar
Anouar

Reputation: 2470

You will need to copy the body data in a new stream and put it back in the body:

using (var reader = new StreamReader(
        context.Request.Body,
        encoding: Encoding.UTF8,
        detectEncodingFromByteOrderMarks: false,
        bufferSize: bufferSize,
        leaveOpen: true))
{
    var body = await reader.ReadToEndAsync();
    // Do some processing with body

    // Get the body data    
    byte[] bodyData = Encoding.UTF8.GetBytes(body);

    // Put a new stream with that data in the body
    context.Request.Body = new MemoryStream(bodyData);
}

// Call the next delegate/middleware in the pipeline
await next(context);

Upvotes: 1

Related Questions