目录
1 MethodInterceptor
1.1 /// This will be called via Reflection
1.2 HandleAsync
/// </summary>
/// <typeparam name="TResult"></typeparam>
/// <param name="filterAttributes"></param>
/// <param name="exceptionFilterAttributes"></param>
/// <param name="methodExecutingContext"></param>
/// <returns></returns>
private async Task<TResult> HandleAsyncWithType<TResult>(IReadOnlyList<MethodFilterAttribute> filterAttributes, IReadOnlyList<ExceptionFilterAttribute> exceptionFilterAttributes, MethodExecutingContext methodExecutingContext)
{
foreach (var f in filterAttributes)
{
try
{
if (methodExecutingContext.Result == null) await f.OnMethodExecutingAsync(methodExecutingContext).ConfigureAwait(false);
}
catch (Exception ex)
{
var exContext = new MethodExceptionContext(ex, methodExecutingContext);
await HandleExceptionAsync(exceptionFilterAttributes, exContext);
if (!exContext.Handled)
{
throw;
}
}
}
var reversedFilterAttributes = filterAttributes.Reverse();
var methodExecutedContext = new MethodExecutedContext(methodExecutingContext);
foreach (var f in reversedFilterAttributes)
{
try
{
await f.OnMethodExecutedAsync(methodExecutedContext).ConfigureAwait(false);
}
catch (Exception ex)
{
var exContext = new MethodExceptionContext(ex, methodExecutedContext);
await HandleExceptionAsync(exceptionFilterAttributes, exContext);
if (!exContext.Handled)
{
throw;
}
}
}
if (methodExecutedContext.Result != null && methodExecutedContext.Result is TResult result)
{
return result;
}
return default(TResult);
}
private async Task HandleAsync(IReadOnlyList<MethodFilterAttribute> filterAttributes, IReadOnlyList<ExceptionFilterAttribute> exceptionFilterAttributes, MethodExecutingContext methodExecutingContext)
{
foreach (var f in filterAttributes)
{
try
{
if (methodExecutingContext.Result == null) await f.OnMethodExecutingAsync(methodExecutingContext).ConfigureAwait(false);
}
catch (Exception ex)
{
var exContext = new MethodExceptionContext(ex, methodExecutingContext);
await HandleExceptionAsync(exceptionFilterAttributes, exContext);
if (!exContext.Handled)
{
throw;
}
}
}
var reversedFilterAttributes = filterAttributes.Reverse();
var methodExecutedContext = new MethodExecutedContext(methodExecutingContext);
foreach (var f in reversedFilterAttributes)
{
try
{
await f.OnMethodExecutedAsync(methodExecutedContext).ConfigureAwait(false);
}
catch (Exception ex)
{
var exContext = new MethodExceptionContext(ex, methodExecutedContext);
await HandleExceptionAsync(exceptionFilterAttributes, exContext);
if (!exContext.Handled)
{
throw;
}
}
}
}