I'd like to use interception with Unity, here is my code :
UnityContainer container = new UnityContainer();
container.AddNewExtension<Interception>();
container.RegisterType<T, T>();
container.Configure<Interception>().SetDefaultInterceptorFor<T>(new VirtualMethodInterceptor());
return container.Resolve<T>();
If T is a class with a constructor with parameters (an an empty constructor) an exception is thrown when I call Resolve, else it works. How can I intercept a type who has a non empty constructor ?
Update
UnityContainer container = new UnityContainer();
container.AddNewExtension<Interception>();
container.RegisterType<T, T>();
container.Configure<InjectedMembers>().ConfigureInjectionFor<T>(new InjectionConstructor());
container.Configure<Interception>().SetDefaultInterceptorFor<T>(new VirtualMethodInterceptor());
return container.Resolve<T>();
This code works, but what if I'd like to use a constructor with argument ?
I've tried this :
public static T Resolve<T>(object param)
{
UnityContainer container = new UnityContainer();
container.AddNewExtension<Interception>();
container.RegisterType<T, T>();
container.Configure<InjectedMembers>().ConfigureInjectionFor<T>(new InjectionConstructor(param));
container.Configure<Interception>().SetDefaultInterceptorFor<T>(new VirtualMethodInterceptor());
return container.Resolve<T>();
}
And in my code :
var service = Resolve<MyService>(4);
And I'm back with the same exception as earlier...