从 AppDomain 卸载 .DLL 所需的帮助 - 即使使用 ShadowCopy 仍然无法正常工作 [英] Help needed with unloading .DLL's from AppDomain - Still not working even with ShadowCopy

查看:17
本文介绍了从 AppDomain 卸载 .DLL 所需的帮助 - 即使使用 ShadowCopy 仍然无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试执行以下操作.应用程序 A 是母应用程序".它保持打开状态.App B 只是一个 .DLL,我在其中编写了一些从 App A 中指定的接口派生的类.

I am trying to do the following. App A is the "mother app". It stays open. App B is just a .DLL where I write some classes that are derived from an interface specified in App A.

然后,从 App A 中,我将从 App B 中导入"类并在其中运行方法.我希望能够动态更改App B(更改代码并重新编译)并在App A中使用新代码.

Then, from App A, I will "import" classes from App B and run methods within them. I want to be able to dynamically change App B (change code and recompile) and use the new code in App A.

我在 App B 中有一个编译后命令,可以将新的 .DLL 复制到 App A 目录.App A 创建一个新的 AppDomain 并使用 ShadowCopying.我认为这已经足够了,但是当我尝试重新编译 &复制 App B 的新 .DLL,它说该文件正在使用中,不能被覆盖.

I have a post-compile command in App B that copies the new .DLL to the App A directory. App A creates a new AppDomain and uses ShadowCopying. I thought this would be sufficient, but when I try to recompile & copy App B's new .DLL, it says that the file is in use and can't be overwritten.

这是我现在的代码:

应用 A(代码中的 TestServer):

namespace TestServer
{
    public interface IRunnable
    {
        void Run();        
    }

    class Program
    {        
        static void Main(string[] args)
        {
            AppDomainSetup setup = new AppDomainSetup();
            setup.ApplicationName = "DemoApp";
            setup.ApplicationBase = Environment.CurrentDirectory;
            setup.ShadowCopyDirectories = Environment.CurrentDirectory;
            setup.ShadowCopyFiles = "true";
            int _domain = 1;

            while (true)
            {
                string typeName = Console.ReadLine();

            AppDomain appDomain = AppDomain.CreateDomain("DemoDomain" + _domain, null, setup);

            IRunnable runner = appDomain.CreateInstanceFromAndUnwrap("TestClient.dll", typeName) as IRunnable;

            runner.Run();

            AppDomain.Unload(appDomain);
            _domain++;
            }
        }   
    }   
}

应用 B(代码中的 TestClient):

namespace TestClient
{    
    public class TestRunner : TestServer.IRunnable
    {
        public void Run()
        {
            Console.WriteLine("RUNNING");
        }
    }

    public class Bob : TestServer.IRunnable
    {
        public void Run()
        {
            Console.WriteLine("BOB");
        }
    }
}

我了解到,如果您使用来自其他应用程序域的内容,这些应用程序域可以自动加载 .DLL 或类似内容.在这种情况下,我担心使用接口会导致基础 AppDomain 加载 .DLL 从而将其锁定.

I have read that if you use stuff from other app domains, those app domains could automatically load the .DLL or something along those lines. In this case, I fear that using the Interface is causing the base AppDomain to load the .DLL thereby locking it up.

我该如何解决这个问题/是否有更好的设置?

How can I resolve this issue / is there a better setup??

注意:我已经更新了我的代码,它仍然产生相同的结果.

NOTE: I have update my code and it still yields the same result.

推荐答案

您的代码仍在母 AppDomain 中运行,因为您将程序集和类型拉入其中.任何代码都应该在生成的域中运行.我已经展示了一种在我的网站上设置类似内容的方法:在不同的 AppDomain 中启动代码的简单方法

Your code still runs in the mother AppDomain, as you're pulling in assemblies and types into there. Any code should run in the spawned domain. I've shown one way to set something like that up on my website : A simple way to start your code in a different AppDomain

我不是 100% 确定,但这肯定是您必须采取的一步

I'm not 100% sure on that but that's certainly one step you'll have to undertake

更新

就那里提出的解决方案而言,您的跑步者实例化将发生在 DomainLifetimeHook 的继承者中.显示的基础结构确保 Start 和 Stop 方法在 AppDomainExpander 类创建的 AppDomain 中运行.该扩展器是创建新域的类,因此您的域设置应该在域的 Create 方法中进行.

In terms of the solution presented there, your instantiation of a runner would happen in an inheritor of DomainLifetimeHook. The infrastructure shown ensures that the Start and Stop methods run in the AppDomain created by the class AppDomainExpander. That Expander is the class that creates the new domain, hence your domain setup should go in the Create method of the domain.

这篇关于从 AppDomain 卸载 .DLL 所需的帮助 - 即使使用 ShadowCopy 仍然无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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