Skip to content

Commit 7c36421

Browse files
committed
Use GetAsTask
1 parent a082669 commit 7c36421

File tree

1 file changed

+20
-16
lines changed
  • src/Servers/Kestrel/samples/PlaintextApp

1 file changed

+20
-16
lines changed

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

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.IO;
66
using System.IO.Pipelines;
77
using System.Net;
8+
using System.Runtime.CompilerServices;
89
using System.Text;
910
using System.Threading.Tasks;
1011
using Microsoft.AspNetCore.Builder;
@@ -31,22 +32,7 @@ private static Task Plaintext(HttpContext httpContext)
3132
response.ContentType = "text/plain";
3233
response.ContentLength = payload.Length;
3334

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();
5036
}
5137

5238
public static Task Main(string[] args)
@@ -63,4 +49,22 @@ public static Task Main(string[] args)
6349
return host.RunAsync();
6450
}
6551
}
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+
}
6670
}

0 commit comments

Comments
 (0)