Unity 1.2 - Trouble resolving private classes

Nov 25, 2008 at 7:14 PM
Edited Nov 25, 2008 at 7:18 PM
Please pardon my ignorance, but has Unity 1.2 changed regarding the visibility requirements for classes to be resolved ?

I used to be able to resolve private classes nested in a public test class, like this:

public class TestUnityWorks
{
        private class Class1
        {
            :
        }

        :

        [Test]
        public void TestPrivateClassResolve()
        {
            Class1 subscriber = container.Resolve<Class1>();
            Assert.IsNotNull(subscriber);
        }
}

But, I'm getting exceptions like these:

TestUnityWorks.TestPrivateClassResolve : FailedSystem.MethodAccessException: Class1..ctor()
at BuildUp_Terma.TMission.Fundamentals.Communication.EventBroker.Tests.TestUnityWorks+Class1(IBuilderContext )
at Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlan.BuildUp(IBuilderContext context)
at Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context)
at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)
Microsoft.Practices.ObjectBuilder2.BuildFailedException: The current build operation (build key Build Key[TestUnityWorks+Class1, null]) failed: Class1..ctor() (Strategy type BuildPlanStrategy, index 4)
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)
Microsoft.Practices.Unity.ResolutionFailedException: Resolution of the dependency failed, type = "TestUnityWorks+Class1", name = "". Exception message is: The current build operation (build key Build Key[TestUnityWorks+Class1, null]) failed: Class1..ctor() (Strategy type BuildPlanStrategy, index 4)
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 .....

If I change Class1's visibility to public the test passes.

Regards,
Morten

PS: Perhaps this has something to do with Unity 1.2 trying to embrace SilverLight security ?
Nov 25, 2008 at 9:47 PM
Yes, this was a change. I put up a forum post asking for people's opinions about this, and nobody complained so we went ahead with it.

The reason was about security, but it was more about supporting partial trust scenarios than it was about Silverlight. Basically, some of the things we wanted to do just wouldn't work in partial trust. The tradeoff was that we could no longer inject private classes.

Nov 26, 2008 at 9:39 AM
Ok, it seems like a reasonable tradeoff. Thanks for clarifying.

/Morten
Jan 30, 2009 at 7:53 PM
Hi,

we require creation of private/internal types also since we just dependency injection internally.

Thanks
Jan 30, 2009 at 7:53 PM
Hi,

we require creation of private/internal types also since we just dependency injection internally.

Thanks
Feb 2, 2009 at 7:24 PM
Internal types should work if you're running in full trust. Private types will not. We have no plans to work on injection of private types; the security model won't let it work.

Feb 2, 2009 at 9:30 PM
Thanks, works fine for public and internal types!