简体   繁体   English

找不到程序集

[英]Cannot find assembly

I'm using Autofac to dynamically load assemblies.我正在使用 Autofac 动态加载程序集。 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.我知道,我可以将这些程序集放在 GAC 中,然后问题就解决了。 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?关于如何动态加载位于子文件夹中的程序集而不使用 GAC 的任何想法?

Thanks谢谢

You have several options:您有多种选择:

  1. Add a probing path to your app.config pointing to the sub folder.向您的 app.config 添加一个指向子文件夹的探测路径 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, ie 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM