1

I am trying to use Interception in a MVC application. The problem I am having is with the controllers. Because the controllers are registered in the Unity container, and don't have interfaces, I am getting an error. I think I need a way to exclude the controllers from the Interception process or can I do something else? Is this possible or feasible? I asked a similar question on SO and, based on the comments I received, feel it's best to create another question.

Here's the error:

Exception information: Exception type: ResolutionFailedException Exception message: Resolution of the dependency failed, type = "CSR.Presentation.Controllers.HomeController", name = "(none)". Exception occurred while: Calling constructor Microsoft.Practices.Unity.InterceptionExtension.PolicyInjectionBehavior(Microsoft.Practices.Unity.InterceptionExtension.CurrentInterceptionRequest interceptionRequest, Microsoft.Practices.Unity.InterceptionExtension.InjectionPolicy[] policies, Microsoft.Practices.Unity.IUnityContainer container). Exception is: ArgumentException - Type passed must be an interface.

Here's the code;

// FIRST
   container.RegisterTypes(
       AllClasses.FromLoadedAssemblies(), 
       WithMappings.FromMatchingInterface, 
       WithName.Default,
       WithLifetime.ContainerControlled,
         t => new InjectionMember[] { 
            new Interceptor<InterfaceInterceptor>(),
            new InterceptionBehavior<PolicyInjectionBehavior>()});

// SECOND
   container.AddNewExtension<Interception>();

// THIRD    
   var first = new InjectionProperty("Order", 1);
   var second = new InjectionProperty("Order", 2);

   container.Configure<Interception>()
       .AddPolicy("logging")
       .AddMatchingRule<NamespaceMatchingRule>(
            new InjectionConstructor(
            new InjectionParameter("CSR.*")))             
       .AddCallHandler<LoggingCallHandler>(
            new ContainerControlledLifetimeManager(),
            new InjectionConstructor(),
            first);

// Controller ==> execution doesn't reach here using interception
 public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View("Index");
        }        
    }
Community
  • 1
  • 1
Big Daddy
  • 5,160
  • 5
  • 46
  • 76
  • did you find a solution for this? I am trying to set up interception to log calls to my controllers but no success yet. Thanks – Fabio Milheiro Aug 23 '16 at 16:03
  • 1
    @FabioMilheiro...Sorry, no I didn't – Big Daddy Aug 23 '16 at 16:20
  • I just found this: http://www.codeproject.com/Articles/469251/ASP-NET-MVC-controller-action-with-Interceptor-pat but don't like it at all. It seems that using filters is the best way to apply interception to controllers. – Fabio Milheiro Aug 23 '16 at 16:42
  • @FabioMilheiro...Thanks and I tend to agree. They are what's expected by other devs and this makes them more discoverable and maintainable. – Big Daddy Aug 23 '16 at 17:13

0 Answers0