Asp.Net MVC Controller: declarative AOP with Spring.Net

后端 未结 2 603
轻奢々
轻奢々 2020-12-07 03:24

Is it possible, that Spring.Net Aspects don\'t work with Asp.Net Controller?

I want to configure transactions on Action methods of Controllers but the proxy doesn\'t

相关标签:
2条回答
  • 2020-12-07 04:13

    I had a similar a problem with the [Transaction] attribute (which works using Spring.AOP). In my case, I called the [Transaction] flagged methods from within the same class and was surprised that the transaction advice didn't fire.

    The explanation was, that when calling a [Transaction] marked method from within the class, you hold a reference to the real instance instead of the AOP-proxied instance, therefore the call does not get intercepted.

    When a request is made to an MVC app, then from the request url a controller is chosen (from the IControllerFactory instance). On this controller, the Execute method is called, which in turn is responsible for calling the actions. So I think that action methods are always called from within the controller. Which means that by definition action methods will never get intercepted. This would explain why those pointcuts are recognized, but don't fire.

    if I take an other class than a controller as pointcut it works

    It also explains why pointcuts on other classes than controllers do fire: they are likely called from a controller, which will hold a AOP-proxied reference to instances of the other classes.

    ... for some methods the advice works ... For example for the setter for the repository

    I assume that your (for instance) CompanyController.CustomerController has a property CustomerRepository, set using DI. It makes sense that this pointcut fires, because the setter is called from outside the CompanyController.CustomerController, for instance by your DI container (or your ControllerFactory).

    A solution could be to introduce service objects, on which you define the transaction advice that you now have on your controllers. From your controllers, you call methods on these service objects - and then the pointcuts will fire.

    0 讨论(0)
  • 2020-12-07 04:22

    Can you try with the configuration

    <aop:config proxy-target-type="true">
    

    This will create an inheritance based proxy. you may need to download a recent nightly build of spirng.net for this to work (not 1.3.0).

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