using System.Collections.Generic;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.InterceptionExtension;
public static void Main()
var container = new UnityContainer();
container.AddNewExtension<Interception>();
container.RegisterType<IService>(
new InjectionFactory(c => new Service()),
new InterceptionBehavior<PolicyInjectionBehavior>(),
new Interceptor<InterfaceInterceptor>());
Console.WriteLine("Resolve and call SayHello without interception registered");
var service = container.Resolve<IService>();
container.Configure<Interception>()
.AddPolicy("yourInterceptor")
.AddMatchingRule<TypeMatchingRule>
(new InjectionConstructor("Service", true))
.AddCallHandler<ServiceInterceptorHandler>(
new ContainerControlledLifetimeManager(),
new InjectionConstructor(),
new InjectionProperty("Order", 1));
Console.WriteLine("Resolve and call SayHello after interception registered");
var serviceWithInterceptors = container.Resolve<IService>();
serviceWithInterceptors.SayHello();
Console.WriteLine("Interception isn't applied to previous instance");
public interface IService
public class Service: IService
Console.WriteLine("Hello from Service");
public class ServiceInterceptorHandler : ICallHandler
public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
Console.WriteLine(String.Format(
"Invoking method {0} at {1}",
input.MethodBase, DateTime.Now.ToLongTimeString()));
var result = getNext().Invoke(input, getNext);
public IEnumerable<Type> GetRequiredInterfaces()
public int Order { get; set; }