asp.net-mvc-3 – ASP.NET MVC 3,动作过滤器和Autofac依赖注入
在ASP.NET MVC 2上,我有一个名为[Transaction]的ActionFilterAttribute,它在执行操作之前启动NHibernate事务,然后提交或回滚它,具体取决于是否抛出了异常. ISession实例是HttpRequestScoped()并由
Autofac注入.它看起来像这样并且效果很好:
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)] public sealed class TransactionAttribute : ActionFilterAttribute { private ITransaction transaction; public TransactionAttribute() { this.Order = 0; } public ISession Session { get; set; } public override void OnActionExecuted( ActionExecutedContext filterContext) { if (this.Session != null && this.transaction != null) { try { if (this.transaction.IsActive) { if (filterContext.Exception == null) { this.transaction.Commit(); } else { this.transaction.Rollback(); } } } finally { this.transaction.Dispose(); this.transaction = null; } } } public override void OnActionExecuting( ActionExecutingContext filterContext) { if (this.Session != null) { this.transaction = this.Session.BeginTransaction(); } } } 太棒了. Seems to be a common pattern. 在ASP.NET MVC 3中,我在“Breaking Changes”(强调我的)下看到了这个小小的模糊:
哎呀. >这是否意味着如果我升级到MVC 3,我就会被冲洗? 感谢您的任何见解. 解决方法
我刚在谷歌论坛上问了一个类似的问题.
这是链接 https://groups.google.com/forum/#!topic/autofac/a0qqp2b3WA8 我得到了答案: builder.RegisterType<ExtensibleActionInvoker>().As<IActionInvoker>(); builder.RegisterControllers(Assembly.GetExecutingAssembly()).InjectActionInvoker(); 然后,您可以在属性中使用属性注入. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net – 如何更改F#Canopy UI测试脚本中的下拉列表
- 什么可能导致ASP.NET在调试中跳过行?
- asp.net-mvc-2 – 在MVC 2中使用自定义TextWriter时,“Bina
- 如何在ASP.NET网站中解决长时间的Glimpse线路时间问题?
- 扩展ToolBarManager、ListView和Grid控件以实现气球式的Too
- 在我的应用程序中托管ASP.NET
- asp.net – 无法在嵌入式ASPX代码中引用动态对象
- asp.net – 如何创建/使用MembershipProvider?
- asp.net – SQL使用逗号分隔值和IN子句
- 权限管理之ASP.NET Forms身份认证
- Asp.Net WebApi接口返回值IHttpActionResult
- asp.net-membership – 具有自定义成员资格提供程
- asp.net – ascx中的Updatepanel刷新了整个控件
- asp.net – 加密ASP .NET 2.0和SQL Server 2005中
- asp.net-mvc – 访问OAuth中的电子邮件地址Exter
- asp.net-mvc – ASP.net MVC数据注释DateTime默认
- asp.net-mvc – 如何添加到Azure会话Cookie Http
- 单元测试后面的ASP.NET代码
- asp.net HiddenField:动态添加自定义属性
- ASP.NET核心:如何获取远程IP地址?