From 0a922f7a558bcc44b2d976a4df4c0299b102769e Mon Sep 17 00:00:00 2001 From: cKey <35512826+colinin@users.noreply.github.com> Date: Fri, 26 Nov 2021 23:21:41 +0800 Subject: [PATCH] =?UTF-8?q?fix(wrapper):=20=E5=BC=82=E5=B8=B8=E5=8C=85?= =?UTF-8?q?=E8=A3=85=E5=99=A8=E5=BA=94=E5=9C=A8=E5=A4=84=E7=90=86=E7=BB=93?= =?UTF-8?q?=E6=9E=9C=E6=97=B6=E5=88=A4=E6=96=AD=E6=98=AF=E5=90=A6=E9=9C=80?= =?UTF-8?q?=E8=A6=81=E5=8C=85=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Mvc/Wrapper/AbpAspNetCoreMvcWrapperModule.cs | 3 +++ .../AbpExceptionPageWrapResultFilter.cs | 9 --------- .../AbpExceptionWrapResultFilter.cs | 12 ++++-------- .../AspNetCore/Mvc/Wrapper/WrapResultChecker.cs | 8 +++++--- .../AspNetCore/Mvc/AbpAspNetCoreMvcTestModule.cs | 2 ++ .../Mvc/Results/WrapResultController.cs | 16 ++++++++++++++++ .../Mvc/Results/WrapResultController_Tests.cs | 8 ++++++++ 7 files changed, 38 insertions(+), 20 deletions(-) diff --git a/aspnet-core/modules/mvc/LINGYUN.Abp.AspNetCore.Mvc.Wrapper/LINGYUN/Abp/AspNetCore/Mvc/Wrapper/AbpAspNetCoreMvcWrapperModule.cs b/aspnet-core/modules/mvc/LINGYUN.Abp.AspNetCore.Mvc.Wrapper/LINGYUN/Abp/AspNetCore/Mvc/Wrapper/AbpAspNetCoreMvcWrapperModule.cs index 7dad941c5..0283ffba6 100644 --- a/aspnet-core/modules/mvc/LINGYUN.Abp.AspNetCore.Mvc.Wrapper/LINGYUN/Abp/AspNetCore/Mvc/Wrapper/AbpAspNetCoreMvcWrapperModule.cs +++ b/aspnet-core/modules/mvc/LINGYUN.Abp.AspNetCore.Mvc.Wrapper/LINGYUN/Abp/AspNetCore/Mvc/Wrapper/AbpAspNetCoreMvcWrapperModule.cs @@ -8,6 +8,7 @@ using Volo.Abp.AspNetCore.Mvc; using Volo.Abp.AspNetCore.Mvc.ApiExploring; using Volo.Abp.AspNetCore.Mvc.ApplicationConfigurations; using Volo.Abp.AspNetCore.Mvc.ProxyScripting; +using Volo.Abp.Content; using Volo.Abp.Http.Modeling; using Volo.Abp.Localization; using Volo.Abp.Modularity; @@ -47,6 +48,8 @@ namespace LINGYUN.Abp.AspNetCore.Mvc.Wrapper options.IgnoreReturnTypes.Add(); // api/abp/application-configuration options.IgnoreReturnTypes.Add(); + // + options.IgnoreReturnTypes.Add(); // Abp/ServiceProxyScript options.IgnoreControllers.Add(); diff --git a/aspnet-core/modules/mvc/LINGYUN.Abp.AspNetCore.Mvc.Wrapper/LINGYUN/Abp/AspNetCore/Mvc/Wrapper/ExceptionHandling/AbpExceptionPageWrapResultFilter.cs b/aspnet-core/modules/mvc/LINGYUN.Abp.AspNetCore.Mvc.Wrapper/LINGYUN/Abp/AspNetCore/Mvc/Wrapper/ExceptionHandling/AbpExceptionPageWrapResultFilter.cs index 4df387198..6a62f7589 100644 --- a/aspnet-core/modules/mvc/LINGYUN.Abp.AspNetCore.Mvc.Wrapper/LINGYUN/Abp/AspNetCore/Mvc/Wrapper/ExceptionHandling/AbpExceptionPageWrapResultFilter.cs +++ b/aspnet-core/modules/mvc/LINGYUN.Abp.AspNetCore.Mvc.Wrapper/LINGYUN/Abp/AspNetCore/Mvc/Wrapper/ExceptionHandling/AbpExceptionPageWrapResultFilter.cs @@ -21,15 +21,6 @@ namespace LINGYUN.Abp.AspNetCore.Mvc.Wrapper.ExceptionHandling [ExposeServices(typeof(AbpExceptionPageFilter))] public class AbpExceptionPageWrapResultFilter: AbpExceptionPageFilter, ITransientDependency { - protected override bool ShouldHandleException(PageHandlerExecutingContext context) - { - if (context.ActionDescriptor.CanWarpRsult()) - { - return true; - } - return base.ShouldHandleException(context); - } - protected override async Task HandleAndWrapException(PageHandlerExecutedContext context) { var wrapResultChecker = context.GetRequiredService(); diff --git a/aspnet-core/modules/mvc/LINGYUN.Abp.AspNetCore.Mvc.Wrapper/LINGYUN/Abp/AspNetCore/Mvc/Wrapper/ExceptionHandling/AbpExceptionWrapResultFilter.cs b/aspnet-core/modules/mvc/LINGYUN.Abp.AspNetCore.Mvc.Wrapper/LINGYUN/Abp/AspNetCore/Mvc/Wrapper/ExceptionHandling/AbpExceptionWrapResultFilter.cs index 96c13e746..e5bf849d8 100644 --- a/aspnet-core/modules/mvc/LINGYUN.Abp.AspNetCore.Mvc.Wrapper/LINGYUN/Abp/AspNetCore/Mvc/Wrapper/ExceptionHandling/AbpExceptionWrapResultFilter.cs +++ b/aspnet-core/modules/mvc/LINGYUN.Abp.AspNetCore.Mvc.Wrapper/LINGYUN/Abp/AspNetCore/Mvc/Wrapper/ExceptionHandling/AbpExceptionWrapResultFilter.cs @@ -22,19 +22,15 @@ namespace LINGYUN.Abp.AspNetCore.Mvc.Wrapper.ExceptionHandling [ExposeServices(typeof(AbpExceptionFilter))] public class AbpExceptionWrapResultFilter : AbpExceptionFilter, ITransientDependency { - protected override bool ShouldHandleException(ExceptionContext context) + protected override async Task HandleAndWrapException(ExceptionContext context) { var wrapResultChecker = context.GetRequiredService(); - if (wrapResultChecker.WrapOnException(context)) + if (!wrapResultChecker.WrapOnException(context)) { - return true; + await base.HandleAndWrapException(context); + return; } - return base.ShouldHandleException(context); - } - - protected override async Task HandleAndWrapException(ExceptionContext context) - { //TODO: Trigger an AbpExceptionHandled event or something like that. var wrapResultOptions = context.GetRequiredService>().Value; var exceptionHandlingOptions = context.GetRequiredService>().Value; diff --git a/aspnet-core/modules/mvc/LINGYUN.Abp.AspNetCore.Mvc.Wrapper/LINGYUN/Abp/AspNetCore/Mvc/Wrapper/WrapResultChecker.cs b/aspnet-core/modules/mvc/LINGYUN.Abp.AspNetCore.Mvc.Wrapper/LINGYUN/Abp/AspNetCore/Mvc/Wrapper/WrapResultChecker.cs index 239a28657..e6861de37 100644 --- a/aspnet-core/modules/mvc/LINGYUN.Abp.AspNetCore.Mvc.Wrapper/LINGYUN/Abp/AspNetCore/Mvc/Wrapper/WrapResultChecker.cs +++ b/aspnet-core/modules/mvc/LINGYUN.Abp.AspNetCore.Mvc.Wrapper/LINGYUN/Abp/AspNetCore/Mvc/Wrapper/WrapResultChecker.cs @@ -9,6 +9,7 @@ using System.Linq; using System.Reflection; using Volo.Abp.DependencyInjection; using Volo.Abp.Http; +using Volo.Abp.Threading; namespace LINGYUN.Abp.AspNetCore.Mvc.Wrapper { @@ -126,13 +127,14 @@ namespace LINGYUN.Abp.AspNetCore.Mvc.Wrapper protected virtual bool CheckForReturnType(ControllerActionDescriptor controllerActionDescriptor) { - if (controllerActionDescriptor.MethodInfo.ReturnType.IsDefined(typeof(IgnoreWrapResultAttribute), true)) + var returnType = AsyncHelper.UnwrapTask(controllerActionDescriptor.MethodInfo.ReturnType); + + if (returnType.IsDefined(typeof(IgnoreWrapResultAttribute), true)) { return false; } - return !Options.IgnoreReturnTypes.Any(type => - controllerActionDescriptor.MethodInfo.ReturnType.IsAssignableFrom(type)); + return !Options.IgnoreReturnTypes.Any(type => returnType.IsAssignableFrom(type)); } protected virtual bool CheckForException(Exception exception) diff --git a/aspnet-core/tests/LINGYUN.Abp.AspNetCore.Mvc.Tests/LINGYUN/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcTestModule.cs b/aspnet-core/tests/LINGYUN.Abp.AspNetCore.Mvc.Tests/LINGYUN/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcTestModule.cs index 5653cc27f..27229a7a9 100644 --- a/aspnet-core/tests/LINGYUN.Abp.AspNetCore.Mvc.Tests/LINGYUN/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcTestModule.cs +++ b/aspnet-core/tests/LINGYUN.Abp.AspNetCore.Mvc.Tests/LINGYUN/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcTestModule.cs @@ -15,6 +15,7 @@ using Volo.Abp.AspNetCore.Mvc.Localization; using Volo.Abp.AspNetCore.Security.Claims; using Volo.Abp.AspNetCore.TestBase; using Volo.Abp.Autofac; +using Volo.Abp.Content; using Volo.Abp.GlobalFeatures; using Volo.Abp.Localization; using Volo.Abp.Localization.ExceptionHandling; @@ -84,6 +85,7 @@ namespace LINGYUN.Abp.AspNetCore.Mvc options.IgnoreControllers.Add(); // api/abp/application-configuration options.IgnoreReturnTypes.Add(); + options.IgnoreReturnTypes.Add(); options.IgnorePrefixUrls.Add("/api/dont/wrap-result-test"); diff --git a/aspnet-core/tests/LINGYUN.Abp.AspNetCore.Mvc.Tests/LINGYUN/Abp/AspNetCore/Mvc/Results/WrapResultController.cs b/aspnet-core/tests/LINGYUN.Abp.AspNetCore.Mvc.Tests/LINGYUN/Abp/AspNetCore/Mvc/Results/WrapResultController.cs index 62eb03c99..5412fb329 100644 --- a/aspnet-core/tests/LINGYUN.Abp.AspNetCore.Mvc.Tests/LINGYUN/Abp/AspNetCore/Mvc/Results/WrapResultController.cs +++ b/aspnet-core/tests/LINGYUN.Abp.AspNetCore.Mvc.Tests/LINGYUN/Abp/AspNetCore/Mvc/Results/WrapResultController.cs @@ -4,8 +4,12 @@ using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Generic; using System.Data.Common; +using System.IO; +using System.Text; +using System.Threading.Tasks; using Volo.Abp; using Volo.Abp.AspNetCore.Mvc; +using Volo.Abp.Content; namespace LINGYUN.Abp.AspNetCore.Mvc.Results { @@ -17,6 +21,18 @@ namespace LINGYUN.Abp.AspNetCore.Mvc.Results LocalizationResource = typeof(MvcTestResource); } + [HttpGet] + [Route("get-text")] + public async Task DontWrapRemoteStreamContext() + { + var textBytes = Encoding.UTF8.GetBytes("text"); + + var memoryStream = new MemoryStream(); + await memoryStream.WriteAsync(textBytes); + + return new RemoteStreamContent(memoryStream); + } + [HttpGet] [Route("exception")] public TestResultObject WrapBusinessException() diff --git a/aspnet-core/tests/LINGYUN.Abp.AspNetCore.Mvc.Tests/LINGYUN/Abp/AspNetCore/Mvc/Results/WrapResultController_Tests.cs b/aspnet-core/tests/LINGYUN.Abp.AspNetCore.Mvc.Tests/LINGYUN/Abp/AspNetCore/Mvc/Results/WrapResultController_Tests.cs index b33924693..165fa5b57 100644 --- a/aspnet-core/tests/LINGYUN.Abp.AspNetCore.Mvc.Tests/LINGYUN/Abp/AspNetCore/Mvc/Results/WrapResultController_Tests.cs +++ b/aspnet-core/tests/LINGYUN.Abp.AspNetCore.Mvc.Tests/LINGYUN/Abp/AspNetCore/Mvc/Results/WrapResultController_Tests.cs @@ -11,6 +11,14 @@ namespace LINGYUN.Abp.AspNetCore.Mvc.Results { public class WrapResultController_Tests : AbpAspNetCoreMvcTestBase { + [Fact] + public async Task Should_Return_Not_Wrap_Result_For_Abp_Remote_Stream_Content() + { + var result = await GetResponseAsStringAsync("/api/wrap-result-test/get-text"); + result.ShouldNotBeNull(); + result.ShouldBe("text"); + } + [Fact] public async Task Should_Return_Not_Wrap_Result_For_Abp_Api_Definition() {