Skip to content
This repository was archived by the owner on Oct 11, 2023. It is now read-only.

Commit 02bd4e8

Browse files
committed
RequestLoggingMiddleware is not aware certain HTTP response can not carry body.
Setting body on HTTP status such as 204 will cause exception. This repros today already by going through a bike renting flow and examine the logs of Gateway container. If running under a debugger, the exception will fail the request. Fix the issue by skipping setting HTTP body for 204, 205 and 304. This is what asp.net core checks in AspNetCore\src\Servers\Kestrel\Core\src\Internal\Http\ HttpProtocol.cs.
1 parent 8bf2ae3 commit 02bd4e8

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

samples/BikeSharingApp/Gateway/Middleware/RequestLoggingMiddleware.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,18 @@ public async Task Invoke(HttpContext context)
3030
await this._next(context);
3131

3232
context.Response.Body = responseStream;
33-
memStream.Seek(0, SeekOrigin.Begin);
34-
using (StreamReader reader = new StreamReader(memStream))
33+
// Writing to response body is not supported for 204, 205 and 304 responses.
34+
if (context.Response.StatusCode != 204 /* NoContent */ &&
35+
context.Response.StatusCode != 205 /* ResetContent */ &&
36+
context.Response.StatusCode != 304 /* NotModified */)
3537
{
36-
responseBody = await reader.ReadToEndAsync();
38+
memStream.Seek(0, SeekOrigin.Begin);
39+
using (StreamReader reader = new StreamReader(memStream))
40+
{
41+
responseBody = await reader.ReadToEndAsync();
42+
}
43+
await context.Response.WriteAsync(responseBody);
3744
}
38-
await context.Response.WriteAsync(responseBody);
3945
}
4046

4147
if (context.Response.StatusCode >= 500)

0 commit comments

Comments
 (0)