From 7b06048593a742d44b09f22919b451b58f7750ef Mon Sep 17 00:00:00 2001 From: maliming Date: Fri, 26 May 2023 11:29:22 +0800 Subject: [PATCH] Add `AbpMemoryPoolHttpResponseStreamWriterFactory`. https://github.com/dotnet/aspnetcore/issues/40928 --- .../AspNetCore/Mvc/AbpAspNetCoreMvcModule.cs | 3 ++ ...moryPoolHttpResponseStreamWriterFactory.cs | 40 +++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Infrastructure/AbpMemoryPoolHttpResponseStreamWriterFactory.cs diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcModule.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcModule.cs index 0a12d0332a..e22fef4236 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcModule.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcModule.cs @@ -28,6 +28,7 @@ using Volo.Abp.AspNetCore.Mvc.ApiExploring; using Volo.Abp.AspNetCore.Mvc.Conventions; using Volo.Abp.AspNetCore.Mvc.DataAnnotations; using Volo.Abp.AspNetCore.Mvc.DependencyInjection; +using Volo.Abp.AspNetCore.Mvc.Infrastructure; using Volo.Abp.AspNetCore.Mvc.Json; using Volo.Abp.AspNetCore.Mvc.Localization; using Volo.Abp.AspNetCore.VirtualFileSystem; @@ -201,6 +202,8 @@ public class AbpAspNetCoreMvcModule : AbpModule { options.DisableModule("abp"); }); + + context.Services.Replace(ServiceDescriptor.Singleton()); } public override void PostConfigureServices(ServiceConfigurationContext context) diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Infrastructure/AbpMemoryPoolHttpResponseStreamWriterFactory.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Infrastructure/AbpMemoryPoolHttpResponseStreamWriterFactory.cs new file mode 100644 index 0000000000..78e13fc73e --- /dev/null +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Infrastructure/AbpMemoryPoolHttpResponseStreamWriterFactory.cs @@ -0,0 +1,40 @@ +using System; +using System.Buffers; +using System.IO; +using System.Text; +using Microsoft.AspNetCore.Mvc.Infrastructure; +using Microsoft.AspNetCore.WebUtilities; + +namespace Volo.Abp.AspNetCore.Mvc.Infrastructure; + +/// +/// https://github.com/dotnet/aspnetcore/issues/40928#issuecomment-1450063613 +/// +public class AbpMemoryPoolHttpResponseStreamWriterFactory : IHttpResponseStreamWriterFactory +{ + public const int DefaultBufferSize = 32 * 1024; + + private readonly ArrayPool _bytePool; + private readonly ArrayPool _charPool; + + public AbpMemoryPoolHttpResponseStreamWriterFactory(ArrayPool bytePool, ArrayPool charPool) + { + _bytePool = bytePool ?? throw new ArgumentNullException(nameof(bytePool)); + _charPool = charPool ?? throw new ArgumentNullException(nameof(charPool)); + } + + public TextWriter CreateWriter(Stream stream, Encoding encoding) + { + if (stream == null) + { + throw new ArgumentNullException(nameof(stream)); + } + + if (encoding == null) + { + throw new ArgumentNullException(nameof(encoding)); + } + + return new HttpResponseStreamWriter(stream, encoding, DefaultBufferSize, _bytePool, _charPool); + } +}