pluginsstructuremapstructuremap4

StructureMap could not load assembly from file in a plugin framework


I'm using StructureMap to load Plugins at the start of my application. At application startup, when I create the container I do a simple scan like:

internal static IContainer Init()
{
    var container = new Container(a =>
    {
        a.Scan(scan =>
        {
            scan.TheCallingAssembly();
            scan.WithDefaultConventions();
            scan.AssembliesFromPath($"{AppDomain.CurrentDomain.BaseDirectory}Plugins");
        });
    });

    ConfigureDebugSubstitutions(container);

    return container;
}

But I see these errors in the console:

StructureMap could not load assembly from file <filepath>

I end up getting around it currently by manually loading the files into the AppDomain like this:

private static void LoadPluginAssemblies()
{
    var pluginAssemblies = Directory.GetFiles($"{AppDomain.CurrentDomain.BaseDirectory}plugins", "*.dll");
    pluginAssemblies.Each(a =>
    {
        Assembly.LoadFile(a);
    });
}

But I'm curious why I'm getting this message from structuremap to begin with. I looked at the code, and it looks like any errors are simply getting handled. Any thoughts? I'm using version 4.0.1.318 and I didn't notice this until I upgraded StructureMap from version 3.1.4.143. Also, I'm using .Net v4.6.1 if it matters.


Solution

  • Looks like it might have been a bug.

    So, yes, I know why. It's trying to load the assembly by name directly from the AppDomain, which is really more efficient and less error prone than loading by the file path. Fine and good, but it should be trying to fallback to loading by the file in your case. I'm addressing this right now and it'll be in 4.1.

    https://github.com/structuremap/structuremap/issues/451