0

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");
        }        
    }
Big Daddy
  • 5,160
  • 5
  • 46
  • 76
  • Which of the following you are asking: "why interception only works on interfaces", "why my controller registered via Unity", "why `RegisterType` registers types and not mapping to interfaces", "why injection rule matches my controller's class name", "what is the meaning of `ContainerControlled` and should I use it to register my controllers as singletons", something else – Alexei Levenkov Jan 28 '15 at 15:24
  • @AlexeiLevenkov...I'm not asking any of those. It looks like the parameter in the controller's ctor isn't being resolved. Why not? Maybe that's not the case and the controller is registered in Unity? Why would it be? If I remove the Interception/injection code, this works. MatchingRule wrong? FromLoadedAssemblies wrong? – Big Daddy Jan 28 '15 at 15:40
  • Controller indeed is registered with Unity (don't you call `RegisterTypes` for that?) but it is type and interception requires (as I've seen so far) interface (same will apply to all dependencies)... Consider posting signature of controller's constructor + clarify whether you expect it to be registered or not. – Alexei Levenkov Jan 28 '15 at 15:44
  • @AlexeiLevenkov...I posted the controller. You are correct about the controller being registered. I expected that it wouldn't. It doesn't have an interface, so why is it and should it be? – Big Daddy Jan 28 '15 at 15:55
  • I think registering controllers with container is good thing and you should just configure your interception rule to exclude it. I've only seen interception on interfaces which seem to match error you see (also [documentation](https://msdn.microsoft.com/en-us/library/ff660890%28v=pandp.20%29.aspx) imply you can also do it on types with virtual methods). Since controllers don't have interfaces (or rather MVC does not use them via interfaces) there is nothing Unity interceptor can do to wrap action methods - you should use MVC's filter there. – Alexei Levenkov Jan 28 '15 at 16:10
  • To exclude controller from registration - filter out during RegisterAll (also I think by default Unity may register all types already). Alternatively - don't register Unity as controller factory. Your particular controller seem not to have any dependency - so should be fine not registered... – Alexei Levenkov Jan 28 '15 at 16:11
  • @AlexeiLevenkov...I've been looking for a way to filter out controllers from interception. All the interception rules seem to be geared towards including, not excluding. Any ideas or code? Thanks. – Big Daddy Jan 28 '15 at 16:47
  • No, sorry. You probably should ask separate question (building small standalone sample - no ASP.Net, just Unity + basic class could make new question even better) so someone could answer. – Alexei Levenkov Jan 28 '15 at 17:08

0 Answers0