当前位置:网站首页>net6 的Web MVC项目中事务功能的应用
net6 的Web MVC项目中事务功能的应用
2022-08-11 05:24:00 【Three Big Stones】
主要原理是借助于ActionFilter过滤器对Action方法实现拦截,再加上TransactionScope事务处理功能,实现Action方法的事务功能。
1、实现IAsyncActionFilter接口,定义事务拦截器。
public class TransactionScopeFilter : IAsyncActionFilter
{
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
bool hasNotTransactionalAttribute = false;//action上是否标识不启用事务功能
if (context.ActionDescriptor is ControllerActionDescriptor)
{
var actionDesc = (ControllerActionDescriptor)context.ActionDescriptor;
hasNotTransactionalAttribute = actionDesc.MethodInfo.IsDefined(typeof(NotTransactionalAttribute), false);
}
if (hasNotTransactionalAttribute)
{
await next();
return;
}
using var txScope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled);
var result = await next();
if (result.Exception == null)//action执行结果没有异常则提交事务
{
txScope.Complete();
}
}
}
2、注册拦截器。
需要在Startup.cs代码中注册我们自定义的拦截器。
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddMvcCore(opt => {
opt.Filters.Add<RateLimitFilter>();//添加限流支持
opt.Filters.Add<TransactionScopeFilter>();//添加事务支持
opt.Filters.Add<ExceptionFilter>();//添加异常处理支持
opt.Filters.Add<LogFilter>();
});
services.AddDbContext<BookDbContext>(opt=> {
var connectString = this.Configuration.GetSection("DbConnectionStr").Value;
opt.UseSqlServer(connectString);
});
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "WebApplication1", Version = "v1" });
});
services.AddMemoryCache();//添加缓存服务
}
这样我们的action方法默认全部是支持事务功能的,如果不希望action添加事务支持,只需要在action方法上增加特性NotTransactional就可以了,这个是我自定义的一个标记特性。
public class NotTransactionalAttribute:Attribute
{
}
边栏推荐
- 珍爱网App竞品分析报告
- CMT2380F32模块开发7-reset例程
- Maykle Studio - HarmonyOS Application Development Third Training
- 梅科尔工作室-DjangoWeb 应用框架+MySQL数据库第六次培训
- Zhejiang University School of Software 2020 Guarantee Research Computer Real Question Practice
- Maykle Studio - HarmonyOS Application Development First Training
- 关于安全帽识别系统,你需要知道的选择要点
- 梅科尔工作室-HarmonyOS应用开发第三次培训
- Joint 3D Instance Segmentation and Object Detection for Autonomous Driving
- 动画(其二)
猜你喜欢
arduino的esp32环境搭建(不需要翻墙,不需要离线安装)
Mei cole studios - fifth training DjangoWeb application framework + MySQL database
产品如何拟定优化方案?
微文案也能提升用户体验
梅科尔工作室-华为云ModelArts第二次培训
OpenPCDet安装最新版:spconv一步到位
咕咚vs悦跑圈的竞品分析
梅科尔工作室-HarmonyOS应用开发第三次培训
Waymo dataset usage introduction (waymo-open-dataset)
AI-based intelligent image recognition: 4 different industry applications
随机推荐
数据库(其二)
Toolbar 和 DrawerLayout 滑动菜单
支付牌照是什么意思
LAGRANGIAN FLUID SIMULATION WITH CONTINUOUS CONVOLUTIONS
Safety helmet recognition - construction safety "regulator"
梅科尔工作室-HarmonyOS应用开发的第二次培训
TAMNet:A loss-balanced multi-task model for simultaneous detection and segmentation
Fragment 和 CardView
Reconstruction and Synthesis of Lidar Point Clouds of Spray
珍爱网App竞品分析报告
vmware不可恢复错误vmui
目标检测学习目录(持续更新)
HTTP缓存机制详解
10 个超好用的 DataGrip 快捷键,快加入收藏! | 实用技巧
智慧工地 安全帽识别系统
Socket 网络协议 等
解决jupyter中import torch出错问题
Introduction of safety helmet wearing recognition system
arduino的esp32环境搭建(不需要翻墙,不需要离线安装)
梅科尔工作室-HarmonyOS应用开发第三次培训