1

How do I configure Unity 2.0 Policy Injection to use custom attribute matching rule in configuration file?

What I want is to translate the following code snippet in the unity configuration file.

myContainer.Configure<Interception>()
       .AddPolicy("MyPolicy")
       .AddMatchingRule<CustomAttributeMatchingRule>
           (new InjectionConstructor(typeof(MyAttributeType), true))
       .AddCallHandler<MyCallHandler>
            ("MyValidator", 
            new ContainerControlledLifetimeManager());
surajnaik
  • 725
  • 2
  • 10
  • 26

1 Answers1

-1

Can configure as follows, [TypeName] need to be configured correctly according to your assembly.

... ...

<sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension, Microsoft.Practices.Unity.Interception.Configuration"/>

<container>
  <extension type="Interception"/>
  <interception>
    <policy name="MyPolicy">
      <matchingRule name="customAttribute" type="CustomAttributeMatchingRule">
        <constructor>
          <param name="attributeType" type="[MyAttributeType]"/>
          <param name="inherited" value="true"/>
        </constructor>
      </matchingRule>
      <callHandler name="MyValidator" type="[MyCallHandler]">
        <lifetime type="transient"/>
      </callHandler>
    </policy>
  </interception>
</container>

... ...

Ethan Wu
  • 428
  • 5
  • 15
  • The attributeType requires the Type object of the custom attribute. How do I pass the Type object of my custom attribute in the configuration? Or do I have implement a custom TypeConverter to get the Type object of my custom attribute? – surajnaik Jan 11 '12 at 09:44
  • It should be "[YourAttributeNameSpace].[YourAttributeTypeName], [YourAttributeAssemblyName]", there is already a TypeConverter that can be figured by .NET Configuration System. – Ethan Wu Jan 11 '12 at 09:55
  • 1
    For some reason it is not working for me. Its throwing InvalidOperation Exception. – surajnaik Jan 11 '12 at 10:20