I want to load and unload an assembly using AppDomain and it does not work. Here the simplest example I came up with.
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());
}
}
}
For the simplicity of the example the WorkerTest assembly is an empty assembly with no references to anything except standard Microsoft.CSharp and System.Dll
namespace WorkerTest
{
public class Worker
{
}
}
This example has a Program class with Main() method where I create a new AppDomain (_newDomain) and load a WorkerTest.DLL assembly into it. After that I unload the _newDomain and expect the WorkerTest.DLL to be unloaded with it, but it does not happen. Below you can see the program output that shows all the loaded assemblies before the load, after the load of WorkerTest into the _newDomain and after the _newDomain is unloaded. The WorkerTest.DLL is still loaded. Could someone tell me why it does not work?
-----------------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