Search code examples
c#structuremap.net-assembly

StructureMap: 'AddAllTypesOf' not adding assembly to ObjectFactory?


I am trying to build a console app that loads an assembly at runtime which implements an interface. The point is to create a .dll with instructions that can be changed by changing the .dll.

My VS2012 solution exists of 3 projects: the interface, a library which implements it, and a console app. (Because of this, all three have their own namespace, maybe this is a problem?) The library and console app both have references to the interface project.

After adding the assemblies to the ObjectFactory, they are not found when using GetAllInstances. They are found when using AssembliesFromPath, but the adding does not seem to work.

Here's my code:

var dir = "..\\..\\..\\ComputeLibrary\\bin\\Debug";
ObjectFactory.Initialize(cfg =>
{
    cfg.Scan(scanner =>
    {
        scanner.AssembliesFromPath(dir);
        scanner.AddAllTypesOf(typeof(ICompute)).NameBy(i => i.Name);
    });
});
var list = ObjectFactory.GetAllInstances<ICompute>().ToList();
list.ForEach(item => item.Test());

In case anyone want to look at the entire solution, it is available here.

I'm probably doing something wrong here, but I can't seem to find what it is. Is this how it's supposed to be used? Or should I look towards MEF for doing something like this?


Solution

  • This puzzled me for a while, until I remembered this thread in the Structuremap google group.

    When you run your application, the ComputeInterface.dll assembly is loaded. When you scan the ComputeLibrary folder, the same assembly is loaded again and this is confusing Structuremap, since the interface can only be implemented from one assembly. Try deleting the ComputeInterface.dll from the ComputeLibrary\bin\Debug folder and it will work.