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 – 可以从没有这些.svn文件夹的subversion中检出文
- asp.net – 如何防止asp:FormView作为表格呈现?
- asp.net – 多个DataContext类是否适合?
- asp.net-mvc – 用于子操作的ASP.NET MVC路由匹配
- asp.net – Web.config自定义错误模式冲突
- asp.net-mvc-3 – 如何在.NET MVC3中注入用于验证的依赖关系
- .net – Linq:获取DataContext中所有表的列表
- asp.net-mvc-4 – @ Html.Raw坚持编码引号
- asp.net-mvc-3 – MVC3和认证
- 如何在asp.net中检测服务器端的浏览器关闭?
- asp.net mvc 3 c#post变量数组
- asp.net-mvc – 在MVC/ASP.NET MVC中正确使用Mod
- asp.net – 我可以在SQL会话状态配置中重用现有连
- asp.net – 我可以在卫星装配中组合本地资源吗?
- 设置ASP.NET页面不被缓存(客户端/服务器端取消缓
- asp.net – 如何在Visual Studio 2008调试器中查
- asp.net-mvc-5 – 在MVC 5的IPasswordStore中,Se
- asp.net-ajax – Sys.Application.add_load()vs.
- asp.net-mvc – ASP.NET MVC不提供默认文档
- 在ASP.net中使用NVP API时,PayPal SetExpressChe
