9
9
using System . Threading . Tasks ;
10
10
using Microsoft . AspNetCore . Builder ;
11
11
using Microsoft . AspNetCore . Hosting ;
12
+ using Microsoft . AspNetCore . Http ;
12
13
13
14
namespace PlaintextApp
14
15
{
@@ -18,24 +19,34 @@ public class Startup
18
19
19
20
public void Configure ( IApplicationBuilder app )
20
21
{
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 ) ;
32
23
}
33
24
34
- private static void Write ( PipeWriter pipe , byte [ ] payload )
25
+ private static Task Plaintext ( HttpContext httpContext )
35
26
{
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
+ }
39
50
}
40
51
41
52
public static Task Main ( string [ ] args )
0 commit comments