Tutorial :StructureMap Assembly scanning issue


I am trying to use StructureMap to scan at runtime for assemblies that contain an implementation of the Registry class, but I'm running into a problem.

If a dll contains a Registry class, but also contains a reference to a dll that isn't present at runtime (say a Rhino.Mocks dll that isn't required at runtime), StructureMap will throw a StructureMapConfiguration exception resulting from an Assembly.GetExportedTypes() call.

Is there a way to avoid this behaviour in StructureMap?

ObjectFactory.Initialize(x =>  {      x.Scan(s =>      {          s.AssembliesFromApplicationBaseDirectory();          s.LookForRegistries();      });  });  


Through reflector i found that with the call to graph.Log.AssertFailures() any exceptions that may have occurred during assembly scanning (that includes the ones that are thrown during assembly.GetExportedTypes() which are the ones that you are referring to) are thrown as one big exception. I havent found anything indicating that this behavior can be somehow switched off. What I found out though is that if you catch the exception, the scanning actually succeeds with any assemblies that it managed to scan. StructureMap cannot actually do anything about exceptions being thrown when calling GetExportedTypes() (which is a framework method) except continue to the next assembly. If you dont mind 'losing' some assemblies just catch the exception and move on.

public void Configure(Action configure) { lock (this) { ConfigurationExpression expression = new ConfigurationExpression(); configure(expression); PluginGraph graph = expression.BuildGraph(); graph.Log.AssertFailures(); this._interceptorLibrary.ImportFrom(graph.InterceptorLibrary); this._pipelineGraph.ImportFrom(graph); } }

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »