diff --git a/src/Hosting/TestHost/src/HttpContextBuilder.cs b/src/Hosting/TestHost/src/HttpContextBuilder.cs
index 287e4aacf7b2..de3ec58709b8 100644
--- a/src/Hosting/TestHost/src/HttpContextBuilder.cs
+++ b/src/Hosting/TestHost/src/HttpContextBuilder.cs
@@ -127,6 +127,9 @@ internal void AbortRequest()
internal async Task CompleteResponseAsync()
{
+ // Flush any pending content in writer
+ await _httpContext.Response.BodyWriter.FlushAsync();
+
_pipelineFinished = true;
await ReturnResponseMessageAsync();
_responseStream.CompleteWrites();
diff --git a/src/Hosting/TestHost/test/ClientHandlerTests.cs b/src/Hosting/TestHost/test/ClientHandlerTests.cs
index 75399284602c..5a6b6b6de2ef 100644
--- a/src/Hosting/TestHost/test/ClientHandlerTests.cs
+++ b/src/Hosting/TestHost/test/ClientHandlerTests.cs
@@ -5,6 +5,7 @@
using System.IO;
using System.Linq;
using System.Net.Http;
+using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
@@ -267,6 +268,24 @@ public async Task HeadersAvailableBeforeBodyFinished()
Assert.Equal("BodyStarted,BodyFinished", await response.Content.ReadAsStringAsync());
}
+ [Fact]
+ public async Task PendingContentInWriterIsSent()
+ {
+ var handler = new ClientHandler(PathString.Empty, new DummyApplication(context =>
+ {
+ var data = Encoding.UTF8.GetBytes("Hello world");
+
+ var span = context.Response.BodyWriter.GetSpan(data.Length);
+ data.CopyTo(span);
+ context.Response.BodyWriter.Advance(data.Length);
+
+ return Task.CompletedTask;
+ }));
+ var httpClient = new HttpClient(handler);
+ HttpResponseMessage response = await httpClient.GetAsync("https://example.com/");
+ Assert.Equal("Hello world", await response.Content.ReadAsStringAsync());
+ }
+
[Fact]
public async Task FlushSendsHeaders()
{
diff --git a/src/Hosting/WindowsServices/src/Microsoft.AspNetCore.Hosting.WindowsServices.csproj b/src/Hosting/WindowsServices/src/Microsoft.AspNetCore.Hosting.WindowsServices.csproj
index 00037213e0a7..3ea91fde9f82 100644
--- a/src/Hosting/WindowsServices/src/Microsoft.AspNetCore.Hosting.WindowsServices.csproj
+++ b/src/Hosting/WindowsServices/src/Microsoft.AspNetCore.Hosting.WindowsServices.csproj
@@ -16,9 +16,7 @@
-
- Component
-
+