我想在单独的应用程序域中动态卸载程序集,但它不起作用

本文关键字:程序集 卸载 不起作用 动态 单独 应用程序域 | 更新日期: 2023-09-27 18:05:08

我想使用AppDomain加载和卸载程序集,但它不起作用。下面是我想到的最简单的例子。

using System;
using System.Reflection;
namespace DomainTest
{
    class Program
    {
        static void Main(string[] args)
        {
            AppDomain _newDomain;
            var setup = new AppDomainSetup();
            setup.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory;
            setup.ApplicationName = "Isolator";
            _newDomain = AppDomain.CreateDomain("Isolation:" + Guid.NewGuid(),
               null, setup);
            Console.WriteLine("-----------------Before Domain created");
            ShowLoadedAssemblies();
            _newDomain.Load("WorkerTest");
            Console.WriteLine("---------------------After Load");
            ShowLoadedAssemblies();
            AppDomain.Unload(_newDomain);
            Console.WriteLine("--------------------After Unload");
            ShowLoadedAssemblies();
            Console.ReadLine();
        }
        public static void ShowLoadedAssemblies()
        {
            AppDomain currentDomain = AppDomain.CurrentDomain;
            //Make an array for the list of assemblies.
            Assembly[] assems = currentDomain.GetAssemblies();
            //List the assemblies in the current application domain.
            Console.WriteLine("Currently loaded Assemblies:");
            foreach (Assembly assem in assems)
                Console.WriteLine(assem.ToString());
        }
    }
}

为了简单起见,WorkerTest程序集是一个空程序集除了标准的微软,没有任何参考。CSharp and System.Dll

namespace WorkerTest
{
    public class Worker
    {
    }
}

这个例子有一个带有Main()方法的Program类,我在其中创建了一个新的AppDomain (_newDomain)并将WorkerTest.DLL程序集加载到其中。之后,我卸载了_newDomain,并期望WorkerTest.DLL与它一起卸载,但它没有发生。下面您可以看到程序输出,其中显示了在加载之前、在将WorkerTest加载到_newDomain之后以及在将_newDomain卸载之后加载的所有程序集。dll仍然被加载。有人能告诉我为什么它不起作用吗?

 -----------------Before Domain created
Currently loaded Assemblies:
mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
Microsoft.VisualStudio.HostingProcess.Utilities, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
Microsoft.VisualStudio.HostingProcess.Utilities.Sync, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
Microsoft.VisualStudio.Debugger.Runtime, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
vshost32, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
Microsoft.CSharp, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
DomainTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
---------------------After Load
Currently loaded Assemblies:
mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
Microsoft.VisualStudio.HostingProcess.Utilities, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
Microsoft.VisualStudio.HostingProcess.Utilities.Sync, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
Microsoft.VisualStudio.Debugger.Runtime, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
vshost32, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
Microsoft.CSharp, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
DomainTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
WorkerTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
--------------------After Unload
Currently loaded Assemblies:
mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
Microsoft.VisualStudio.HostingProcess.Utilities, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
Microsoft.VisualStudio.HostingProcess.Utilities.Sync, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
Microsoft.VisualStudio.Debugger.Runtime, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
vshost32, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
Microsoft.CSharp, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
DomainTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
WorkerTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null

我想在单独的应用程序域中动态卸载程序集,但它不起作用

我认为问题是这个_newDomain.Load("WorkerTest");返回Assembly对象到当前的应用程序域。由于Assembly包括对所有元信息的访问,它必须实际加载汇编到当前域。

要正确地将程序集加载到新域中,应该由新域中的代码单独完成。或者如果您只需要创建对象的实例-将dll加载到单独的AppDomain