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:
[AttributeUsage(AttributeTargets.Method)]
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
{
[MyInterception]
public void Do()
{
Console.WriteLine("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"?>
<configuration>
<configSections>
<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>
</configSections>
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<namespace name="UnityCallHandlerConfig" />
<assembly name="UnityCallHandlerConfig" />
<sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension, Microsoft.Practices.Unity.Interception.Configuration"/>
<container>
<extension type="Interception"/>
<interception>
<policy name="Policy">
<matchingRule name="Match" type="Microsoft.Practices.Unity.InterceptionExtension.CustomAttributeMatchingRule, Microsoft.Practices.Unity.Interception">
<constructor>
<param name="attributeType" value="UnityCallHandlerConfig.MyInterceptionAttribute, UnityCallHandlerConfig" typeConverter="AssemblyQualifiedTypeNameConverter" />
<param name="inherited">
<value value="false"/>
</param>
</constructor>
</matchingRule>
<callHandler name="MyLogging" type="MyLoggingCallHandler">
<lifetime type="singleton"/>
</callHandler>
</policy>
</interception>
<register type="IMyClass" mapTo="MyClass">
<interceptor type="InterfaceInterceptor"/>
<interceptionBehavior type="PolicyInjectionBehavior"/>
</register>
</container>
</unity>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
</startup>
</configuration>
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>();
myClass.Do();
myClass.DoAgain();
The output will be:
Invoking Do
Do!
Done Invoke
Do Again!
showing that the first method is intercepted while the second is not.
This is an old question but was really useful to me so I'll add the C# version of the unity configuration.
container.Configure<Interception>()
.AddPolicy("LoggingPolicy")
.AddMatchingRule(new CustomAttributeMatchingRule(typeof(MyInterceptionAttribute), false))
.AddCallHandler<MyLoggingCallHandler>();
container.RegisterType<IMyClass , MyClass>(new Interceptor<InterfaceInterceptor>(), new InterceptionBehavior<PolicyInjectionBehavior>());
© 2022 - 2024 — McMap. All rights reserved.
AssemblyQualifiedTypeNameConverter
if it's done in code? – Yetac