-
Notifications
You must be signed in to change notification settings - Fork 526
Can't reset the position of FrameRequestStream #1113
Comments
This requires fully buffering the request. Kestrel will not do this for you, but there are some helpers available. https://github.com/aspnet/HttpAbstractions/blob/3e69df87f89601a5ede32c6765b736ab922b8fee/src/Microsoft.AspNetCore.Http/Internal/BufferingHelper.cs#L41 |
That helper is "Internal" though. |
Calling 'EnableRewind' method will change type of the 'Body' and it may affect the next execution of the middleware. |
body.Read() will throw exception because the body has been disposed. |
You're hitting a different bug in kestrel that was fixed in 1.0.1 #1028. Also, avoid synchronous reads on the body in general. |
@563203132 you should upgrade to the released 1.0.1 patch https://blogs.msdn.microsoft.com/webdev/2016/09/13/asp-net-core-sept-2016-patch/ |
With 1.0.0 you have to restore the body at end to work around the bug: var prebody = request.Body;
request.EnableRewind();
var body = request.Body;
if (request.ContentLength != null && request.ContentLength > 0)
{
byte[] buffer = new byte[request.ContentLength.Value];
await body.ReadAsync(buffer, 0, (int)request.ContentLength.Value);
}
// Add this
request.Body = prebody; |
You might also want to consider just wrapping the request body and logging data as it's read rather than eagerly reading the thing into a |
@benaadams I try your code and the 'body.ReadAsync' also throw exception because the body was disposed after calling the 'EnableRewind' method. I upgrade to 1.0.1 and it's work. @davidfowl I need to log the request at the begin of pipeline route. Thank you very much. |
Beware of the memory concerns listed before. You don't want logging code to destroy your server performance. |
I write a middleware to log the http request body, but after reading from Request.Body(FrameRequestStream), I can't read it again in other middleware.
Because the position of Body stream has been set to end.
Any ways to make me read it many times?
The text was updated successfully, but these errors were encountered: