Question

I'm using Unity 2.1 in my application and receive the error in the title for one resolving scenario. I have no idea on where to look.

  • I'm running Windows 7 64bit
  • 32-bit applications are enabled
  • Tried building against "Any CPU" and "x64"

The problem doesn't seem to be related to the 64bit architecture. Any assistance would be greatly appreciated!

Scenario's

//works: class = "ProductManager<Product>
Container.Resolve<IProductManager<Product>>()

//works: class = "OrderManager"
Container.Resolve<IOrderManager()

//works: class="OrderManager" 
Container.Resolve<IOrderManager("OrderManager")

//DOESN'T WORK: EXCEPTION: BadImageFormatException
Container.Resolve<IOrderManager("OrderManager") 

//works: class="GenericOrderManager<Order>" (obviously)
var manager = new GenericOrderManager<Order>();

Code

Unity.config

<alias name="IProductManager" type="Assembly1.Namespace.IProductManager`1" />
<alias name="ProductManager" type="Assembly2.Namespace.ProductManager`1" />

<alias name="IOrderManager" type="Assembly1.Namespace.IOrderManager" />
<alias name="OrderManager" 
       type="Assembly1.Namespace.OrderManager" />
<alias name="OrderManager" 
       type="Assembly1.Namespace.OrderManager" 
       name="OrderManager" />
<alias name="GenericOrderManager" 
       type="Assembly2.Namespace.GenericOrderManager`1"
       name="GenericOrderManager" />

ProductManager + Interface

public interface IProductManager<TProduct> where TProduct : Product
{
}

public class ProductManager<TProduct> : IProductManager<TProduct> where TProduct : Product
{
}

OrderManager + Interface

public interface IOrderManager
{
}

public class OrderManager : IOrderManager
{
}

public class OrderManager<TOrder> : OrderManager where TOrder : Order
{
}

Update with StackTrace:

at System.Runtime.CompilerServices.RuntimeHelpers._CompileMethod(IRuntimeMethodInfo method) at System.Reflection.Emit.DynamicMethod.CreateDelegate(Type delegateType) at Microsoft.Practices.ObjectBuilder2.DynamicBuildPlanGenerationContext.GetBuildMethod() at Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlanCreatorPolicy.CreatePlan(IBuilderContext context, NamedTypeBuildKey buildKey) at Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context) at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context) at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name, IEnumerable`1 resolverOverrides)

Was it helpful?

Solution

The Unity version you have is x32. Your project assemblies are being built as x64, but have the 32-bit Unity assemblies as references. Unfortunately, the compile will go fine. You will get a nasty surprise at run-time, though. Bottom line: compile with platform target of x86.

64-bit assemblies calling 32-bit assemblies is one of the most common reasons for BadImageFormatException.

MSDN notes:

A DLL or executable is loaded as a 64-bit assembly, but it contains 32-bit features or resources. For example, it relies on COM interop or calls methods in a 32-bit dynamic link library.

To address this exception, set the project's Platform target property to x86 (instead of x64 or AnyCPU) and recompile.

More information here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top