Skip to content

Commit 3b1dcfb

Browse files
committed
Use Pipelines in PlaintextApp sample
1 parent 9f202fe commit 3b1dcfb

File tree

1 file changed

+14
-4
lines changed
  • src/Servers/Kestrel/samples/PlaintextApp

1 file changed

+14
-4
lines changed

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System;
45
using System.IO;
6+
using System.IO.Pipelines;
57
using System.Net;
68
using System.Text;
79
using System.Threading.Tasks;
@@ -16,18 +18,26 @@ public class Startup
1618

1719
public void Configure(IApplicationBuilder app)
1820
{
19-
app.Run((httpContext) =>
21+
app.Run(async (httpContext) =>
2022
{
2123
var response = httpContext.Response;
2224
response.StatusCode = 200;
2325
response.ContentType = "text/plain";
26+
response.ContentLength = _helloWorldBytes.Length;
2427

25-
var helloWorld = _helloWorldBytes;
26-
response.ContentLength = helloWorld.Length;
27-
return response.Body.WriteAsync(helloWorld, 0, helloWorld.Length);
28+
var pipe = response.BodyPipe;
29+
Write(pipe, _helloWorldBytes);
30+
await pipe.FlushAsync();
2831
});
2932
}
3033

34+
private static void Write(PipeWriter pipe, byte[] payload)
35+
{
36+
var span = pipe.GetSpan(sizeHint: payload.Length);
37+
payload.CopyTo(span);
38+
pipe.Advance(payload.Length);
39+
}
40+
3141
public static Task Main(string[] args)
3242
{
3343
var host = new WebHostBuilder()

0 commit comments

Comments
 (0)