Sunday, November 18, 2012

Could not load type - The Structuremap Error

Recently I got an structuremap error when I launch my application. The error message as follows
Error Message: Exception has been thrown by the target of an invocation. - 0%System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> 
System.TypeLoadException: Could not load type 'MetricsEngine.ApplicationServices.Metrics.IMetricService' from assembly 'MetricsEngine, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.

at WebApplication6.Bootstrapper.Registries.ClinicalIntegrationRegistry.<.ctor>b__0(IAssemblyScanner scanner)
at StructureMap.Configuration.DSL.Registry.Scan(Action`1 action) in c:\BuildAgent\work\767273992e840853\src\StructureMap\Configuration\DSL\Registry.cs:line 250
at WebApplication6.Bootstrapper.Registries.ClinicalIntegrationRegistry..ctor() in E:\Src\SolutionFolder\WebApplication6.Bootstrapper\Registries\MetricsRegistry.cs:line 33
--- End of inner exception stack trace ---
at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache)
at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean skipCheckThis, Boolean fillCache)
at System.Activator.CreateInstance(Type type, Boolean nonPublic)
at StructureMap.Graph.PluginGraph.ImportRegistry(Type type) in c:\BuildAgent\work\767273992e840853\src\StructureMap\Graph\PluginGraph.cs:line 232
at System.Collections.Generic.List`1.ForEach(Action`1 action)
at StructureMap.Configuration.DSL.Registry.ConfigurePluginGraph(PluginGraph graph) in c:\BuildAgent\work\767273992e840853\src\StructureMap\Configuration\DSL\Registry.cs:line 397
at StructureMap.Graph.AssemblyScanner.ScanForAll(PluginGraph pluginGraph) in c:\BuildAgent\work\767273992e840853\src\StructureMap\Graph\AssemblyScanner.cs:line 249
at StructureMap.Graph.PluginGraph.Seal() in c:\BuildAgent\work\767273992e840853\src\StructureMap\Graph\PluginGraph.cs:line 121
at StructureMap.PluginGraphBuilder.Build() in c:\BuildAgent\work\767273992e840853\src\StructureMap\PluginGraphBuilder.cs:line 72
at StructureMap.ObjectFactory.Initialize(Action`1 action) in c:\BuildAgent\work\767273992e840853\src\StructureMap\ObjectFactory.cs:line 65
at WebApplication6.Bootstrapper.Bootstrapper.RegisterContainer() in E:\Src\SolutionFolder\WebApplication6.Bootstrapper\Bootstrapper.cs:line 27
at WebApplication6.Bootstrapper.Bootstrapper.Bootstrap() in E:\Src\SolutionFolder\WebApplication6.Bootstrapper\Bootstrapper.cs:line 20

Reason:

My project looks very similar to the following picture.
In the above picture, I am referencing two different dlls from two different sources with the same name "MetricsEngine.dll". Now during the bootstrap the structuremap supposed to load it's referenced assembly(0.0.4696.21382) which is from vendor1, but it loads the assembly (1.0.0.0) from vendor2 which is referenced by WebApplication6.EngineServices. Though both the assemblies had different public key token and different version, it always loads the one from vendor2. The reason is structuremap uses reflection to load the assemblies dynamically at runtime; it scans though current project's referenced assemblies and it's references and so forth. In that way assembly MetricsEngine.dll from vendor1 and vendor2 are considered by the structuremap, now it picks the latest version, in our case it picks 1.0.0.0. It doesn't care about assembly's identity (public key token).

Temporary Solution:

I tried, But I couldn't figure out the right solution. For time being after downloading the assembly I renamed the assembly manually and referencing it in my project. I uninstalled that package from NuGet. That is, I am not using NuGet for one of the referencing assembly. Once I figure out the solution I will update this post.

No comments:

Post a Comment