InjectionFactory and InterfaceInterceptor

Aug 26, 2010 at 8:44 AM

  I have trouble using InjecttionFactory together with interception. The following code example throws an exception on the last line:

Dim container As New UnityContainer()
container.AddNewExtension(Of Interception)()
container.RegisterType(Of ITestInterface)(New InjectionFactory(Function(c) New TestClass))
container.RegisterType(Of ITestInterface)(New DefaultInterceptor(New InterfaceInterceptor), New DefaultInterceptionBehavior(Of UnityTestInterceptor)())
container.Resolve(Of ITestInterface)()


Test 'MyTestClass.Test' failed: Test method MyTestClass.Test threw exception:

Microsoft.Practices.Unity.ResolutionFailedException: Resolution of the dependency failed, type = "ITestInterface", name = "(none)".

Exception occurred while: while resolving.

Exception is: InvalidOperationException - The current type, ITestInterface, is an interface and cannot be constructed. Are you missing a type mapping?


At the time of the exception, the container was:

Resolving ITestInterface,(none)

---> System.InvalidOperationException: The current type, ITestInterface, is an interface and cannot be constructed. Are you missing a type mapping?

ObjectBuilder\Strategies\BuildPlan\DynamicMethod\Creation\DynamicMethodConstructorStrategy.cs(209,0): at Microsoft.Practices.ObjectBuilder2.DynamicMethodConstructorStrategy.ThrowForAttemptingToConstructInterface(IBuilderContext context)

at BuildUp_ITestInterface(IBuilderContext )

ObjectBuilder\Strategies\BuildPlan\DynamicMethod\DynamicMethodBuildPlan.cs(37,0): at Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlan.BuildUp(IBuilderContext context)

ObjectBuilder\Strategies\BuildPlan\BuildPlanStrategy.cs(43,0): at Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context)

ObjectBuilder\Strategies\StrategyChain.cs(110,0): at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)

UnityContainer.cs(511,0): at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name, IEnumerable`1 resolverOverrides)

--- End of inner exception stack trace ---

UnityContainer.cs(515,0): at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name, IEnumerable`1 resolverOverrides)

UnityContainer.cs(485,0): at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, String name, IEnumerable`1 resolverOverrides)

UnityContainer.cs(173,0): at Microsoft.Practices.Unity.UnityContainer.Resolve(Type t, String name, ResolverOverride[] resolverOverrides)

D:\Div\Ext\UnitySource\Source\Unity\Src\UnityContainerExtensions.cs(504,0): at Microsoft.Practices.Unity.UnityContainerExtensions.Resolve[T](IUnityContainer container, ResolverOverride[] overrides)




Aug 27, 2010 at 2:08 AM

With the code you posted, you are actually overriding the first registration with the second one, thus, your factory method won't be used when resolving for ITestInterface.  Your registration should be like:

container.RegisterType(Of ITestInterface, TestClass)(New InjectionFactory(Function(c) New TestClass), _
	New DefaultInterceptor(New InterfaceInterceptor), New DefaultInterceptionBehavior(Of UnityTestInterceptor)())

Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.

Aug 30, 2010 at 10:03 PM

I have noticed that the code also works if I change the order of my registrations (line 3 and 4 in my previous example). This works:

Dim container As New UnityContainer()
container.AddNewExtension(Of Interception)()
container.RegisterType(Of ITestInterface)(New DefaultInterceptor(New InterfaceInterceptor), New DefaultInterceptionBehavior(Of UnityTestInterceptor)())
container.RegisterType(Of ITestInterface)(New InjectionFactory(Function(c) New TestClass))
container.Resolve(Of ITestInterface)()

Is this expected behavior?

How can I correctly first register a factory, and then later if needed dynamically add an interceptor?



Tore Amundsen

Sep 1, 2010 at 1:36 AM

The way Unity works is that the last registration wins.  That code will not throw any exception but the resolved instance of ITestInterface won't be intercepted since the last registration you made override the previous one. 

You may consider using the stand-alone interception feature of Unity 2.0.  You'll register the type with a factory and if you need it to be intercepted, perform interception on the resolved instance from the container.  Here's a link to the stand-alone interception in the documentation.


Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.