Simeon Kredatus Simeon Kredatus - 3 months ago 31
C# Question

C# unity interception by attribute

Is there a way on how to use interception by attribute in C# unity and keep the object registration code in XML file(like app.config)? If yes, can you provide me with code, how should such registration look? I did a lot of workaround but did not find some working solution for this issue.


I'm assuming you mean using a custom attribute to indicate what methods to intercept. You can use policy injection to achieve interception using XML configuration.

First, let's define a custom attribute:

public class MyInterceptionAttribute : Attribute

Next we can create an ICallHandler to do some interception work. This implementation will just do a Console.WriteLine before and after the method:

public class MyLoggingCallHandler : ICallHandler
    IMethodReturn ICallHandler.Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
        Console.WriteLine("Invoking " + input.MethodBase.Name);
        IMethodReturn result = getNext()(input, getNext);
        Console.WriteLine("Done Invoke");
        return result;

    int ICallHandler.Order { get; set; }

Next let's assume that we have some interface and an implementation:

public interface IMyClass
    void Do();
    void DoAgain();

public class MyClass : IMyClass 
    public void Do()

    public void DoAgain()
        Console.WriteLine("Do Again!");

Notice that I've applied the custom attribute, MyInterception, to only the Do method but not the DoAgain method. We will intercept all calls to the Do method.

Next we create the configuration to define a policy, configure the matching rule and register the type along with an interceptor:

<?xml version="1.0"?>
    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>
  <unity xmlns="">
    <namespace name="UnityCallHandlerConfig" />
    <assembly name="UnityCallHandlerConfig"  />
    <sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension, Microsoft.Practices.Unity.Interception.Configuration"/>
      <extension type="Interception"/>
        <policy name="Policy">
          <matchingRule name="Match" type="Microsoft.Practices.Unity.InterceptionExtension.CustomAttributeMatchingRule, Microsoft.Practices.Unity.Interception">
              <param name="attributeType" value="UnityCallHandlerConfig.MyInterceptionAttribute, UnityCallHandlerConfig" typeConverter="AssemblyQualifiedTypeNameConverter" />
              <param name="inherited">
                <value value="false"/>
          <callHandler name="MyLogging" type="MyLoggingCallHandler">
            <lifetime type="singleton"/>
      <register type="IMyClass" mapTo="MyClass">
        <interceptor type="InterfaceInterceptor"/>
        <interceptionBehavior type="PolicyInjectionBehavior"/>

    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>

We also need a type converter to convert the string representation of the custom attribute to the proper type:

public class AssemblyQualifiedTypeNameConverter : ConfigurationConverterBase
    public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
        if (value != null)
            Type typeValue = value as Type;
            if (typeValue == null)
                throw new ArgumentException("Cannot convert type", typeof(Type).Name);

            if (typeValue != null) return (typeValue).AssemblyQualifiedName;
        return null;

    public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        string stringValue = (string)value;
        if (!string.IsNullOrEmpty(stringValue))
            Type result = Type.GetType(stringValue, false);
            if (result == null)
                throw new ArgumentException("Invalid type", "value");

            return result;
        return null;

Once we have all that set up we can create a container and load the configuration:

var container = new UnityContainer().LoadConfiguration();

var myClass = container.Resolve<IMyClass>();

The output will be:

Invoking Do
Done Invoke
Do Again!

showing that the first method is intercepted while the second is not.