Does Unity work in a Medium Trust environment?

Mar 25, 2008 at 6:48 AM
I gotta get to sleep, so I haven't had time to really dig into this, but...

I tried uploading a MVC framework experiment project to my shared host, which is a IIS7 running .NET 3.5 environment. I received the following error:

[VerificationException: Operation could destabilize the runtime.]
BuildUp_sbCommunity.Core.Infrastructure.DLinqUnitOfWork(IBuilderContext ) +6
Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlan.BuildUp(IBuilderContext context) in DynamicMethodBuildPlan.cs:37
Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context) in BuildPlanStrategy.cs:40
Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context) in StrategyChain.cs:92

[BuildFailedException: The current build operation failed on strategy type Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy, index 2 for build key Build Key[sbCommunity.Core.Infrastructure.DLinqUnitOfWork, null]]
Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context) in StrategyChain.cs:109
Microsoft.Practices.ObjectBuilder2.Builder.BuildUp(IReadWriteLocator locator, ILifetimeContainer lifetime, IPolicyList policies, IStrategyChain strategies, Object buildKey, Object existing) in Builder.cs:61
Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name) in UnityContainer.cs:449

[ResolutionFailedException: Resolution of the dependency failed, type = "IUnitOfWork", name = "". See the InnerException for more details.]
Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name) in UnityContainer.cs:459
Microsoft.Practices.Unity.UnityContainer.Resolve(Type t, String name) in UnityContainer.cs:146
Microsoft.Practices.Unity.UnityContainerBase.Resolve() in UnityContainerBase.cs:394
sbCommunity.Core.TagUtility.GetCloud(Int32 listSize) in TagUtility.cs:20
sbCommunity.Controllers.HomeController.Index() in HomeController.cs:26

Anyway, I just yanked all of the Unity stuff out and created the objects directly and it worked.
Mar 25, 2008 at 6:26 PM
This isn't a medium trust issue, it's looks like bad IL is being generated.

Is there any chance you could send me the code for the sbCommunity.Core.Infrastructure.DLinqUnitOfWork class? There's something there we're not handling correctly.
Mar 25, 2008 at 7:38 PM
Hmm, on second thought, it might be a medium trust issue. It should work in medium trust, especially on VS2008 (which, if you're using MVC, I assume your host is running?)

If running on a 2.0 box, you need to add the "generate dynamic IL" permission to the medium trust set. This was done by default with the release of .NET 3.5.

In any event, if you could send me a small repro case that would really help in tracking this down. It should work.
Mar 27, 2008 at 12:36 AM
We found the issue. It turned out to be both IL generation and medium trust. The IL I was generating was just slightly off, and wasn't verifiable, but still worked. In full trust, IL verification is skipped, so it worked, but blew up in lower-trust environments.

The fix will be in the next drop, but until then you can edit the source. The fix is in Src\ObjectBuilder\Strategies\BuildPlan\DynamicMethod\DynamicBuildPlanGenerationContext.cs.

Change line 221 from:

existingObjectLocal = il.DeclareLocal(typeToBuild);

to

existingObjectLocal = il.DeclareLocal(typeof(object));

and that will fix the verification problem.
Mar 27, 2008 at 3:42 AM
Awesome! I made the change locally and validated that it worked.

Thanks