2
2
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3
3
4
4
using System . IO ;
5
+ using System . IO . Pipelines ;
5
6
using System . Net ;
7
+ using System . Runtime . CompilerServices ;
6
8
using System . Text ;
7
9
using System . Threading . Tasks ;
8
10
using Microsoft . AspNetCore . Builder ;
@@ -18,13 +20,14 @@ public void Configure(IApplicationBuilder app)
18
20
{
19
21
app . Run ( ( httpContext ) =>
20
22
{
23
+ var payload = _helloWorldBytes ;
21
24
var response = httpContext . Response ;
25
+
22
26
response . StatusCode = 200 ;
23
27
response . ContentType = "text/plain" ;
28
+ response . ContentLength = payload . Length ;
24
29
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 ( ) ;
28
31
} ) ;
29
32
}
30
33
@@ -42,4 +45,22 @@ public static Task Main(string[] args)
42
45
return host . RunAsync ( ) ;
43
46
}
44
47
}
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
+ }
45
66
}
0 commit comments