You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
92 lines
2.5 KiB
C#
92 lines
2.5 KiB
C#
using Admin.Core.Common;
|
|
using Admin.Core.Repository;
|
|
using Castle.DynamicProxy;
|
|
using System;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Admin.Core.Extensions
|
|
{
|
|
/// <summary>
|
|
/// 事务拦截器AdminTranAOP 继承IInterceptor接口
|
|
/// </summary>
|
|
public class AdminTranAOP : IInterceptor
|
|
{
|
|
private readonly IUnitOfWork _unitOfWork;
|
|
public AdminTranAOP(IUnitOfWork unitOfWork)
|
|
{
|
|
_unitOfWork = unitOfWork;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 实例化IInterceptor唯一方法
|
|
/// </summary>
|
|
/// <param name="invocation">包含被拦截方法的信息</param>
|
|
public void Intercept(IInvocation invocation)
|
|
{
|
|
var method = invocation.MethodInvocationTarget ?? invocation.Method;
|
|
//对当前方法的特性验证
|
|
//如果需要验证
|
|
if (method.GetCustomAttributes(true).FirstOrDefault(x => x.GetType() == typeof(UseTranAttribute)) is UseTranAttribute)
|
|
{
|
|
try
|
|
{
|
|
Console.WriteLine($"Begin Transaction");
|
|
|
|
_unitOfWork.BeginTran();
|
|
|
|
invocation.Proceed();
|
|
|
|
|
|
// 异步获取异常,先执行
|
|
if (IsAsyncMethod(invocation.Method))
|
|
{
|
|
var result = invocation.ReturnValue;
|
|
if (result is Task)
|
|
{
|
|
Task.WaitAll(result as Task);
|
|
}
|
|
}
|
|
_unitOfWork.CommitTran();
|
|
|
|
}
|
|
catch (Exception)
|
|
{
|
|
Console.WriteLine($"Rollback Transaction");
|
|
_unitOfWork.RollbackTran();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
invocation.Proceed();//直接执行被拦截方法
|
|
}
|
|
|
|
}
|
|
|
|
private async Task SuccessAction(IInvocation invocation)
|
|
{
|
|
await Task.Run(() =>
|
|
{
|
|
//...
|
|
});
|
|
}
|
|
|
|
public static bool IsAsyncMethod(MethodInfo method)
|
|
{
|
|
return (
|
|
method.ReturnType == typeof(Task) ||
|
|
(method.ReturnType.IsGenericType && method.ReturnType.GetGenericTypeDefinition() == typeof(Task<>))
|
|
);
|
|
}
|
|
private async Task TestActionAsync(IInvocation invocation)
|
|
{
|
|
await Task.Run(null);
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|