Vasile Mare
Vasile Mare

Reputation: 189

Cannot find assembly

I'm using Autofac to dynamically load assemblies. It works all ok as long as the assemblies are in the same folder as the executing assembly.

But if the assembly that I want to dynamically load is in a subfolder then the app crashes with "Could not load file or assembly..."

I know, I can put these assemblies in GAC and then problem solved. But that's not what I want...

This is what I'm doing...

    var builder = new ContainerBuilder();
    builder.RegisterApiControllers(AppDomain.CurrentDomain.GetAssemblies());
    builder.RegisterModule(new ConfigurationSettingsReader());
    builder.Register(c => c.ResolveNamed<IMyInterface>(myVersion));

and also some configuration as well:

    autofac defaultAssembly="blah"
      modules
        module type="myVersionFile, blah"
      modules
    autofac

Any ideas on how to dynamically load assemblies located in a subfolder and not using GAC?

Thanks

Upvotes: 2

Views: 474

Answers (1)

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174427

You have several options:

  1. Add a probing path to your app.config pointing to the sub folder. This will only work if the assembly you are trying to load is somehow referenced in your project.
  2. You can specify the assemblies you want to load by other means. For example, I always load them by path and file name mask, i.e. all assemblies in a specific folder with a specific name pattern. For example, you can use code similar to this:

    var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, subFolder);
    var pattern = "YourProject.*.dll";
    var assemblies = Directory.GetFiles(path, pattern).Select(Assembly.LoadFrom);
    builder.RegisterApiControllers(assemblies);
    

Upvotes: 2

Related Questions