使用 StructureMap.DynamicInterception 的面向方面编程 [英] Aspect Oriented Programming with StructureMap.DynamicInterception

查看:16
本文介绍了使用 StructureMap.DynamicInterception 的面向方面编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现了一个在结构图上使用 DynamicInterception 的控制台应用程序,我按照页面上的示例 StructureMap动态拦截.现在我正在尝试将代码传递给 asp.net MVC 站点,但出现以下错误

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.) 带有拦截的 ITSector.Site.Core.Controllers.HomeController 的 DynamicProxyInterceptor行为:ITSector.Library.Aspect.LoggerInterceptor2.) ITSector.Site.Core.Controllers.HomeController3.) ITSector.Site.Core.Controllers.HomeController 的实例4.) Container.GetInstance(ITSector.Site.Core.Controllers.HomeController)

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)

内部异常是:指定类型不是接口参数名称:interfaceToProxy使用堆栈跟踪:

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 )

我使用 Structure.MVC5.Update nuget 作为依赖关系解析器.谁能给我任何关于如何实现它以拦截对控制器方法的调用的提示.

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.

谢谢.

推荐答案

StructureMap 需要 For<T>() 方法中的接口,但您正在传递它 HomeController,这是一个具体的类.

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

您有多种选择可以让它发挥作用.您可以使用 For()LoggerInteceptor 附加到所有控制器.如果您只想将记录器附加到您的某些控制器,您可以专门为记录的控制器创建一个新接口,例如 For<ILoggedController>(),并确保所需的控制器继承自 ILoggedController.

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.

这篇关于使用 StructureMap.DynamicInterception 的面向方面编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆