ASP.NET MVC 3, Action Filters, and Autofac Dependency Injection

前端 未结 2 1022
一整个雨季
一整个雨季 2021-01-02 17:02

On ASP.NET MVC 2 I have an ActionFilterAttribute called [Transaction] that starts an NHibernate transaction before executing the a

相关标签:
2条回答
  • 2021-01-02 17:48

    Oh yuck.... Nicholas, you might need to store your ISession and Transaction in HttpContext.Items, which you should be able to get to via the ActionExecutedContext/ActionExecutingContext (perhaps setting it in the OnActionExecuting event handler), instead of keeping them in instance members. Either that, or you could call a ServiceLocator inside your filter to grab them for you (also yuck).

    Now I have to go look through my MVC 3 code and see if I have any similar problems myself!

    0 讨论(0)
  • 2021-01-02 17:53

    I just asked a similar question on google forums. Here is the link https://groups.google.com/forum/#!topic/autofac/a0qqp2b3WA8

    I got the answer:

    builder.RegisterType<ExtensibleActionInvoker>().As<IActionInvoker>();
    
    
    builder.RegisterControllers(Assembly.GetExecutingAssembly()).InjectActionInvoker();
    

    Then you can use property injection in your attributes.

    0 讨论(0)
提交回复
热议问题