我有一个 mvc3 应用程序。我的应用程序项目中有一个名为“插件”的子文件夹
存储在该文件夹中的所有 dll 都应该在运行时更新,所以我们放下 appdomain 并重新加载新版本的 dll,所以我尝试将所有插件 dll 加载到不同的 appDomain 并设置它的 shadowCopy 属性.
通过 SO、msdn 和一些博客,我得到了这个“解决方案”。(这是在我的 Application_Start 期间调用的)
static AppDomain pluginDomain;
static PluginHolder()
{
AppDomainSetup domainSetup = AppDomain.CurrentDomain.SetupInformation;
domainSetup.ApplicationName = "Plugins";
domainSetup.PrivateBinPathProbe = domainSetup.PrivateBinPath;
domainSetup.PrivateBinPath = GetPluginsDirectory();
domainSetup.ShadowCopyFiles = "true";
domainSetup.ShadowCopyDirectories = domainSetup.PrivateBinPath;
pluginDomain= AppDomain.CreateDomain("Plugins", null, domainSetup);
var item = pluginDomain.Load(File.ReadAllBytes(GetPluginsDirectory() + "Item.dll"));
}
“Item.dll”是我试图加载的 dll。最后一行抛出“无法加载文件或程序集项或其依赖项之一”。这似乎是其他人成功的方式,但它对我不起作用。
我以前没有使用 AppDomains 的经验,所以我不确定如何解决这个问题,或者我是否正确处理它。
我的程序集通过新的 AppDomain 加载是一种好的方法吗?