Skip to content

Commit 44b2768

Browse files
benaadamsanalogrelay
authored andcommitted
Use Pipelines in PlaintextApp sample (#7227)
1 parent 0f22452 commit 44b2768

File tree

1 file changed

+24
-3
lines changed
  • src/Servers/Kestrel/samples/PlaintextApp

1 file changed

+24
-3
lines changed

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

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System.IO;
5+
using System.IO.Pipelines;
56
using System.Net;
7+
using System.Runtime.CompilerServices;
68
using System.Text;
79
using System.Threading.Tasks;
810
using Microsoft.AspNetCore.Builder;
@@ -18,13 +20,14 @@ public void Configure(IApplicationBuilder app)
1820
{
1921
app.Run((httpContext) =>
2022
{
23+
var payload = _helloWorldBytes;
2124
var response = httpContext.Response;
25+
2226
response.StatusCode = 200;
2327
response.ContentType = "text/plain";
28+
response.ContentLength = payload.Length;
2429

25-
var helloWorld = _helloWorldBytes;
26-
response.ContentLength = helloWorld.Length;
27-
return response.Body.WriteAsync(helloWorld, 0, helloWorld.Length);
30+
return response.BodyPipe.WriteAsync(payload).GetAsTask();
2831
});
2932
}
3033

@@ -42,4 +45,22 @@ public static Task Main(string[] args)
4245
return host.RunAsync();
4346
}
4447
}
48+
49+
internal static class ValueTaskExtensions
50+
{
51+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
52+
public static Task GetAsTask(this in ValueTask<FlushResult> valueTask)
53+
{
54+
if (valueTask.IsCompletedSuccessfully)
55+
{
56+
// Signal consumption to the IValueTaskSource
57+
valueTask.GetAwaiter().GetResult();
58+
return Task.CompletedTask;
59+
}
60+
else
61+
{
62+
return valueTask.AsTask();
63+
}
64+
}
65+
}
4566
}

0 commit comments

Comments
 (0)