自定义 AppDomain 和 PrivateBinPath [英] Custom AppDomain and PrivateBinPath

查看:22
本文介绍了自定义 AppDomain 和 PrivateBinPath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 c# 4.0 和一个控制台应用程序只是为了测试,下面的代码确实给出了一个例外.

I'm using c# 4.0 and a console application just for testing, the following code does gives an exception.

AppDomainSetup appSetup = new AppDomainSetup()
{
    ApplicationName = "PluginsDomain",
    ApplicationBase = AppDomain.CurrentDomain.BaseDirectory,
    PrivateBinPath = @"Plugins",
    ConfigurationFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile
};

AppDomain appDomain = AppDomain.CreateDomain("PluginsDomain", null, appSetup);

AssemblyName assemblyName = AssemblyName.GetAssemblyName(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Plugins", "sample.dll"));

Assembly assembly = appDomain.Load(assemblyName); //This gives an exception of File not found

AppDomain.Unload(appDomain);

在我创建的 AppDomain 上使用 Load 时,我不断收到 File not found 异常.

I keep getting File not found exception when using Load on my created AppDomain.

谢谢.

推荐答案

我想我已经知道为什么会发生这种情况了,那是因为当前域也需要加载程序集,即使您在不同的应用程序域中加载程序集,当前域需要了解它并加载它,那是因为 .NET 是如何设计的.

I think i'v figured out why this happens, thats because the current domain needs to load the assembly too even if your loading the assembly in a diffrent app domain, the current domain needs to know about it and load it, thats because how the .NET was designed.

点击此处了解详情.

http://msdn.microsoft.com/en-us/library/36az8x58.aspx

然后我查看了fusion日志,发现新创建的app域能够成功从私有bin路径加载程序集,以及为什么你仍然得到找不到文件"的异常,因为这个异常最初属于当前应用域.

and when i checked the fusion logs, i found that the newly created app domain was successfully able to load the assembly from the private bin path, and the reason why you still get the exception of "File not found", because this exception belongs originally to the current app domain.

这意味着如果您将当前应用程序路径中的程序集复制到当前域正在探测的路径中,您会发现您可以将程序集加载到您的自定义域中.

that means if you copied the assembly in current application path or to the path where the current domain is probing, you will find that you can load the assembly into your custom domain.

希望有所帮助.

这篇关于自定义 AppDomain 和 PrivateBinPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆