Interception Problem when using VS 2010

Sep 18, 2009 at 6:26 PM
Edited Sep 18, 2009 at 6:33 PM

 

I was using interception without issues and then rebuilt my project with studio 2010. At that point I started getting an exception from interface interception, and when I looked into it some more, I'm not sure why it worked before the upgrade. The trace I'm getting is:

FailedSystem.ArgumentException: Cannot set parent to an interface.

at System.Reflection.Emit.TypeBuilder.SetParent(Type parent)
at System.Reflection.Emit.GenericTypeParameterBuilder.SetBaseTypeConstraint(Type baseTypeConstraint)
at Microsoft.Practices.Unity.InterceptionExtension.InterfaceMethodOverride.SetupGenericParameters(MethodBuilder methodBuilder) in e:\Builds\Unity\UnityTemp\Compile\Unity\Src\Unity.Interception\Interceptors\InstanceInterceptors\InterfaceInterception\InterfaceMethodOverride.cs: line 68
at Microsoft.Practices.Unity.InterceptionExtension.InterfaceMethodOverride.CreateDelegateImplementation() in e:\Builds\Unity\UnityTemp\Compile\Unity\Src\Unity.Interception\Interceptors\InstanceInterceptors\InterfaceInterception\InterfaceMethodOverride.cs: line 144
at Microsoft.Practices.Unity.InterceptionExtension.InterfaceInterceptorClassGenerator.CreateProxyType() in e:\Builds\Unity\UnityTemp\Compile\Unity\Src\Unity.Interception\Interceptors\InstanceInterceptors\InterfaceInterception\InterfaceInterceptorClassGenerator.cs: line 66
at Microsoft.Practices.Unity.InterceptionExtension.InterfaceInterceptor.CreateProxy(Type t, Object target) in e:\Builds\Unity\UnityTemp\Compile\Unity\Src\Unity.Interception\Interceptors\InstanceInterceptors\InterfaceInterception\InterfaceInterceptor.cs: line 82
at Microsoft.Practices.Unity.InterceptionExtension.InstanceInterceptionStrategy.PostBuildUp(IBuilderContext context) in e:\Builds\Unity\UnityTemp\Compile\Unity\Src\Unity.Interception\Interceptors\InstanceInterceptors\InstanceInterceptionStrategy.cs: line 51
at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context) in e:\Builds\Unity\UnityTemp\Compile\Unity\Src\ObjectBuilder\Strategies\StrategyChain.cs: line 102 
Microsoft.Practices.ObjectBuilder2.BuildFailedException: The current build operation (build key Build Key[Ptc.Platform.Nds.Piv.PIVManager, null]) failed: Cannot set parent to an interface. (Strategy type InstanceInterceptionStrategy, index 0)
at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context) in e:\Builds\Unity\UnityTemp\Compile\Unity\Src\ObjectBuilder\Strategies\StrategyChain.cs: line 109
at Microsoft.Practices.ObjectBuilder2.Builder.BuildUp(IReadWriteLocator locator, ILifetimeContainer lifetime, IPolicyList policies, IStrategyChain strategies, Object buildKey, Object existing) in e:\Builds\Unity\UnityTemp\Compile\Unity\Src\ObjectBuilder\Builder.cs: line 61
at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name) in e:\Builds\Unity\UnityTemp\Compile\Unity\Src\Unity\UnityContainer.cs: line 463 
Microsoft.Practices.Unity.ResolutionFailedException: Resolution of the dependency failed, type = "Ptc.Platform.Nds.Piv.IPIVManager", name = "". Exception message is: The current build operation (build key Build Key[Ptc.Platform.Nds.Piv.PIVManager, null]) failed: Cannot set parent to an interface. (Strategy type InstanceInterceptionStrategy, index 0)
at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name) in e:\Builds\Unity\UnityTemp\Compile\Unity\Src\Unity\UnityContainer.cs: line 473
at Microsoft.Practices.Unity.UnityContainer.Resolve(Type t, String name) in e:\Builds\Unity\UnityTemp\Compile\Unity\Src\Unity\UnityContainer.cs: line 155
at Microsoft.Practices.Unity.UnityContainerBase.Resolve(Type t) in e:\Builds\Unity\UnityTemp\Compile\Unity\Src\Unity\UnityContainerBase.cs: line 488

When I looked into this some more, the code currently looks as follows:

 

                for(int i = 0; i < genericArguments.Length; ++i)
                {
                    builders[i].SetGenericParameterAttributes(genericArguments[i].GenericParameterAttributes);

                    foreach (Type type in genericArguments[i].GetGenericParameterConstraints())
                    {
                        builders[i].SetBaseTypeConstraint(type);
                    }
                }

I changed it to:

 

                for(int i = 0; i < genericArguments.Length; ++i)
                {
                    builders[i].SetGenericParameterAttributes(genericArguments[i].GenericParameterAttributes);
                    List<Type> ifcConstraints = null;
                    foreach (Type type in genericArguments[i].GetGenericParameterConstraints())
                    {
                        if (type.IsClass)
                        {
                            builders[i].SetBaseTypeConstraint(type);
                        }
                        else
                        {
                            if (ifcConstraints == null)
                            {
                                ifcConstraints = new List<Type>();
                            }
                            ifcConstraints.Add(type);
                        }
                    }
                    if (ifcConstraints != null)
                    {
                        builders[i].SetInterfaceConstraints(ifcConstraints.ToArray());
                    }                    
                }
            }

Then things work. I'm kind of concerned I'm missing something though, since the code used to work OK the old way, even though the old way seems wrong to me. Should I put something in the issue tracker, or what am I doing wrong? Thx!

 

 

Sep 7, 2010 at 8:30 AM

I have got the exact same problem : using Unity for DI. And when I configured the container for interception (using Virtual method injection), I get the same exact problem at the same place when wanting to register a generic type for injection and interception

The proposed solution seems to do the trick. However any fix published "officially" in the main Unity source code stream?

Thanks in advance 

Sep 7, 2010 at 6:09 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Sep 7, 2010 at 6:10 PM

Sounds like it may be a real bug. I've added an issue to the bug tracker so we don't lose track. No promises on when there will be a fix though.