You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Dec 19, 2018. It is now read-only.
First of all, I do not know where I should be asking his question. If this is not the right project, please let me know. I have a middleware like this that writes the request body out.
app.Use(next => async context =>
{
var request = context.Request;
var buffer = new MemoryStream();
await request.Body.CopyToAsync(buffer);
request.Body = buffer;
string body = System.Text.Encoding.UTF8.GetString(buffer.ToArray());
buffer.Position = 0;
await next.Invoke(context);
});
This works correctly for the first request. For the subsequent request, request.Body continues to be a MemoryStream and the request body of the second message is not being copied into buffer, since request.body is not the network stream.
If I change the code like this, it works.
app.Use(next => async context =>
{
var request = context.Request;
var buffer = new MemoryStream();
var originalStream = request.Body;
await request.Body.CopyToAsync(buffer);
request.Body = buffer;
string body = System.Text.Encoding.UTF8.GetString(buffer.ToArray());
buffer.Position = 0;
await next.Invoke(context);
request.Body = originalStream;
});
But why do I need to restore the request body stream so that it works correctly for the next request? It seems very weird, at least to me.
The text was updated successfully, but these errors were encountered:
First of all, I do not know where I should be asking his question. If this is not the right project, please let me know. I have a middleware like this that writes the request body out.
This works correctly for the first request. For the subsequent request,
request.Body
continues to be aMemoryStream
and the request body of the second message is not being copied into buffer, sincerequest.body
is not the network stream.If I change the code like this, it works.
But why do I need to restore the request body stream so that it works correctly for the next request? It seems very weird, at least to me.
The text was updated successfully, but these errors were encountered: