Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

context.Response.Body read stream .net

public class IOMiddleware
{
    private readonly RequestDelegate _next;

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

    public async Task Invoke(HttpContext context)
    {
        await LogRequest(context.Request);

        await LogResponseAndInvokeNext(context);
    }

    private async Task LogRequest(HttpRequest request)
    {
        using (var bodyReader = new StreamReader(request.Body))
        {
            string body = await bodyReader.ReadToEndAsync();

            request.Body = new MemoryStream(Encoding.UTF8.GetBytes(body));
            System.Diagnostics.Debug.Print(body);
        }
    }

    private async Task LogResponseAndInvokeNext(HttpContext context)
    {
        using (var buffer = new MemoryStream())
        {
            //replace the context response with our buffer
            var stream = context.Response.Body;
            context.Response.Body = buffer;

            //invoke the rest of the pipeline
            await _next.Invoke(context);

            //reset the buffer and read out the contents
            buffer.Seek(0, SeekOrigin.Begin);
            var reader = new StreamReader(buffer);
            using (var bufferReader = new StreamReader(buffer))
            {
                string body = await bufferReader.ReadToEndAsync();

                //reset to start of stream
                buffer.Seek(0, SeekOrigin.Begin);

                //copy our content to the original stream and put it back
                await buffer.CopyToAsync(stream);
                context.Response.Body = stream;

                System.Diagnostics.Debug.Print($"Response: {body}");

            }
        }
    }
}
Comment

context.Response.Body read stream .net

public async Task Invoke(HttpContext context)
    {
        //Workaround - copy original Stream
        var initalBody = context.Request.Body;

        using (var bodyReader = new StreamReader(request.Body))
        {
            string body = await bodyReader.ReadToEndAsync();
            //Do something with body
            //Replace write only request body with read/write memorystream so you can read from it later

               request.Body = new MemoryStream(Encoding.UTF8.GetBytes(body));

        //handle other middlewares
        await _next.Invoke(context);

        //Workaround - return back to original Stream
        context.Request.Body = initalBody;
    }
Comment

PREVIOUS NEXT
Code Example
Csharp :: how to change text in richtextbox wpf 
Csharp :: how to update model in entity framework db first approach 
Csharp :: C# The request was aborted: Could not create SSL/TLS secure 
Csharp :: c# convertir caracter con tilde 
Csharp :: run in new thread C# 
Csharp :: read json from assets c# 
Csharp :: unitry raycast 
Csharp :: monegame deltatime 
Csharp :: c# chance of 
Csharp :: async where linq 
Csharp :: C# scrape html document 
Csharp :: unity DOScale 
Csharp :: unity rigidbody freeze all rotation 
Csharp :: unity collision.impulse 
Csharp :: int to char c# 
Csharp :: v-slot 
Csharp :: dictionary all key where value c# 
Csharp :: c# on variable change property get set 
Csharp :: or operator in shell 
Csharp :: c# how to get a file path from user 
Csharp :: query parameters sending to controller action asp.net core 
Csharp :: generate a dropdown list from array data using razor .net mvc 
Csharp :: check if list contains any empty element in c# 
Csharp :: .net core login redirect loop 
Csharp :: instantiate date time variable C# 
Csharp :: unity play animation on click 
Csharp :: set background from C# wpf 
Csharp :: c# C# read text from a certain line number from string 
Csharp :: unity action 
Csharp :: what dotnet command does 
ADD CONTENT
Topic
Content
Source link
Name
4+6 =