Unity Interception Exception / What am I doing wrong?

Mar 17, 2010 at 1:47 PM

Hi there,

The following is a part of the unity configuration section I am using in an application:

<unity>

        <typeAliases>

            <typeAlias alias="interceptionExtension" type="Microsoft.Practices.Unity.InterceptionExtension.Interception, Microsoft.Practices.Unity.Interception" />
            <typeAlias alias="vmethodInterceptor" type="Microsoft.Practices.Unity.InterceptionExtension.VirtualMethodInterceptor, Microsoft.Practices.Unity.Interception" />
                        
            <typeAlias alias="realLseeRepository" type="Dal.LicenseeRepository, Dal" />
            <typeAlias alias="licenseeManager" type="Biz.LicenseeManager, UnityInterception" />

        </typeAliases>

        <containers>
            <container>

                <types>

                    <type type="Dal.Common.Interfaces.ILicenseeRepository, Dal.Common" mapTo="realLseeRepository" />

                    <type type="Biz.LicenseeManager, UnityInterception" mapTo="licenseeManager" >
                        <typeConfig>
                            <constructor>
                                <param name="repository" parameterType="Dal.Common.Interfaces.ILicenseeRepository, Dal.Common">
                                    <dependency />
                                </param>
                            </constructor>
                        </typeConfig>
                    </type>
                    
                </types>

                <extensions>
                    <add type="interceptionExtension" />
                </extensions>
                
                <extensionConfig>
                    <add name="interception" type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationElement, Microsoft.Practices.Unity.Interception.Configuration">
                        <interceptors>
                            <interceptor type="vmethodInterceptor">
                                <key type="licenseeManager" />    
                            </interceptor>
                        </interceptors>
                    </add>
                </extensionConfig>

            </container>
        </containers>

    </unity>

In an attempt to implement interception with Unity Framework, as you can see in the section above, I have added  the extensions and extensionConfig elements.When i try to resolve the type "Biz.LicenseeManager, UnityInterception" the following exception is thrown:

Resolution of the dependency failed, type = "Biz.LicenseeManager", name = "". Exception message is: The current build operation (build key Build Key[Biz.LicenseeManager, null]) failed: The current build operation (build key Build Key[Biz.LicenseeManager, null]) failed: Value cannot be null.
Parameter name: str (Strategy type DynamicMethodConstructorStrategy, index 0) (Strategy type BuildPlanStrategy, index 5)

Without these two elements (extensions and extensionConfig) all would work fine (alas no interception). As well, if I were to use a parameterless constructor for the type "Biz.LicenseeManager, UnityInterception" all would work as expected.

For your convenience the following is the implementation of the class LicenseeManager:

	Public Class LicenseeManager
		
		Dim _repository As ILicenseeRepository

		Public Sub New(ByVal repository As ILicenseeRepository)
			_repository = repository
		End Sub


		<ExceptionCallHandler()> _
		Public Overridable Function BuggyMethod(ByVal id As Double) As Licensee

			If id < 0.5 Then
				Throw New DalException("Less than 0.5")
			Else
				Throw New ArgumentException("More than 0.5")
			End If

		End Function
	End Class

Any ideas suggestions are welcome!

Thanks!

Zen

 

 

Mar 18, 2010 at 5:16 AM

My first suggestion would be to dig further into the exception message. That top level one basically just says "something went wrong." The real exception message is several levels deeper. Could you post the entire exception message?

 

Mar 18, 2010 at 1:59 PM

Hey Chris,

Thank you very much for your prompt reply.

The exception is being thrown when trying to resolve the type:

Dim mgr As LicenseeManager = container.Resolve(Of LicenseeManager)()

The exception details (message and stack trace) including those of the corresponding inner exceptions are shown below:

 

Exception details

Type:
Microsoft.Practices.Unity.ResolutionFailedException

Message:
Resolution of the dependency failed, type = "Biz.LicenseeManager", name = "". Exception message is: The current build operation (build key Build Key[Biz.LicenseeManager, null]) failed: The current build operation (build key Build Key[Biz.LicenseeManager, null]) failed: Value cannot be null.
Parameter name: str (Strategy type DynamicMethodConstructorStrategy, index 0) (Strategy type BuildPlanStrategy, index 5)

Stack trace:    
at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name)
   at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, String name)
   at Microsoft.Practices.Unity.UnityContainer.Resolve(Type t, String name)
   at Microsoft.Practices.Unity.UnityContainerBase.Resolve(Type t)
   at Microsoft.Practices.Unity.UnityContainerBase.Resolve[T]()
   at Main.Main() in C:\Documents and Settings\a_saraqinis\My Documents\Visual Studio 2008\Projects\PlayGround\UnityInterception\Main.vb:line 29

 

 

Inner Exception I details    

Type:
Microsoft.Practices.ObjectBuilder2.BuildFailedException

Message:
The current build operation (build key Build Key[Biz.LicenseeManager, null]) failed: The current build operation (build key Build Key[Biz.LicenseeManager, null]) failed: Value cannot be null.
Parameter name: str (Strategy type DynamicMethodConstructorStrategy, index 0) (Strategy type BuildPlanStrategy, index 5)

Stack trace:
   at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)
   at Microsoft.Practices.ObjectBuilder2.Builder.BuildUp(IReadWriteLocator locator, ILifetimeContainer lifetime, IPolicyList policies, IStrategyChain strategies, Object buildKey, Object existing)
   at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name)

 

 

Inner Exception II details

Type:
Microsoft.Practices.ObjectBuilder2.BuildFailedException

Message:
The current build operation (build key Build Key[Biz.LicenseeManager, null]) failed: Value cannot be null.
Parameter name: str (Strategy type DynamicMethodConstructorStrategy, index 0)

Stack Trace:
   at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)
   at Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlanCreatorPolicy.CreatePlan(IBuilderContext context, Object buildKey)
   at Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context)
   at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)

 

 

Inner Exception III details

Type:
System.ArgumentNullException

Message:
Value cannot be null. Parameter name: str

Stack trace:
   at System.Reflection.Emit.DynamicILGenerator.Emit(OpCode opcode, String str)
   at Microsoft.Practices.ObjectBuilder2.DynamicMethodConstructorStrategy.PreBuildUp(IBuilderContext context)
   at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)

 

I hope this helps.

 

Thanks again!

Zen

Mar 22, 2010 at 12:40 PM

Anybody any idea?

 

Mar 26, 2010 at 6:55 PM
Edited Mar 26, 2010 at 6:57 PM

I would appreciate any help on this. Chris?

If you need further information, please let me know.

 

Zen

Mar 27, 2010 at 12:56 AM

Sorry I didn't get back to you sooner, I was presenting at a conference this week and had to prepare my talks.

This is with Unity 1.2, right? We had a bug in there where the virtual method interceptor didn't work properly on classes with constructor arguments. Since I don't see anything wrong with your config otherwise, and it works fine if you turn the interception off, or if you use a zero-arg constructor, that must be the issue. The original bug was work item 3696, which was closed back in August as fixed.

There's a source code patch in there for Unity 1.2, or you could grab 2.0 beta 2 now or wait a couple weeks for the final release of 2.0 on April 14th.

 

 

Mar 29, 2010 at 1:45 PM

Hey Chris,

Thank you very much for your help.That must have been the issue. All is working fine now.

Yes, I was using Unity 1.2. Looking forward to v2.0...

 

Cheers!

Zen