5
5
using System . IO ;
6
6
using System . IO . Pipelines ;
7
7
using System . Net ;
8
+ using System . Runtime . CompilerServices ;
8
9
using System . Text ;
9
10
using System . Threading . Tasks ;
10
11
using Microsoft . AspNetCore . Builder ;
@@ -31,22 +32,7 @@ private static Task Plaintext(HttpContext httpContext)
31
32
response . ContentType = "text/plain" ;
32
33
response . ContentLength = payload . Length ;
33
34
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
- }
35
+ return response . BodyPipe . WriteAsync ( payload ) . GetAsTask ( ) ;
50
36
}
51
37
52
38
public static Task Main ( string [ ] args )
@@ -63,4 +49,22 @@ public static Task Main(string[] args)
63
49
return host . RunAsync ( ) ;
64
50
}
65
51
}
52
+
53
+ internal static class ValueTaskExtensions
54
+ {
55
+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
56
+ public static Task GetAsTask < T > ( this in ValueTask < T > valueTask )
57
+ {
58
+ if ( valueTask . IsCompletedSuccessfully )
59
+ {
60
+ // Signal consumption to the IValueTaskSource
61
+ valueTask . GetAwaiter ( ) . GetResult ( ) ;
62
+ return Task . CompletedTask ;
63
+ }
64
+ else
65
+ {
66
+ return valueTask . AsTask ( ) ;
67
+ }
68
+ }
69
+ }
66
70
}
0 commit comments