帮助需要与卸载从AppDomain的.DLL的 - 仍然没有与影拷贝甚至工作 [英] Help needed with unloading .DLL's from AppDomain - Still not working even with ShadowCopy

查看:279
本文介绍了帮助需要与卸载从AppDomain的.DLL的 - 仍然没有与影拷贝甚至工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做到以下几点。应用程序A是母锏。它保持打开状态。 App B就只是在那里我写的是从应用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.

然后,从应用程序A,我将进口班部分班级一个.DLL从应用程序B和在其中运行的方法。我希望能够动态地改变应用程序B(改变代码并重新编译),并使用在应用一个新的代码。

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.

我在附录二的后编译命令那份新的.dll到App的目录。应用程序A创建了一个新的AppDomain并使用ShadowCopying。我认为这将是足够的,但是当我尝试重新编译和放大器;复制应用程序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.

下面是我为现在的代码:

Here is the code I have as of now:

应用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的继承者。显示的基础设施保证了启动和停止方法由类AppDomainExpander创建的AppDomain中运行。这扩展是创建新的域类,因此,您的域名设置应该在域中的创建方法去。

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的 - 仍然没有与影拷贝甚至工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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