src/ShenNius.Share.Domain/Repository/TransactionAttribute.cs
(开头部分) 2KB这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。
using AspectCore.DynamicProxy;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Threading.Tasks;
/*************************************
* 类名:TransactionInterceptorAttribute
* 作者:realyrare
* 邮箱:mhg215@yeah.net
* 时间:2021/3/30 16:27:18
*┌───────────────────────────────────┐
*│ 版权所有:神牛软件 │
*└───────────────────────────────────┘
**************************************/
namespace ShenNius.Share.Domain.Repository
{
/// <summary>
/// AOP 事务
/// </summary>
public class TransactionAttribute : AbstractInterceptorAttribute
{
public async override Task Invoke(AspectContext context, AspectDelegate next)
{
// DbContext dbContext = new DbContext();
var dbContext = context.ServiceProvider.GetService<DbContext>();
try
{
dbContext.Db.BeginTran();
var result = await RunAndGetReturn(context, next);
if (result is Task)
{
Task.WaitAll(result as Task);
}
dbContext.Db.CommitTran();
}
catch (AggregateException ex)
{
dbContext.Db.RollbackTran();
throw ex;
}
catch (Exception)
{
dbContext.Db.RollbackTran();
throw;
}
}
/// <summary>
/// 执行被拦截方法
/// </summary>
/// <param name="context"></param>
/// <param name="next"></param>
/// <returns></returns>
private async Task<object> RunAndGetReturn(AspectContext context, AspectDelegate next)
{
await context.Invoke(next);
return context.IsAsync()
? await context.UnwrapAsyncReturnValue()
: context.ReturnValue;
}
后面还有 2 行代码,解锁后查看完整代码
24 小时内免费解锁 3 个项目,之后 1 积分/个。 规则说明
AI 解读
登录后可用,每次 10 积分,解读结果公开显示在下面。
还没有人解读过这个文件。
