diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Auditing/AbpAuditActionFilter.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Auditing/AbpAuditActionFilter.cs index 0ee62f9b65..2e05204304 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Auditing/AbpAuditActionFilter.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Auditing/AbpAuditActionFilter.cs @@ -3,6 +3,7 @@ using System.Diagnostics; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc.Abstractions; using Microsoft.AspNetCore.Mvc.Filters; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using Volo.Abp.Aspects; using Volo.Abp.Auditing; @@ -12,17 +13,6 @@ namespace Volo.Abp.AspNetCore.Mvc.Auditing { public class AbpAuditActionFilter : IAsyncActionFilter, ITransientDependency { - protected AbpAuditingOptions Options { get; } - private readonly IAuditingHelper _auditingHelper; - private readonly IAuditingManager _auditingManager; - - public AbpAuditActionFilter(IOptions options, IAuditingHelper auditingHelper, IAuditingManager auditingManager) - { - Options = options.Value; - _auditingHelper = auditingHelper; - _auditingManager = auditingManager; - } - public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) { if (!ShouldSaveAudit(context, out var auditLog, out var auditLogAction)) @@ -63,7 +53,8 @@ namespace Volo.Abp.AspNetCore.Mvc.Auditing auditLog = null; auditLogAction = null; - if (!Options.IsEnabled) + var options = context.HttpContext.RequestServices.GetRequiredService>().Value; + if (!options.IsEnabled) { return false; } @@ -73,19 +64,20 @@ namespace Volo.Abp.AspNetCore.Mvc.Auditing return false; } - var auditLogScope = _auditingManager.Current; + var auditLogScope = context.HttpContext.RequestServices.GetRequiredService().Current; if (auditLogScope == null) { return false; } - if (!_auditingHelper.ShouldSaveAudit(context.ActionDescriptor.GetMethodInfo(), true)) + var auditingHelper = context.HttpContext.RequestServices.GetRequiredService(); + if (!auditingHelper.ShouldSaveAudit(context.ActionDescriptor.GetMethodInfo(), true)) { return false; } auditLog = auditLogScope.Log; - auditLogAction = _auditingHelper.CreateAuditLogAction( + auditLogAction = auditingHelper.CreateAuditLogAction( auditLog, context.ActionDescriptor.AsControllerActionDescriptor().ControllerTypeInfo.AsType(), context.ActionDescriptor.AsControllerActionDescriptor().MethodInfo, diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Auditing/AbpAuditPageFilter.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Auditing/AbpAuditPageFilter.cs index 6e9996574c..70fac063b5 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Auditing/AbpAuditPageFilter.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Auditing/AbpAuditPageFilter.cs @@ -3,6 +3,7 @@ using System.Diagnostics; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc.Abstractions; using Microsoft.AspNetCore.Mvc.Filters; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using Volo.Abp.Aspects; using Volo.Abp.Auditing; @@ -12,22 +13,11 @@ namespace Volo.Abp.AspNetCore.Mvc.Auditing { public class AbpAuditPageFilter : IAsyncPageFilter, ITransientDependency { - protected AbpAuditingOptions Options { get; } - private readonly IAuditingHelper _auditingHelper; - private readonly IAuditingManager _auditingManager; - - public AbpAuditPageFilter(IOptions options, IAuditingHelper auditingHelper, IAuditingManager auditingManager) - { - Options = options.Value; - _auditingHelper = auditingHelper; - _auditingManager = auditingManager; - } - public Task OnPageHandlerSelectionAsync(PageHandlerSelectedContext context) { return Task.CompletedTask; } - + public async Task OnPageHandlerExecutionAsync(PageHandlerExecutingContext context, PageHandlerExecutionDelegate next) { if (context.HandlerMethod == null || !ShouldSaveAudit(context, out var auditLog, out var auditLogAction)) @@ -68,7 +58,8 @@ namespace Volo.Abp.AspNetCore.Mvc.Auditing auditLog = null; auditLogAction = null; - if (!Options.IsEnabled) + var options = context.HttpContext.RequestServices.GetRequiredService>().Value; + if (!options.IsEnabled) { return false; } @@ -78,19 +69,20 @@ namespace Volo.Abp.AspNetCore.Mvc.Auditing return false; } - var auditLogScope = _auditingManager.Current; + var auditLogScope = context.HttpContext.RequestServices.GetRequiredService().Current; if (auditLogScope == null) { return false; } - if (!_auditingHelper.ShouldSaveAudit(context.HandlerMethod.MethodInfo, true)) + var auditingHelper = context.HttpContext.RequestServices.GetRequiredService(); + if (!auditingHelper.ShouldSaveAudit(context.HandlerMethod.MethodInfo, true)) { return false; } auditLog = auditLogScope.Log; - auditLogAction = _auditingHelper.CreateAuditLogAction( + auditLogAction = auditingHelper.CreateAuditLogAction( auditLog, context.HandlerMethod.GetType(), context.HandlerMethod.MethodInfo, @@ -100,4 +92,4 @@ namespace Volo.Abp.AspNetCore.Mvc.Auditing return true; } } -} \ No newline at end of file +} diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionFilter.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionFilter.cs index c6f353b876..2a28d2cc0a 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionFilter.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionFilter.cs @@ -19,27 +19,6 @@ namespace Volo.Abp.AspNetCore.Mvc.ExceptionHandling { public class AbpExceptionFilter : IAsyncExceptionFilter, ITransientDependency { - public ILogger Logger { get; set; } - - private readonly IExceptionToErrorInfoConverter _errorInfoConverter; - private readonly IHttpExceptionStatusCodeFinder _statusCodeFinder; - private readonly IJsonSerializer _jsonSerializer; - private readonly AbpExceptionHandlingOptions _exceptionHandlingOptions; - - public AbpExceptionFilter( - IExceptionToErrorInfoConverter errorInfoConverter, - IHttpExceptionStatusCodeFinder statusCodeFinder, - IJsonSerializer jsonSerializer, - IOptions exceptionHandlingOptions) - { - _errorInfoConverter = errorInfoConverter; - _statusCodeFinder = statusCodeFinder; - _jsonSerializer = jsonSerializer; - _exceptionHandlingOptions = exceptionHandlingOptions.Value; - - Logger = NullLogger.Instance; - } - public async Task OnExceptionAsync(ExceptionContext context) { if (!ShouldHandleException(context)) @@ -78,9 +57,14 @@ namespace Volo.Abp.AspNetCore.Mvc.ExceptionHandling //TODO: Trigger an AbpExceptionHandled event or something like that. context.HttpContext.Response.Headers.Add(AbpHttpConsts.AbpErrorFormat, "true"); - context.HttpContext.Response.StatusCode = (int)_statusCodeFinder.GetStatusCode(context.HttpContext, context.Exception); + context.HttpContext.Response.StatusCode = (int) context.HttpContext.RequestServices + .GetRequiredService() + .GetStatusCode(context.HttpContext, context.Exception); - var remoteServiceErrorInfo = _errorInfoConverter.Convert(context.Exception, _exceptionHandlingOptions.SendExceptionsDetailsToClients); + var remoteServiceErrorInfo = context.HttpContext.RequestServices + .GetRequiredService().Convert(context.Exception, + context.HttpContext.RequestServices.GetRequiredService>() + .Value.SendExceptionsDetailsToClients); context.Result = new ObjectResult(new RemoteServiceErrorResponse(remoteServiceErrorInfo)); @@ -88,10 +72,15 @@ namespace Volo.Abp.AspNetCore.Mvc.ExceptionHandling var remoteServiceErrorInfoBuilder = new StringBuilder(); remoteServiceErrorInfoBuilder.AppendLine($"---------- {nameof(RemoteServiceErrorInfo)} ----------"); - remoteServiceErrorInfoBuilder.AppendLine( _jsonSerializer.Serialize(remoteServiceErrorInfo, indented: true)); - Logger.LogWithLevel(logLevel, remoteServiceErrorInfoBuilder.ToString()); + remoteServiceErrorInfoBuilder.AppendLine(context.HttpContext.RequestServices + .GetRequiredService().Serialize(remoteServiceErrorInfo, indented: true)); + + var logger = context.HttpContext.RequestServices.GetService>() ?? + NullLogger.Instance; + + logger.LogWithLevel(logLevel, remoteServiceErrorInfoBuilder.ToString()); - Logger.LogException(context.Exception, logLevel); + logger.LogException(context.Exception, logLevel); await context.HttpContext .RequestServices diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionPageFilter.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionPageFilter.cs index 9ef0378909..a91980043b 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionPageFilter.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionPageFilter.cs @@ -19,27 +19,6 @@ namespace Volo.Abp.AspNetCore.Mvc.ExceptionHandling { public class AbpExceptionPageFilter : IAsyncPageFilter, ITransientDependency { - public ILogger Logger { get; set; } - - private readonly IExceptionToErrorInfoConverter _errorInfoConverter; - private readonly IHttpExceptionStatusCodeFinder _statusCodeFinder; - private readonly IJsonSerializer _jsonSerializer; - private readonly AbpExceptionHandlingOptions _exceptionHandlingOptions; - - public AbpExceptionPageFilter( - IExceptionToErrorInfoConverter errorInfoConverter, - IHttpExceptionStatusCodeFinder statusCodeFinder, - IJsonSerializer jsonSerializer, - IOptions exceptionHandlingOptions) - { - _errorInfoConverter = errorInfoConverter; - _statusCodeFinder = statusCodeFinder; - _jsonSerializer = jsonSerializer; - _exceptionHandlingOptions = exceptionHandlingOptions.Value; - - Logger = NullLogger.Instance; - } - public Task OnPageHandlerSelectionAsync(PageHandlerSelectedContext context) { return Task.CompletedTask; @@ -90,9 +69,14 @@ namespace Volo.Abp.AspNetCore.Mvc.ExceptionHandling //TODO: Trigger an AbpExceptionHandled event or something like that. context.HttpContext.Response.Headers.Add(AbpHttpConsts.AbpErrorFormat, "true"); - context.HttpContext.Response.StatusCode = (int)_statusCodeFinder.GetStatusCode(context.HttpContext, context.Exception); + context.HttpContext.Response.StatusCode = (int) context.HttpContext.RequestServices + .GetRequiredService() + .GetStatusCode(context.HttpContext, context.Exception); - var remoteServiceErrorInfo = _errorInfoConverter.Convert(context.Exception, _exceptionHandlingOptions.SendExceptionsDetailsToClients); + var remoteServiceErrorInfo = context.HttpContext.RequestServices + .GetRequiredService().Convert(context.Exception, + context.HttpContext.RequestServices.GetRequiredService>() + .Value.SendExceptionsDetailsToClients); context.Result = new ObjectResult(new RemoteServiceErrorResponse(remoteServiceErrorInfo)); @@ -100,10 +84,14 @@ namespace Volo.Abp.AspNetCore.Mvc.ExceptionHandling var remoteServiceErrorInfoBuilder = new StringBuilder(); remoteServiceErrorInfoBuilder.AppendLine($"---------- {nameof(RemoteServiceErrorInfo)} ----------"); - remoteServiceErrorInfoBuilder.AppendLine( _jsonSerializer.Serialize(remoteServiceErrorInfo, indented: true)); - Logger.LogWithLevel(logLevel, remoteServiceErrorInfoBuilder.ToString()); + remoteServiceErrorInfoBuilder.AppendLine(context.HttpContext.RequestServices + .GetRequiredService().Serialize(remoteServiceErrorInfo, indented: true)); + + var logger = context.HttpContext.RequestServices.GetService>() ?? + NullLogger.Instance; + logger.LogWithLevel(logLevel, remoteServiceErrorInfoBuilder.ToString()); - Logger.LogException(context.Exception, logLevel); + logger.LogException(context.Exception, logLevel); await context.HttpContext .RequestServices diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Features/AbpFeatureActionFilter.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Features/AbpFeatureActionFilter.cs index 577acda339..82cc65ff0b 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Features/AbpFeatureActionFilter.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Features/AbpFeatureActionFilter.cs @@ -1,7 +1,7 @@ using Microsoft.AspNetCore.Mvc.Filters; -using System; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc.Abstractions; +using Microsoft.Extensions.DependencyInjection; using Volo.Abp.Aspects; using Volo.Abp.DependencyInjection; using Volo.Abp.Features; @@ -10,15 +10,8 @@ namespace Volo.Abp.AspNetCore.Mvc.Features { public class AbpFeatureActionFilter : IAsyncActionFilter, ITransientDependency { - private readonly IMethodInvocationFeatureCheckerService _methodInvocationAuthorizationService; - - public AbpFeatureActionFilter(IMethodInvocationFeatureCheckerService methodInvocationAuthorizationService) - { - _methodInvocationAuthorizationService = methodInvocationAuthorizationService; - } - public async Task OnActionExecutionAsync( - ActionExecutingContext context, + ActionExecutingContext context, ActionExecutionDelegate next) { if (!context.ActionDescriptor.IsControllerAction()) @@ -31,7 +24,7 @@ namespace Volo.Abp.AspNetCore.Mvc.Features using (AbpCrossCuttingConcerns.Applying(context.Controller, AbpCrossCuttingConcerns.FeatureChecking)) { - await _methodInvocationAuthorizationService.CheckAsync( + await context.HttpContext.RequestServices.GetRequiredService().CheckAsync( new MethodInvocationFeatureCheckerContext(methodInfo) ); diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Features/AbpFeaturePageFilter.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Features/AbpFeaturePageFilter.cs index 83e26ace9c..3f54597c2a 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Features/AbpFeaturePageFilter.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Features/AbpFeaturePageFilter.cs @@ -1,6 +1,7 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc.Abstractions; using Microsoft.AspNetCore.Mvc.Filters; +using Microsoft.Extensions.DependencyInjection; using Volo.Abp.Aspects; using Volo.Abp.DependencyInjection; using Volo.Abp.Features; @@ -9,18 +10,11 @@ namespace Volo.Abp.AspNetCore.Mvc.Features { public class AbpFeaturePageFilter : IAsyncPageFilter, ITransientDependency { - private readonly IMethodInvocationFeatureCheckerService _methodInvocationAuthorizationService; - - public AbpFeaturePageFilter(IMethodInvocationFeatureCheckerService methodInvocationAuthorizationService) - { - _methodInvocationAuthorizationService = methodInvocationAuthorizationService; - } - public Task OnPageHandlerSelectionAsync(PageHandlerSelectedContext context) { return Task.CompletedTask; } - + public async Task OnPageHandlerExecutionAsync(PageHandlerExecutingContext context, PageHandlerExecutionDelegate next) { if (context.HandlerMethod == null || !context.ActionDescriptor.IsPageAction()) @@ -33,7 +27,7 @@ namespace Volo.Abp.AspNetCore.Mvc.Features using (AbpCrossCuttingConcerns.Applying(context.HandlerInstance, AbpCrossCuttingConcerns.FeatureChecking)) { - await _methodInvocationAuthorizationService.CheckAsync( + await context.HttpContext.RequestServices.GetRequiredService().CheckAsync( new MethodInvocationFeatureCheckerContext(methodInfo) ); @@ -41,4 +35,4 @@ namespace Volo.Abp.AspNetCore.Mvc.Features } } } -} \ No newline at end of file +} diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/GlobalFeatures/GlobalFeatureActionFilter.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/GlobalFeatures/GlobalFeatureActionFilter.cs index e3fa489115..f0f08fad16 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/GlobalFeatures/GlobalFeatureActionFilter.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/GlobalFeatures/GlobalFeatureActionFilter.cs @@ -3,6 +3,7 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Abstractions; using Microsoft.AspNetCore.Mvc.Filters; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; using Volo.Abp.DependencyInjection; @@ -13,13 +14,6 @@ namespace Volo.Abp.AspNetCore.Mvc.GlobalFeatures { public class GlobalFeatureActionFilter : IAsyncActionFilter, ITransientDependency { - public ILogger Logger { get; set; } - - public GlobalFeatureActionFilter() - { - Logger = NullLogger.Instance; - } - public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) { if (!context.ActionDescriptor.IsControllerAction()) @@ -30,7 +24,11 @@ namespace Volo.Abp.AspNetCore.Mvc.GlobalFeatures if (!IsGlobalFeatureEnabled(context.Controller.GetType(), out var attribute)) { - Logger.LogWarning($"The '{context.Controller.GetType().FullName}' controller needs to enable '{attribute.Name}' feature."); + var logger = + context.HttpContext.RequestServices.GetRequiredService>() ?? + NullLogger.Instance; + + logger.LogWarning($"The '{context.Controller.GetType().FullName}' controller needs to enable '{attribute.Name}' feature."); context.Result = new NotFoundResult(); return; } diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/GlobalFeatures/GlobalFeaturePageFilter.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/GlobalFeatures/GlobalFeaturePageFilter.cs index d29c44b4af..18c56ab698 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/GlobalFeatures/GlobalFeaturePageFilter.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/GlobalFeatures/GlobalFeaturePageFilter.cs @@ -3,6 +3,7 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Abstractions; using Microsoft.AspNetCore.Mvc.Filters; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; using Volo.Abp.DependencyInjection; @@ -13,13 +14,6 @@ namespace Volo.Abp.AspNetCore.Mvc.GlobalFeatures { public class GlobalFeaturePageFilter: IAsyncPageFilter, ITransientDependency { - public ILogger Logger { get; set; } - - public GlobalFeaturePageFilter() - { - Logger = NullLogger.Instance; - } - public Task OnPageHandlerSelectionAsync(PageHandlerSelectedContext context) { return Task.CompletedTask; @@ -35,7 +29,11 @@ namespace Volo.Abp.AspNetCore.Mvc.GlobalFeatures if (!IsGlobalFeatureEnabled(context.HandlerInstance.GetType(), out var attribute)) { - Logger.LogWarning($"The '{context.HandlerInstance.GetType().FullName}' page needs to enable '{attribute.Name}' feature."); + var logger = + context.HttpContext.RequestServices.GetRequiredService>() ?? + NullLogger.Instance; + + logger.LogWarning($"The '{context.HandlerInstance.GetType().FullName}' page needs to enable '{attribute.Name}' feature."); context.Result = new NotFoundResult(); return; } diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/AbpHybridJsonInputFormatter.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/AbpHybridJsonInputFormatter.cs index bd2ade69ad..8c0277321e 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/AbpHybridJsonInputFormatter.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/AbpHybridJsonInputFormatter.cs @@ -8,8 +8,14 @@ namespace Volo.Abp.AspNetCore.Mvc.Json { public class AbpHybridJsonInputFormatter : TextInputFormatter, IInputFormatterExceptionPolicy { - public AbpHybridJsonInputFormatter() + private readonly SystemTextJsonInputFormatter _systemTextJsonInputFormatter; + private readonly NewtonsoftJsonInputFormatter _newtonsoftJsonInputFormatter; + + public AbpHybridJsonInputFormatter(SystemTextJsonInputFormatter systemTextJsonInputFormatter, NewtonsoftJsonInputFormatter newtonsoftJsonInputFormatter) { + _systemTextJsonInputFormatter = systemTextJsonInputFormatter; + _newtonsoftJsonInputFormatter = newtonsoftJsonInputFormatter; + SupportedEncodings.Add(UTF8EncodingWithoutBOM); SupportedEncodings.Add(UTF16EncodingLittleEndian); @@ -18,7 +24,7 @@ namespace Volo.Abp.AspNetCore.Mvc.Json SupportedMediaTypes.Add(MediaTypeHeaderValues.ApplicationAnyJsonSyntax); } - public async override Task ReadRequestBodyAsync(InputFormatterContext context, Encoding encoding) + public override async Task ReadRequestBodyAsync(InputFormatterContext context, Encoding encoding) { return await GetTextInputFormatter(context).ReadRequestBodyAsync(context, encoding); } @@ -26,12 +32,13 @@ namespace Volo.Abp.AspNetCore.Mvc.Json protected virtual TextInputFormatter GetTextInputFormatter(InputFormatterContext context) { var typesMatcher = context.HttpContext.RequestServices.GetRequiredService(); + if (!typesMatcher.Match(context.ModelType)) { - return context.HttpContext.RequestServices.GetRequiredService(); + return _systemTextJsonInputFormatter; } - return context.HttpContext.RequestServices.GetRequiredService(); + return _newtonsoftJsonInputFormatter; } public virtual InputFormatterExceptionPolicy ExceptionPolicy => InputFormatterExceptionPolicy.MalformedInputExceptions; diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/AbpHybridJsonOptionsSetup.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/AbpHybridJsonOptionsSetup.cs new file mode 100644 index 0000000000..c0987e8f76 --- /dev/null +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/AbpHybridJsonOptionsSetup.cs @@ -0,0 +1,73 @@ +using System.Buffers; +using System.Text.Encodings.Web; +using System.Text.Json; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Formatters; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.ObjectPool; +using Microsoft.Extensions.Options; + +namespace Volo.Abp.AspNetCore.Mvc.Json +{ + public class AbpHybridJsonOptionsSetup : IConfigureOptions + { + private readonly IOptions _jsonOptions; + private readonly IOptions _mvcNewtonsoftJsonOptions; + private readonly ILoggerFactory _loggerFactory; + private readonly ArrayPool _charPool; + private readonly ObjectPoolProvider _objectPoolProvider; + + public AbpHybridJsonOptionsSetup( + IOptions jsonOptions, + IOptions mvcNewtonsoftJsonOptions, + ILoggerFactory loggerFactory, + ArrayPool charPool, + ObjectPoolProvider objectPoolProvider) + { + _jsonOptions = jsonOptions; + _mvcNewtonsoftJsonOptions = mvcNewtonsoftJsonOptions; + _loggerFactory = loggerFactory; + _charPool = charPool; + _objectPoolProvider = objectPoolProvider; + } + + public void Configure(MvcOptions options) + { + var systemTextJsonInputFormatter = new SystemTextJsonInputFormatter( + _jsonOptions.Value, + _loggerFactory.CreateLogger()); + + var newtonsoftJsonInputFormatter = new NewtonsoftJsonInputFormatter( + _loggerFactory.CreateLogger(), + _mvcNewtonsoftJsonOptions.Value.SerializerSettings, + _charPool, + _objectPoolProvider, + options, + _mvcNewtonsoftJsonOptions.Value); + + options.InputFormatters.RemoveType(); + options.InputFormatters.RemoveType(); + options.InputFormatters.Add(new AbpHybridJsonInputFormatter(systemTextJsonInputFormatter, newtonsoftJsonInputFormatter)); + + var jsonSerializerOptions = _jsonOptions.Value.JsonSerializerOptions; + if (jsonSerializerOptions.Encoder is null) + { + // If the user hasn't explicitly configured the encoder, use the less strict encoder that does not encode all non-ASCII characters. + jsonSerializerOptions = new JsonSerializerOptions(jsonSerializerOptions) + { + Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping, + }; + } + + var systemTextJsonOutputFormatter = new SystemTextJsonOutputFormatter(jsonSerializerOptions); + var newtonsoftJsonOutputFormatter = new NewtonsoftJsonOutputFormatter( + _mvcNewtonsoftJsonOptions.Value.SerializerSettings, + _charPool, + options); + + options.OutputFormatters.RemoveType(); + options.OutputFormatters.RemoveType(); + options.OutputFormatters.Add(new AbpHybridJsonOutputFormatter(systemTextJsonOutputFormatter, newtonsoftJsonOutputFormatter)); + } + } +} diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/AbpHybridJsonOutputFormatter.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/AbpHybridJsonOutputFormatter.cs index 1507457f08..1dcd5d0fac 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/AbpHybridJsonOutputFormatter.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/AbpHybridJsonOutputFormatter.cs @@ -8,8 +8,14 @@ namespace Volo.Abp.AspNetCore.Mvc.Json { public class AbpHybridJsonOutputFormatter : TextOutputFormatter { - public AbpHybridJsonOutputFormatter() + private readonly SystemTextJsonOutputFormatter _systemTextJsonOutputFormatter; + private readonly NewtonsoftJsonOutputFormatter _newtonsoftJsonOutputFormatter; + + public AbpHybridJsonOutputFormatter(SystemTextJsonOutputFormatter systemTextJsonOutputFormatter, NewtonsoftJsonOutputFormatter newtonsoftJsonOutputFormatter) { + _systemTextJsonOutputFormatter = systemTextJsonOutputFormatter; + _newtonsoftJsonOutputFormatter = newtonsoftJsonOutputFormatter; + SupportedEncodings.Add(Encoding.UTF8); SupportedEncodings.Add(Encoding.Unicode); @@ -18,7 +24,7 @@ namespace Volo.Abp.AspNetCore.Mvc.Json SupportedMediaTypes.Add(MediaTypeHeaderValues.ApplicationAnyJsonSyntax); } - public async override Task WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding) + public override async Task WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding) { await GetTextInputFormatter(context).WriteResponseBodyAsync(context, selectedEncoding); } @@ -28,10 +34,10 @@ namespace Volo.Abp.AspNetCore.Mvc.Json var typesMatcher = context.HttpContext.RequestServices.GetRequiredService(); if (!typesMatcher.Match(context.ObjectType)) { - return context.HttpContext.RequestServices.GetRequiredService(); + return _systemTextJsonOutputFormatter; } - return context.HttpContext.RequestServices.GetRequiredService(); + return _newtonsoftJsonOutputFormatter; } } } diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/MvcCoreBuilderExtensions.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/MvcCoreBuilderExtensions.cs index b4e19e6ae7..366a24e8f4 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/MvcCoreBuilderExtensions.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/MvcCoreBuilderExtensions.cs @@ -1,12 +1,7 @@ -using System.Buffers; -using System.Text.Json; -using Microsoft.AspNetCore.Mvc; -using Microsoft.AspNetCore.Mvc.Formatters; +using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Options; -using System.Text.Encodings.Web; -using Microsoft.Extensions.Logging; using Microsoft.Extensions.ObjectPool; using Volo.Abp.Json; @@ -24,68 +19,10 @@ namespace Volo.Abp.AspNetCore.Mvc.Json return builder; } - //SystemTextJsonInputFormatter - builder.Services.AddTransient(provider => - { - var jsonOptions = provider.GetRequiredService>(); - var logger = provider.GetRequiredService().CreateLogger(); - return new SystemTextJsonInputFormatter(jsonOptions.Value, logger); - }); - builder.Services.TryAddTransient(); - //NewtonsoftJsonInputFormatter - builder.Services.AddTransient(provider => - { - var jsonOptions = provider.GetRequiredService>().Value; - - return new NewtonsoftJsonInputFormatter( - provider.GetRequiredService().CreateLogger(), - jsonOptions.SerializerSettings, - provider.GetRequiredService>(), - provider.GetRequiredService(), - provider.GetRequiredService>().Value, - jsonOptions); - }); - - //SystemTextJsonOutputFormatter - builder.Services.AddTransient(provider => - { - var jsonSerializerOptions = provider.GetRequiredService>().Value.JsonSerializerOptions; - if (jsonSerializerOptions.Encoder is null) - { - // If the user hasn't explicitly configured the encoder, use the less strict encoder that does not encode all non-ASCII characters. - jsonSerializerOptions = new JsonSerializerOptions(jsonSerializerOptions) - { - Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping, - }; - } - return new SystemTextJsonOutputFormatter(jsonSerializerOptions); - }); - - //NewtonsoftJsonOutputFormatter - builder.Services.AddTransient(provider => - { - var jsonOptions = provider.GetRequiredService>().Value; - return new NewtonsoftJsonOutputFormatter( - jsonOptions.SerializerSettings, - provider.GetRequiredService>(), - provider.GetRequiredService>().Value); - }); - builder.Services.TryAddEnumerable(ServiceDescriptor.Transient, AbpJsonOptionsSetup>()); builder.Services.TryAddEnumerable(ServiceDescriptor.Transient, AbpMvcNewtonsoftJsonOptionsSetup>()); - - builder.Services.Configure(options => - { - options.InputFormatters.RemoveType(); - options.InputFormatters.RemoveType(); - options.InputFormatters.Add(new AbpHybridJsonInputFormatter()); - - options.OutputFormatters.RemoveType(); - options.OutputFormatters.RemoveType(); - options.OutputFormatters.Add(new AbpHybridJsonOutputFormatter()); - }); - + builder.Services.TryAddEnumerable(ServiceDescriptor.Transient, AbpHybridJsonOptionsSetup>()); return builder; } } diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Uow/AbpUowActionFilter.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Uow/AbpUowActionFilter.cs index 552ce7e04e..8f11ede0a5 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Uow/AbpUowActionFilter.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Uow/AbpUowActionFilter.cs @@ -1,8 +1,9 @@ -using System; +using System; using System.Net.Http; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc.Abstractions; using Microsoft.AspNetCore.Mvc.Filters; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using Volo.Abp.AspNetCore.Uow; using Volo.Abp.DependencyInjection; @@ -12,15 +13,6 @@ namespace Volo.Abp.AspNetCore.Mvc.Uow { public class AbpUowActionFilter : IAsyncActionFilter, ITransientDependency { - private readonly IUnitOfWorkManager _unitOfWorkManager; - private readonly AbpUnitOfWorkDefaultOptions _defaultOptions; - - public AbpUowActionFilter(IUnitOfWorkManager unitOfWorkManager, IOptions options) - { - _unitOfWorkManager = unitOfWorkManager; - _defaultOptions = options.Value; - } - public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) { if (!context.ActionDescriptor.IsControllerAction()) @@ -45,20 +37,22 @@ namespace Volo.Abp.AspNetCore.Mvc.Uow var options = CreateOptions(context, unitOfWorkAttr); + var unitOfWorkManager = context.HttpContext.RequestServices.GetRequiredService(); + //Trying to begin a reserved UOW by AbpUnitOfWorkMiddleware - if (_unitOfWorkManager.TryBeginReserved(AbpUnitOfWorkMiddleware.UnitOfWorkReservationName, options)) + if (unitOfWorkManager.TryBeginReserved(AbpUnitOfWorkMiddleware.UnitOfWorkReservationName, options)) { var result = await next(); if (!Succeed(result)) { - await RollbackAsync(context); + await RollbackAsync(context, unitOfWorkManager); } return; } //Begin a new, independent unit of work - using (var uow = _unitOfWorkManager.Begin(options)) + using (var uow = unitOfWorkManager.Begin(options)) { var result = await next(); if (Succeed(result)) @@ -76,7 +70,8 @@ namespace Volo.Abp.AspNetCore.Mvc.Uow if (unitOfWorkAttribute?.IsTransactional == null) { - options.IsTransactional = _defaultOptions.CalculateIsTransactional( + var abpUnitOfWorkDefaultOptions = context.HttpContext.RequestServices.GetRequiredService>().Value; + options.IsTransactional = abpUnitOfWorkDefaultOptions.CalculateIsTransactional( autoValue: !string.Equals(context.HttpContext.Request.Method, HttpMethod.Get.Method, StringComparison.OrdinalIgnoreCase) ); } @@ -84,9 +79,9 @@ namespace Volo.Abp.AspNetCore.Mvc.Uow return options; } - private async Task RollbackAsync(ActionExecutingContext context) + private async Task RollbackAsync(ActionExecutingContext context, IUnitOfWorkManager unitOfWorkManager) { - var currentUow = _unitOfWorkManager.Current; + var currentUow = unitOfWorkManager.Current; if (currentUow != null) { await currentUow.RollbackAsync(context.HttpContext.RequestAborted); diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Uow/AbpUowPageFilter.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Uow/AbpUowPageFilter.cs index bcef10ecd4..7dbc65319e 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Uow/AbpUowPageFilter.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Uow/AbpUowPageFilter.cs @@ -1,8 +1,9 @@ -using System; +using System; using System.Net.Http; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc.Abstractions; using Microsoft.AspNetCore.Mvc.Filters; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using Volo.Abp.AspNetCore.Uow; using Volo.Abp.DependencyInjection; @@ -12,14 +13,6 @@ namespace Volo.Abp.AspNetCore.Mvc.Uow { public class AbpUowPageFilter : IAsyncPageFilter, ITransientDependency { - private readonly IUnitOfWorkManager _unitOfWorkManager; - private readonly AbpUnitOfWorkDefaultOptions _defaultOptions; - - public AbpUowPageFilter(IUnitOfWorkManager unitOfWorkManager, IOptions options) - { - _unitOfWorkManager = unitOfWorkManager; - _defaultOptions = options.Value; - } public Task OnPageHandlerSelectionAsync(PageHandlerSelectedContext context) { return Task.CompletedTask; @@ -49,20 +42,22 @@ namespace Volo.Abp.AspNetCore.Mvc.Uow var options = CreateOptions(context, unitOfWorkAttr); + var unitOfWorkManager = context.HttpContext.RequestServices.GetRequiredService(); + //Trying to begin a reserved UOW by AbpUnitOfWorkMiddleware - if (_unitOfWorkManager.TryBeginReserved(AbpUnitOfWorkMiddleware.UnitOfWorkReservationName, options)) + if (unitOfWorkManager.TryBeginReserved(AbpUnitOfWorkMiddleware.UnitOfWorkReservationName, options)) { var result = await next(); if (!Succeed(result)) { - await RollbackAsync(context); + await RollbackAsync(context, unitOfWorkManager); } return; } //Begin a new, independent unit of work - using (var uow = _unitOfWorkManager.Begin(options)) + using (var uow = unitOfWorkManager.Begin(options)) { var result = await next(); if (Succeed(result)) @@ -80,7 +75,8 @@ namespace Volo.Abp.AspNetCore.Mvc.Uow if (unitOfWorkAttribute?.IsTransactional == null) { - options.IsTransactional = _defaultOptions.CalculateIsTransactional( + var abpUnitOfWorkDefaultOptions = context.HttpContext.RequestServices.GetRequiredService>().Value; + options.IsTransactional = abpUnitOfWorkDefaultOptions.CalculateIsTransactional( autoValue: !string.Equals(context.HttpContext.Request.Method, HttpMethod.Get.Method, StringComparison.OrdinalIgnoreCase) ); } @@ -88,9 +84,9 @@ namespace Volo.Abp.AspNetCore.Mvc.Uow return options; } - private async Task RollbackAsync(PageHandlerExecutingContext context) + private async Task RollbackAsync(PageHandlerExecutingContext context, IUnitOfWorkManager unitOfWorkManager) { - var currentUow = _unitOfWorkManager.Current; + var currentUow = unitOfWorkManager.Current; if (currentUow != null) { await currentUow.RollbackAsync(context.HttpContext.RequestAborted); diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Validation/AbpValidationActionFilter.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Validation/AbpValidationActionFilter.cs index 8fe699f4ba..fe74419654 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Validation/AbpValidationActionFilter.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Validation/AbpValidationActionFilter.cs @@ -1,19 +1,13 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc.Abstractions; using Microsoft.AspNetCore.Mvc.Filters; +using Microsoft.Extensions.DependencyInjection; using Volo.Abp.DependencyInjection; namespace Volo.Abp.AspNetCore.Mvc.Validation { public class AbpValidationActionFilter : IAsyncActionFilter, ITransientDependency { - private readonly IModelStateValidator _validator; - - public AbpValidationActionFilter(IModelStateValidator validator) - { - _validator = validator; - } - public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) { //TODO: Configuration to disable validation for controllers..? @@ -25,7 +19,7 @@ namespace Volo.Abp.AspNetCore.Mvc.Validation return; } - _validator.Validate(context.ModelState); + context.HttpContext.RequestServices.GetRequiredService().Validate(context.ModelState); await next(); } }