Skip to content

Commit a082669

Browse files
committed
Feedback
1 parent 3b1dcfb commit a082669

File tree

1 file changed

+26
-15
lines changed
  • src/Servers/Kestrel/samples/PlaintextApp

1 file changed

+26
-15
lines changed

src/Servers/Kestrel/samples/PlaintextApp/Startup.cs

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Threading.Tasks;
1010
using Microsoft.AspNetCore.Builder;
1111
using Microsoft.AspNetCore.Hosting;
12+
using Microsoft.AspNetCore.Http;
1213

1314
namespace PlaintextApp
1415
{
@@ -18,24 +19,34 @@ public class Startup
1819

1920
public void Configure(IApplicationBuilder app)
2021
{
21-
app.Run(async (httpContext) =>
22-
{
23-
var response = httpContext.Response;
24-
response.StatusCode = 200;
25-
response.ContentType = "text/plain";
26-
response.ContentLength = _helloWorldBytes.Length;
27-
28-
var pipe = response.BodyPipe;
29-
Write(pipe, _helloWorldBytes);
30-
await pipe.FlushAsync();
31-
});
22+
app.Run(Plaintext);
3223
}
3324

34-
private static void Write(PipeWriter pipe, byte[] payload)
25+
private static Task Plaintext(HttpContext httpContext)
3526
{
36-
var span = pipe.GetSpan(sizeHint: payload.Length);
37-
payload.CopyTo(span);
38-
pipe.Advance(payload.Length);
27+
var payload = _helloWorldBytes;
28+
var response = httpContext.Response;
29+
30+
response.StatusCode = 200;
31+
response.ContentType = "text/plain";
32+
response.ContentLength = payload.Length;
33+
34+
var vt = response.BodyPipe.WriteAsync(payload);
35+
if (vt.IsCompletedSuccessfully)
36+
{
37+
// Signal consumption to the IValueTaskSource
38+
vt.GetAwaiter().GetResult();
39+
return Task.CompletedTask;
40+
}
41+
else
42+
{
43+
return AwaitResult(vt);
44+
}
45+
46+
async Task AwaitResult(ValueTask<FlushResult> flushResult)
47+
{
48+
await flushResult;
49+
}
3950
}
4051

4152
public static Task Main(string[] args)

0 commit comments

Comments
 (0)