将 .dll 加载到单独的应用程序域 (MEF)

Loading .dll into separate application domain (MEF)

我正在尝试将程序集加载到单独的应用程序域中。 我不想创建任何实例或执行程序集。所以我没有使用 CreateInstanceAndUnwrap 方法。我只想加载 dll 文件(不从中创建任何类型等)

        var cat = new AggregateCatalog();
        pluginApplicationDomain = AppDomain.CreateDomain("pluginApplicationDomain", AppDomain.CurrentDomain.Evidence,
        appDomainInfo);

        cat.Catalogs.Add(new AssemblyCatalog(pluginApplicationDomain.Load(File.ReadAllBytes(plugin))));

而plugin是dll文件的相对路径。

../../Extensions/Plugins\MyAssembly.dll

这会抛出“System.IO.FileNotFoundException” 路径是正确的,如果我这样做 Assembly.LoadFrom(plugin),它不会抛出任何异常,所以我猜这不是不正确的路径。

其他解决方案都使用了 CreateInstanceAndUnwrap,但是如果程序集及其类型对我来说是黑盒怎么办?我知道它实现了一个界面,仅此而已?

Load dll into another domain exception

这行得通吗?但是我的方法签名与 CrossAppDomainDelegate

中的签名不匹配

MEF 更多的是关于可扩展性,看起来你 实际上正在寻找 [MAF]1. Check out this SO question. From my personal experience if your plugins going to invoke some native code (interact with hardware for example) AppDomains will not prevent crashes. It's safer to host such plugins in separate processes.