forked from abpframework/abp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request abpframework#3348 from abpframework/maliming/Inter…
…ceptors-ignore-types Introducing DynamicProxyIgnoreTypes.
- Loading branch information
Showing
38 changed files
with
998 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Auditing/AbpAuditPageFilter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Mvc.Abstractions; | ||
using Microsoft.AspNetCore.Mvc.Filters; | ||
using Microsoft.Extensions.Options; | ||
using Volo.Abp.Aspects; | ||
using Volo.Abp.Auditing; | ||
using Volo.Abp.DependencyInjection; | ||
|
||
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<AbpAuditingOptions> 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)) | ||
{ | ||
await next(); | ||
return; | ||
} | ||
|
||
using (AbpCrossCuttingConcerns.Applying(context.HandlerInstance, AbpCrossCuttingConcerns.Auditing)) | ||
{ | ||
var stopwatch = Stopwatch.StartNew(); | ||
|
||
try | ||
{ | ||
var result = await next(); | ||
|
||
if (result.Exception != null && !result.ExceptionHandled) | ||
{ | ||
auditLog.Exceptions.Add(result.Exception); | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
auditLog.Exceptions.Add(ex); | ||
throw; | ||
} | ||
finally | ||
{ | ||
stopwatch.Stop(); | ||
auditLogAction.ExecutionDuration = Convert.ToInt32(stopwatch.Elapsed.TotalMilliseconds); | ||
auditLog.Actions.Add(auditLogAction); | ||
} | ||
} | ||
} | ||
|
||
private bool ShouldSaveAudit(PageHandlerExecutingContext context, out AuditLogInfo auditLog, out AuditLogActionInfo auditLogAction) | ||
{ | ||
auditLog = null; | ||
auditLogAction = null; | ||
|
||
if (!Options.IsEnabled) | ||
{ | ||
return false; | ||
} | ||
|
||
if (!context.ActionDescriptor.IsPageAction()) | ||
{ | ||
return false; | ||
} | ||
|
||
var auditLogScope = _auditingManager.Current; | ||
if (auditLogScope == null) | ||
{ | ||
return false; | ||
} | ||
|
||
if (!_auditingHelper.ShouldSaveAudit(context.HandlerMethod.MethodInfo, true)) | ||
{ | ||
return false; | ||
} | ||
|
||
auditLog = auditLogScope.Log; | ||
auditLogAction = _auditingHelper.CreateAuditLogAction( | ||
auditLog, | ||
context.HandlerMethod.GetType(), | ||
context.HandlerMethod.MethodInfo, | ||
context.HandlerArguments | ||
); | ||
|
||
return true; | ||
} | ||
} | ||
} |
112 changes: 112 additions & 0 deletions
112
...lo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionPageFilter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Http; | ||
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.AspNetCore.ExceptionHandling; | ||
using Volo.Abp.DependencyInjection; | ||
using Volo.Abp.ExceptionHandling; | ||
using Volo.Abp.Http; | ||
using Volo.Abp.Json; | ||
|
||
namespace Volo.Abp.AspNetCore.Mvc.ExceptionHandling | ||
{ | ||
public class AbpExceptionPageFilter : IAsyncPageFilter, ITransientDependency | ||
{ | ||
public ILogger<AbpExceptionPageFilter> Logger { get; set; } | ||
|
||
private readonly IExceptionToErrorInfoConverter _errorInfoConverter; | ||
private readonly IHttpExceptionStatusCodeFinder _statusCodeFinder; | ||
private readonly IJsonSerializer _jsonSerializer; | ||
|
||
public AbpExceptionPageFilter( | ||
IExceptionToErrorInfoConverter errorInfoConverter, | ||
IHttpExceptionStatusCodeFinder statusCodeFinder, | ||
IJsonSerializer jsonSerializer) | ||
{ | ||
_errorInfoConverter = errorInfoConverter; | ||
_statusCodeFinder = statusCodeFinder; | ||
_jsonSerializer = jsonSerializer; | ||
|
||
Logger = NullLogger<AbpExceptionPageFilter>.Instance; | ||
} | ||
|
||
|
||
public Task OnPageHandlerSelectionAsync(PageHandlerSelectedContext context) | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
|
||
public async Task OnPageHandlerExecutionAsync(PageHandlerExecutingContext context, PageHandlerExecutionDelegate next) | ||
{ | ||
if (context.HandlerMethod == null || !ShouldHandleException(context)) | ||
{ | ||
await next(); | ||
return; | ||
} | ||
|
||
var pageHandlerExecutedContext = await next(); | ||
if (pageHandlerExecutedContext.Exception == null) | ||
{ | ||
return;; | ||
} | ||
|
||
await HandleAndWrapException(pageHandlerExecutedContext); | ||
} | ||
|
||
protected virtual bool ShouldHandleException(PageHandlerExecutingContext context) | ||
{ | ||
//TODO: Create DontWrap attribute to control wrapping..? | ||
|
||
if (context.ActionDescriptor.IsPageAction() && | ||
ActionResultHelper.IsObjectResult(context.HandlerMethod.MethodInfo.ReturnType)) | ||
{ | ||
return true; | ||
} | ||
|
||
if (context.HttpContext.Request.CanAccept(MimeTypes.Application.Json)) | ||
{ | ||
return true; | ||
} | ||
|
||
if (context.HttpContext.Request.IsAjax()) | ||
{ | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
protected virtual async Task HandleAndWrapException(PageHandlerExecutedContext context) | ||
{ | ||
//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); | ||
|
||
var remoteServiceErrorInfo = _errorInfoConverter.Convert(context.Exception); | ||
|
||
context.Result = new ObjectResult(new RemoteServiceErrorResponse(remoteServiceErrorInfo)); | ||
|
||
var logLevel = context.Exception.GetLogLevel(); | ||
|
||
Logger.LogWithLevel(logLevel, $"---------- {nameof(RemoteServiceErrorInfo)} ----------"); | ||
Logger.LogWithLevel(logLevel, _jsonSerializer.Serialize(remoteServiceErrorInfo, indented: true)); | ||
Logger.LogException(context.Exception, logLevel); | ||
|
||
await context.HttpContext | ||
.RequestServices | ||
.GetRequiredService<IExceptionNotifier>() | ||
.NotifyAsync( | ||
new ExceptionNotificationContext(context.Exception) | ||
); | ||
|
||
context.Exception = null; //Handled! | ||
} | ||
|
||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
...work/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Features/AbpFeaturePageFilter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Mvc.Abstractions; | ||
using Microsoft.AspNetCore.Mvc.Filters; | ||
using Volo.Abp.Aspects; | ||
using Volo.Abp.DependencyInjection; | ||
using Volo.Abp.Features; | ||
|
||
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()) | ||
{ | ||
await next(); | ||
return; | ||
} | ||
|
||
var methodInfo = context.HandlerMethod.MethodInfo; | ||
|
||
using (AbpCrossCuttingConcerns.Applying(context.HandlerInstance, AbpCrossCuttingConcerns.FeatureChecking)) | ||
{ | ||
await _methodInvocationAuthorizationService.CheckAsync( | ||
new MethodInvocationFeatureCheckerContext(methodInfo) | ||
); | ||
|
||
await next(); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.