AppDomain 阴影复制不起作用(原始程序集已锁定) [英] AppDomain shadow copying not working (original assemblies locked)

查看:29
本文介绍了AppDomain 阴影复制不起作用(原始程序集已锁定)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我用来探测可用插件列表的一个小类:

Here's a small class I'm using to probe for a list of available plugins:

internal static class PluginDirectoryLoader
{
    public static PluginInfo[] ListPlugins(string path)
    {
        var name = Path.GetFileName(path);
        var setup = new AppDomainSetup
        {
            ApplicationBase = path,
            ShadowCopyFiles = "true"
        };
        var appdomain = AppDomain.CreateDomain("PluginDirectoryLoader." + name, null, setup);
        var exts = (IServerExtensionDiscovery)appdomain.CreateInstanceAndUnwrap("ServerX.Common", "ServerX.Common.ServerExtensionDiscovery");
        PluginInfo[] plugins = null;
        try
        {
            plugins = exts.ListPlugins(); // <-- BREAK HERE
        }
        catch
        {
            // to do
        }
        finally
        {
            AppDomain.Unload(appdomain);
        }
        return plugins ?? new PluginInfo[0];
    }
}

path 参数指向包含要加载的插件程序集的子目录.这个想法是使用单独的 AppDomain 加载它们并启用卷影复制.

The path parameter points to a subdirectory containing the plugin assemblies to load. The idea is to load them using a separate AppDomain with shadow copying enabled.

这种的情况下,因为 AppDomain 被快速卸载,所以实际上不需要影子复制,但是当我在我打算编写的下一个代码块中实际加载插件时,我想使用卷影复制,以便可以动态更新二进制文件.我在这门课中启用了卷影复制作为测试,以确保我做对了.

In this case, shadow copying isn't really necessary seeing as the AppDomain is unloaded quickly, but when I actually load the plugins in the next block of code I intend to write, I want to use shadow copying so the binaries can be updated on the fly. I have enabled shadow copying in this class as a test to make sure I'm doing it right.

显然我做得不对,因为当我在代码示例中的注释行(即 plugins = exts.ListPlugins())上中断调试器时,原始插件程序集被锁定通过应用程序!

Apparently I'm not doing it right because when I break in the debugger on the commented line in the code sample (i.e. plugins = exts.ListPlugins()), the original plugin assemblies are locked by the application!

看到我指定 AppDomain 加载的程序集应该进行影子复制,为什么它们会被应用程序锁定?

Seeing as I'm specifying that assemblies loaded by the AppDomain should be shadow copied, why are they being locked by the application?

推荐答案

我想通了.我在 AppDomainSetup 中遗漏了一个属性.属性是 ShadowCopyDirectories.

I figured it out. There was one property I missed in AppDomainSetup. The property was ShadowCopyDirectories.

var setup = new AppDomainSetup
{
    ApplicationBase = path,
    ShadowCopyFiles = "true",
    ShadowCopyDirectories = path
};

当打破我的问题中提到的行时,即使不卸载 AppDomain,我现在也可以删除插件程序集.

When breaking on the line mentioned in my question, I can now delete the plugin assemblies even without unloading the AppDomain.

这篇关于AppDomain 阴影复制不起作用(原始程序集已锁定)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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