Use FileBufferingWriteStream in ViewComponentResultExecutor (#9523)
Fixes https://github.com/aspnet/AspNetCore/issues/9443
This commit is contained in:
parent
9e4d5c14c7
commit
acf45106fc
|
|
@ -12,6 +12,7 @@ using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||||
using Microsoft.AspNetCore.Mvc.Rendering;
|
using Microsoft.AspNetCore.Mvc.Rendering;
|
||||||
using Microsoft.AspNetCore.Mvc.ViewFeatures.Buffers;
|
using Microsoft.AspNetCore.Mvc.ViewFeatures.Buffers;
|
||||||
using Microsoft.AspNetCore.Mvc.ViewFeatures.Filters;
|
using Microsoft.AspNetCore.Mvc.ViewFeatures.Filters;
|
||||||
|
using Microsoft.AspNetCore.WebUtilities;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
|
|
@ -148,14 +149,13 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
using var memoryStream = new MemoryStream();
|
using var bufferingStream = new FileBufferingWriteStream();
|
||||||
using (var intermediateWriter = _writerFactory.CreateWriter(response.Body, resolvedContentTypeEncoding))
|
using (var intermediateWriter = _writerFactory.CreateWriter(bufferingStream, resolvedContentTypeEncoding))
|
||||||
{
|
{
|
||||||
viewComponentResult.WriteTo(intermediateWriter, _htmlEncoder);
|
viewComponentResult.WriteTo(intermediateWriter, _htmlEncoder);
|
||||||
}
|
}
|
||||||
|
|
||||||
memoryStream.Position = 0;
|
await bufferingStream.DrainBufferAsync(response.Body);
|
||||||
await memoryStream.CopyToAsync(response.Body);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -439,6 +439,48 @@ namespace Microsoft.AspNetCore.Mvc
|
||||||
Assert.Equal(expected, body);
|
Assert.Equal(expected, body);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task ExecuteResultAsync_WithCustomViewComponentHelper_ForLargeText()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var expected = new string('a', 64 * 1024 * 1024);
|
||||||
|
var methodInfo = typeof(TextViewComponent).GetMethod(nameof(TextViewComponent.Invoke));
|
||||||
|
var descriptor = new ViewComponentDescriptor()
|
||||||
|
{
|
||||||
|
FullName = "Full.Name.Text",
|
||||||
|
ShortName = "Text",
|
||||||
|
TypeInfo = typeof(TextViewComponent).GetTypeInfo(),
|
||||||
|
MethodInfo = methodInfo,
|
||||||
|
Parameters = methodInfo.GetParameters(),
|
||||||
|
};
|
||||||
|
var result = Task.FromResult<IHtmlContent>(new HtmlContentBuilder().AppendHtml(expected));
|
||||||
|
|
||||||
|
var helper = Mock.Of<IViewComponentHelper>(h => h.InvokeAsync(It.IsAny<Type>(), It.IsAny<object>()) == result);
|
||||||
|
|
||||||
|
var httpContext = new DefaultHttpContext();
|
||||||
|
var services = CreateServices(diagnosticListener: null, httpContext, new[] { descriptor });
|
||||||
|
services.AddSingleton<IViewComponentHelper>(helper);
|
||||||
|
|
||||||
|
httpContext.RequestServices = services.BuildServiceProvider();
|
||||||
|
httpContext.Response.Body = new MemoryStream();
|
||||||
|
|
||||||
|
var actionContext = new ActionContext(httpContext, new RouteData(), new ActionDescriptor());
|
||||||
|
|
||||||
|
var viewComponentResult = new ViewComponentResult()
|
||||||
|
{
|
||||||
|
Arguments = new { name = "World!" },
|
||||||
|
ViewComponentType = typeof(TextViewComponent),
|
||||||
|
TempData = _tempDataDictionary,
|
||||||
|
};
|
||||||
|
|
||||||
|
// Act
|
||||||
|
await viewComponentResult.ExecuteResultAsync(actionContext);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
var body = ReadBody(actionContext.HttpContext.Response);
|
||||||
|
Assert.Equal(expected, body);
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task ExecuteResultAsync_SetsStatusCode()
|
public async Task ExecuteResultAsync_SetsStatusCode()
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue