Unity(3.5) Interception Not Working in MVC

2019-09-04 23:29发布

问题:

I am trying to incorporate Interception using PolicyInjectionBehavior and get the this 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.

If I exclude the Interception code in the registration, it works fine. Looping through the container in the Output Window, it looks like all the registrations are there. Also, if I manually register and include the Interception code, it also works. I have many interfaces and don't want to register them individually. Here's what I'm doing - in this order:

// 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");
        }        
    }