Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.net 6.0,按照上面的式例,在方法添加Transactional特性,但是没有生效,不是进入事务里,这个是什么原因引起吗? #1239

Closed
km93522719 opened this issue Sep 7, 2022 · 10 comments

Comments

@km93522719
Copy link

km93522719 commented Sep 7, 2022

.net 6.0,按照上面的式例,在方法添加Transactional特性,但是没有生效,不是进入事务里,这个是什么原因引起吗?
Service相关代码:

[Transactional]
private async Task SaveFlowInfoAsync(FlowInfoAddInput flowInfoInput, CancellationToken cancellationToken)
{
    //using IUnitOfWork unitOfWork = _unitOfWorkManager.Begin(isolationLevel: IsolationLevel.Snapshot);
    //try
    //{
    await this._flowDetailService.AddAsync(flowInfoInput, cancellationToken); 
    //其他业务逻辑
    。。。
   
    //    unitOfWork.Commit();
    //}
    //catch (Exception e)
    //{
    //    unitOfWork.Rollback();
    //    LogUtils.LogErr(this, $"流程提交异常:{e.Message}。PARAMS:{flowInfoInput.FlowSubmitInput.ToJson()}", e);
    //    throw new CustomException(ExceptionConst.FlowSubmitFail);
    //}
}

programe.cs代码:

var app = builder.Build();

//使用Correlation中间件
app.UseCorrelationId();
if (app.Environment.IsDevelopment())
{
    app.UseDeveloperExceptionPage();
}
//使用自定义的异常中间件
app.UseErrorHandling();
app.UseStaticFiles();
app.UseLocalLogViewer();
app.Use(async (context, next) =>
{
    TransactionalAttribute.SetServiceProvider(context.RequestServices);
    await next();
});
//使用路由中间件
app.UseRouting()
//使用终端中间件
.UseEndpoints(endpoints =>
{
    endpoints.MapGet("/", async context =>
    {
        await context.Response.WriteAsync("Hello World!");
    });
    endpoints.MapControllers();
});

app.Run();

Originally posted by @km93522719 in #289 (comment)

@2881099
Copy link
Collaborator

2881099 commented Sep 7, 2022

https://github.com/dotnetcore/FreeSql/wiki/DI-UnitOfWorkManager

你的代码和文档这里相差很大

@km93522719
Copy link
Author

https://github.com/dotnetcore/FreeSql/wiki/DI-UnitOfWorkManager

你的代码和文档这里相差很大

我是按照上面的文档进行引用的,上面问题的代码只是其中一部分代码

@km93522719
Copy link
Author

https://github.com/dotnetcore/FreeSql/wiki/DI-UnitOfWorkManager
你的代码和文档这里相差很大

我是按照上面的文档进行引用的,上面问题的代码只是其中一部分代码

直接使用using IUnitOfWork unitOfWork = _unitOfWorkManager.Begin(isolationLevel: IsolationLevel.Snapshot);是可以使用事务的,现在是想使用特性方式进行使用事务,然后我们这边也按照上面文档,如下:
第一步引入动态,添加TransactionalAttribute这个特性类
第二步是在program.cs文件(由于.net 6.0去掉了startup.cs这个文件,相关的代码都在program.cs了)
第三步是在Service类添加相关的Transactional这个特性,但是调试时没有进入这个特性类的方法里

@luoyunchong
Copy link
Collaborator

因为你是private方法,一般情况下,必须拥有接口,或此方法是public + virtual 具体看AOP要求

@km93522719
Copy link
Author

km93522719 commented Sep 7, 2022

因为你是private方法,一般情况下,必须拥有接口,或此方法是public + virtual 具体看AOP要求

这跟private方法没有关系吧,我这边试着将其改成public也无法调用特性transalational里面的方法

@luoyunchong
Copy link
Collaborator

@km93522719
Copy link
Author

项目中有示例:https://github.com/dotnetcore/FreeSql/tree/master/Examples/aspnetcore_transaction

这个例子我这边也试用过,也是不行😓

@luoyunchong
Copy link
Collaborator

其他方案

增加了 UnitOfWorkActionFilter,也支持Controller级别

@km93522719
Copy link
Author

我想问一下TransactionalAttribute这个特性是通过什么进行触发的?

@2881099
Copy link
Collaborator

2881099 commented Sep 7, 2022

我想问一下TransactionalAttribute这个特性是通过什么进行触发的?

动态代理组件触发的,上面有资料,这是面向切面编程的一种设计。 AOP

@2881099 2881099 closed this as completed Oct 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants