Search code examples
c#asp.net-mvcaopstructuremap

Aspect Oriented Programming with StructureMap.DynamicInterception


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.


Solution

  • 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.