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)
The
Unityversion you have isx32. Your project assemblies are being built asx64, 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 ofx86.64-bit assemblies calling 32-bit assemblies is one of the most common reasons for
BadImageFormatException.MSDN notes:
More information here.