Skip to content

Commit dd0a6e6

Browse files
committed
Ensure PipeWriter is flushed
Fixes #461 Related Work Items: #4
1 parent e2fc2b8 commit dd0a6e6

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

src/Microsoft.AspNetCore.SystemWebAdapters.CoreServices/Features/HttpResponseAdapterFeature.cs

+27-1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ bool IHttpResponseBufferingFeature.IsEnabled
9191

9292
private async ValueTask FlushInternalAsync()
9393
{
94+
if (_pipeWriter is { })
95+
{
96+
await _pipeWriter.FlushAsync();
97+
}
98+
9499
if (_state is StreamState.Buffering && _bufferedStream is not null && !SuppressContent)
95100
{
96101
if (_filter is { } filter)
@@ -111,7 +116,23 @@ private async ValueTask FlushInternalAsync()
111116

112117
Stream IHttpResponseBodyFeature.Stream => this;
113118

114-
PipeWriter IHttpResponseBodyFeature.Writer => _pipeWriter ??= PipeWriter.Create(this, new StreamPipeWriterOptions(leaveOpen: true));
119+
PipeWriter IHttpResponseBodyFeature.Writer
120+
{
121+
get
122+
{
123+
if (_pipeWriter is null)
124+
{
125+
_pipeWriter = PipeWriter.Create(this, new StreamPipeWriterOptions(leaveOpen: true));
126+
127+
if (_state is StreamState.Complete)
128+
{
129+
_pipeWriter.Complete();
130+
}
131+
}
132+
133+
return _pipeWriter;
134+
}
135+
}
115136

116137
public bool SuppressContent
117138
{
@@ -229,6 +250,11 @@ private async Task CompleteAsync()
229250

230251
_state = StreamState.Complete;
231252

253+
if (_pipeWriter is { })
254+
{
255+
await _pipeWriter.CompleteAsync();
256+
}
257+
232258
await _responseBodyFeature.CompleteAsync();
233259
}
234260

test/Microsoft.AspNetCore.SystemWebAdapters.CoreServices.Tests/ResponseStreamTests.cs

+14
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System;
5+
using System.Buffers;
56
using System.Collections.Generic;
67
using System.IO;
78
using System.Text;
@@ -42,6 +43,19 @@ public async Task BufferedOutputIsFlushed()
4243
Assert.Equal(ContentValue, result);
4344
}
4445

46+
[Fact]
47+
public async Task OutputPipeIsFlushed()
48+
{
49+
const string Message = "hello world from pipe!";
50+
51+
var result = await RunAsync(context =>
52+
{
53+
context.AsAspNetCore().Response.BodyWriter.Write(Encoding.UTF8.GetBytes(Message));
54+
});
55+
56+
Assert.Equal(Message, result);
57+
}
58+
4559
[Fact]
4660
public async Task BufferedOutputIsFlushedOnceWithStart()
4761
{

0 commit comments

Comments
 (0)