将 DLL 加载到单独的 AppDomain 中
我正在编写一个插件架构。我的插件 dll 位于运行插件管理器的子目录中。 我将插件加载到单独的 AppDomain 中,如下所示:
string subDir;//initialized to the path of the module's directory.
AppDomainSetup setup = new AppDomainSetup();
setup.PrivateBinPath = subDir;
setup.ApplicationBase = subDir;
AppDomain newDomain= AppDomain.CreateDomain(subDir, null, setup);
byte[] file = File.ReadAllBytes(dllPath);//dll path is a dll inside subDir
newDomain.Load(file);
但是。 newDomain.Load 返回当前域尝试加载的程序集。因为插件 dll 位于子目录中,所以当前域不能也不应该看到这些 dll,并且当前域会抛出 FileLoadException "ex = {"无法加载文件或程序集...或其依赖项之一。"
问题是,我们可以将程序集加载到单独的 AppDomain 中而不返回加载的程序集吗?
我知道我可以为当前域中的 AssemblyResolve 事件并返回 null,但我不想走这条路
。
I am writing a plugin architecture. My plugin dlls are located in a sub directory from where the plugin manager is running.
I am loading the plugins into a separate AppDomain as the following:
string subDir;//initialized to the path of the module's directory.
AppDomainSetup setup = new AppDomainSetup();
setup.PrivateBinPath = subDir;
setup.ApplicationBase = subDir;
AppDomain newDomain= AppDomain.CreateDomain(subDir, null, setup);
byte[] file = File.ReadAllBytes(dllPath);//dll path is a dll inside subDir
newDomain.Load(file);
However. newDomain.Load returns an assembly which the currently domain attempts to load. Because the plugin dlls are in a sub directory, the current domain cannot and should not see these dlls and the current domain throws a FileLoadException
"ex = {"Could not load file or assembly ... or one of its dependencies."
The question is, can we load an assembly into a separate AppDomain without it returning the loaded assembly?
I know I can add a handler for the AssemblyResolve event in the current domain and return a null, but I would prefer to not to go this route.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您还可以使用 DoCallBack - 这是我在阅读了很多有关它的内容后整理的内容。这将创建一个应用程序域,检查程序集是否具有相同的签名公钥,加载程序集,执行静态方法,卸载应用程序域,然后删除 dll。
You can also use DoCallBack - here's something I put together after reading lots about it on SO. This creates an appdomain, checks that the assemblies have the same signature public key, loads the assembly, executes a static method, unloads the appdomain, then deletes the dll.
正如下面给出的链接所指出的:
在不同的 AppDomain 中加载/卸载程序集
< a href="https://stackoverflow.com/questions/2715104/load-assemble-in-new-appdomain-without-loading-it-in-parent-appdomain">在新 AppDomain 中加载程序集,而不在父 AppDomain 中加载它
似乎在另一个
AppDomain
对象上调用Load()
方法也会导致该程序集加载到当前的AppDomain
中。解决方案是使用
AppDomain
类的CreateInstanceFromAndUnwrap()
方法。您传递程序集的路径和要转换为的类型。As pointed out in links given below:
Loading/Unloading assembly in different AppDomain
Load Assembly in New AppDomain without loading it in Parent AppDomain
It seems that calling
Load()
method on anotherAppDomain
object causes that assembly to be loaded in currentAppDomain
as well.The solution is to use
CreateInstanceFromAndUnwrap()
method ofAppDomain
class instead. You pass a path to assembly and a type to be converted to.