Constructor injection in an intercepted type

Jun 21, 2009 at 1:06 PM
Edited Jun 21, 2009 at 1:08 PM

Hi,

I have a class with two constructors and one virtual method with an HandlerAttribute :

 

 

public class A
{

	public A()
	{
		....
	}
	public A(int a)
	{
		....
	}
	[SomeHandler]
	public virtual void Toaaa()
	{

	}
}

And I'd like to inject the constructor's parameter 'a'.

So I tried :

 

static void Main(string[] args)
{
	var service = Resolve<A>(6);
}

public static T Resolve<T>(object param)
{
	UnityContainer container = new UnityContainer();
	container.AddNewExtension<Interception>();
	container.RegisterType<T>(new InjectionConstructor(param));
	container.Configure<Interception>().SetDefaultInterceptorFor<T>(new VirtualMethodInterceptor());
	return container.Resolve<T>();
}

But the Resolve method throw a ResolutionFailedException.
Am I doing something wrong or is this a bug of Unity ?
Thanks for your responses
Note: It works fine with InterfaceInterceptor, but I really need to use VirtualMethodInterceptor.

 

 

Jun 22, 2009 at 1:50 PM

If I'm not mistaken, this is a bug in Unity wherein the VirtualMethodInterceptor doesn't work if the constructor you used for the type to be intercepted has parameters.  I believe this is the same as with the issue logged before - http://unity.codeplex.com/WorkItem/View.aspx?WorkItemId=3696

 

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

 

Jun 22, 2009 at 8:03 PM

Thanks, it's the same issue !