Skip to content

Commit 19ecf5e

Browse files
benaadamsTratcher
authored andcommitted
React to IHeaderDictionary ContentLength change
1 parent 2dfdb74 commit 19ecf5e

File tree

3 files changed

+15
-16
lines changed

3 files changed

+15
-16
lines changed

src/Benchmarks/Middleware/MemoryCachePlaintextMiddleware.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ public Task Invoke(HttpContext httpContext)
4040
}
4141
}
4242

43-
httpContext.Response.StatusCode = 200;
44-
httpContext.Response.ContentType = "text/plain";
45-
// HACK: Setting the Content-Length header manually avoids the cost of serializing the int to a string.
46-
// This is instead of: httpContext.Response.ContentLength = payload.Length;
47-
httpContext.Response.Headers["Content-Length"] = "13";
48-
return httpContext.Response.Body.WriteAsync(payload, 0, payload.Length);
43+
var payloadLength = payload.Length;
44+
var response = httpContext.Response;
45+
response.StatusCode = 200;
46+
response.ContentType = "text/plain";
47+
response.ContentLength = payloadLength;
48+
return response.Body.WriteAsync(payload, 0, payloadLength);
4949
}
5050

5151
return _next(httpContext);

src/Benchmarks/Middleware/MemoryCachePlaintextSetRemoveMiddleware.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ public Task Invoke(HttpContext httpContext)
3434
_memoryCache.Set(_key, _helloWorldPayload);
3535
_memoryCache.Remove(_key);
3636

37-
httpContext.Response.StatusCode = 200;
38-
httpContext.Response.ContentType = "text/plain";
39-
// HACK: Setting the Content-Length header manually avoids the cost of serializing the int to a string.
40-
// This is instead of: httpContext.Response.ContentLength = payload.Length;
41-
httpContext.Response.Headers["Content-Length"] = "13";
42-
return httpContext.Response.Body.WriteAsync(_helloWorldPayload, 0, _helloWorldPayload.Length);
37+
var response = httpContext.Response;
38+
var payloadLength = _helloWorldPayload.Length;
39+
response.StatusCode = 200;
40+
response.ContentType = "text/plain";
41+
response.ContentLength = payloadLength;
42+
return httpContext.Response.Body.WriteAsync(_helloWorldPayload, 0, payloadLength);
4343
}
4444

4545
return _next(httpContext);

src/Benchmarks/Middleware/PlaintextMiddleware.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,11 @@ public Task Invoke(HttpContext httpContext)
3434

3535
public static Task WriteResponse(HttpResponse response)
3636
{
37+
var payloadLength = _helloWorldPayload.Length;
3738
response.StatusCode = 200;
3839
response.ContentType = "text/plain";
39-
// HACK: Setting the Content-Length header manually avoids the cost of serializing the int to a string.
40-
// This is instead of: httpContext.Response.ContentLength = _helloWorldPayload.Length;
41-
response.Headers["Content-Length"] = "13";
42-
return response.Body.WriteAsync(_helloWorldPayload, 0, _helloWorldPayload.Length);
40+
response.ContentLength = payloadLength;
41+
return response.Body.WriteAsync(_helloWorldPayload, 0, payloadLength);
4342
}
4443
}
4544

0 commit comments

Comments
 (0)