Skip to content

Use Pipelines on HttpContext #614

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/Benchmarks/Middleware/PlaintextMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.IO.Pipelines;
using System.Text;
using System.Threading.Tasks;
using Benchmarks.Configuration;
Expand Down Expand Up @@ -32,6 +33,33 @@ public Task Invoke(HttpContext httpContext)
return _next(httpContext);
}

#if NETCOREAPP3_0
public static Task WriteResponse(HttpResponse response)
{
var payloadLength = _helloWorldPayload.Length;
response.StatusCode = 200;
response.ContentType = "text/plain";
response.ContentLength = payloadLength;

var vt = response.BodyPipe.WriteAsync(_helloWorldPayload);

if (vt.IsCompletedSuccessfully)
{
// Signal consumption to the IValueTaskSource
vt.GetAwaiter().GetResult();
return Task.CompletedTask;
}
else
{
return AwaitResult(vt);
}

async Task AwaitResult(ValueTask<FlushResult> flushResult)
{
await flushResult;
}
}
#else
public static Task WriteResponse(HttpResponse response)
{
var payloadLength = _helloWorldPayload.Length;
Expand All @@ -40,6 +68,7 @@ public static Task WriteResponse(HttpResponse response)
response.ContentLength = payloadLength;
return response.Body.WriteAsync(_helloWorldPayload, 0, payloadLength);
}
#endif
}

public static class PlaintextMiddlewareExtensions
Expand Down