Injecting Constructors with CustomAttributeMatchingRule

Jul 2, 2013 at 5:02 PM
Trying to make CustomAttributeMatchingRule for Policy injection work, seems that I should do something with the constructor.
The types are unknown and defined in a different class as it's properties.
Got it work in a different manner.

Getting this error
The type Microsoft.Practices.Unity.InterceptionExtension.CustomAttributeMatchingRule does not have a constructor that takes the parameters (LoggingAttribute, Boolean).

Error Location: in below line of the UnityContainer.cs
member.AddPolicies(from, to, name, policies);
The Code
     Type AttType = asp.TypeName; 
     Container.Configure<Interception>()
         .AddPolicy(asp.PolicyName)
         .AddMatchingRule<CustomAttributeMatchingRule>(new
             InjectionConstructor(AttType, true))
         .AddCallHandler(asp.CallHandlerType,
         new ContainerControlledLifetimeManager());
Jul 2, 2013 at 5:39 PM
I would change the code to use the non-generic version:
    Type AttType = asp.CallHandlerType;

    var matchingRule = new CustomAttributeMatchingRule(asp.TypeName, true);

    Container.Configure<Interception>()
        .AddPolicy(asp.PolicyName)
        .AddMatchingRule(matchingRule)
        .AddCallHandler(asp.CallHandlerType,
          new ContainerControlledLifetimeManager());

Probably related to Unity resolving constructors with System.Type in their signature (see http://unity.codeplex.com/discussions/405331 for a discussion).

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to
Jul 2, 2013 at 6:39 PM
Thanks for the fast answer,
you should be right cause the problem disappeared, but it doesn't intercept anything even in my MainApp's ViewModel.

I added this and tested on it's Virtual methods.
.SetInterceptorFor(Type.GetType(stTypeWithAsm), new VirtualMethodInterceptor())
before that I used TypeMatchingRule successfully, but I need to use attributes.
Jul 2, 2013 at 7:23 PM
Not sure what your full code looks like but something like this should work:
    container.AddNewExtension<Interception>();
    container.RegisterType<LoggingCallHandler>(new ContainerControlledLifetimeManager());

    container.RegisterType<MainWindowViewModel>(
        new Interceptor<VirtualMethodInterceptor>(), 
        new InterceptionBehavior<PolicyInjectionBehavior>());
            
    foreach (IAspect asp in Policy.Aspects)
    {
        Type AttType = asp.CallHandlerType;

        var matchingRule = new CustomAttributeMatchingRule(asp.TypeName, true);

        Container.Configure<Interception>()
            .AddPolicy(asp.PolicyName)
            .AddMatchingRule(matchingRule)
            .AddCallHandler(asp.CallHandlerType,
                new ContainerControlledLifetimeManager());
    }

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to