Aspect Oriented Programming with StructureMap.DynamicInterception

那年仲夏 提交于 2019-12-25 08:43:13

问题


I implemented a console app that uses DynamicInterception on structure map, I follow the example on the page StructureMap DynamicInterception. Now I'm trying to pass the code to a asp.net MVC site but I get the following error

Decorator Interceptor failed during object construction. See the inner exception:

1.) DynamicProxyInterceptor of ITSector.Site.Core.Controllers.HomeController with interception behaviors: ITSector.Library.Aspect.LoggerInterceptor 2.) ITSector.Site.Core.Controllers.HomeController 3.) Instance of ITSector.Site.Core.Controllers.HomeController 4.) Container.GetInstance(ITSector.Site.Core.Controllers.HomeController)

The inner exception is: Specified type is not an interface Parameter name: interfaceToProxy With the stacktrace:

at Castle.DynamicProxy.ProxyGenerator.CreateInterfaceProxyWithTarget(Type interfaceToProxy, Type[] additionalInterfacesToProxy, Object target, ProxyGenerationOptions options, IInterceptor[] interceptors)
   at Castle.DynamicProxy.ProxyGenerator.CreateInterfaceProxyWithTarget[TInterface](TInterface target, IInterceptor[] interceptors)
   at lambda_method(Closure , IBuildSession , IContext )

I use the Structure.MVC5.Update nuget as the Dependency Resolver. Can anyone give me any hint on how to implement it to intercept calls on controller methods.

Thanks.


回答1:


StructureMap is expecting an interface in the For<T>() method, but you're passing it HomeController, which is a concrete class.

You have a couple of options to get it working. You can attach your LoggerInteceptor to all controllers with For<IController>(). If you only want to attach the logger to some of your controllers, you can create a new interface specifically for the logged controllers, like For<ILoggedController>(), and ensure the desired controllers inherit from ILoggedController.



来源:https://stackoverflow.com/questions/47104842/aspect-oriented-programming-with-structuremap-dynamicinterception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!