How to Unload an Assembly Loaded Dynamically Using Reflection

A few days ago while developing an application I faced a very interesting issue, where I was unable to unload an assembly loaded dynamically using reflection. I am using Visual Studio 2008 and .Net 3.5. I searched the internet and somewhere I got the hint that I should load the assembly in another appdomain and create an instance there, use it and then unload the appdomain. I have done that but surprisingly even after that, the issue was not resolved and more surprisingly after the appdomain was unloaded, when I tried to invoke the function from a loaded assembly, the function has been called successfully. Now it's more annoying for me. I was hitting my head. After moving around a lot on the internet I found a way out of this.

 
Now here I want to give the solution for such issues and will tell you how to unload the assembly that has been loaded dynamically using reflection using appdomains, with the simple code snippets.
 
Major obstacles:
 
If the developer simply creates a new appdomain, as in:
  1. AppDomain newDomain = AppDomain.CreateDomain("NewDomain");]   
Then create an instance and get an assembly object as in:
  1. System.Runtime.Remoting.ObjectHandle obj = newDomain.CreateInstanceFrom({Path}, {Type});  
  2. object instance = obj.Unwrap(); 
Now when the developers use the newdomain.CreateIntance method, returns a wrapped instance of the assembly from the new appdomain. This not only loads the assembly into a new appdomain but also loads the assembly into the current appdomain. After that when the user calls the appdomain.unload method unloads the appdomain but the assembly that is loaded into the current appdomain remains as it is.
 
Now there is a solution to this problem. The solution is creating the object in a new appdomain only so that the scope of the object should be in the new appdomain only and does not let the object touch the current appdomain.
 
To implement this we will be following the following steps: (please read carefully, it might be confusing for new developers)
  • Create a class say class "B" derived by MarshalByRefObject
  • Write a method LoadAssembly({AssemblyPath}) in class "B" that will take the assembly path as a parameter. This function will only load that assembly and return void.
  • Write another method ExecuteMethod({MethodName, Params}) in class "B". This method will take MethodName and Params as Parameters.
     
    This will execute the method (name passed in the parameter) of the loaded assembly.
  • Create a new class says class "A". This will be our main class from where we will start and execute the code.
  • In class A write a main method which will load class B in the new appdomain, and then call class B methods to load the assembly by passing the loadassembly method by passing assembly path and then by calling ExecuteMethod by passing method name and parameters.
Ok friends enough theory; now let's get our hands on the code.
  1. //Creating a new appdomain  
  2. AppDomainSetup setup = AppDomain.CurrentDomain.SetupInformation;  
  3. AppDomain newDomain = AppDomain.CreateDomain("newDomain", AppDomain.CurrentDomain.Evidence, setup); //Create an instance of loader class in new appdomain  
  4. System.Runtime.Remoting.ObjectHandle obj = newDomain.CreateInstance(typeof(LoadMyAssembly).Assembly.FullName, typeof(LoadMyAssembly).FullName);  
  5.    
  6. LoadMyAssembly loader = (LoadMyAssembly)obj.Unwrap();//As the object we are creating is from another appdomain hence we will get that object in wrapped format and hence in the next step we have unwrapped it  
  7.    
  8. //Call loadassembly method so that the assembly will be loaded into the new appdomain and the object will also remain in the new appdomain only.  
  9. loader.LoadAssembly(StrPath);  
  10.    
  11. //Call exceuteMethod and pass the name of the method from assembly and the parameters.  
  12. loader.ExecuteStaticMethod(strModule, "MyMethod"new object[] {"girish", "girish });  
  13.    
  14. AppDomain.Unload(newDomain); //After the method has been executed call the unload method of the appdomain.  
  15. //Wow you have unloaded the new appdomain and also unloaded the loaded assembly from memory. 
Here we complete the code in our main class and nowhere is the LoadeMyAssembly class.
  1. class LoadeMyAssembly: MarshalByRefObject {  
  2.     private Assembly _assembly;  
  3.     System.Type MyType = null;  
  4.     object inst = null;  
  5.     public override object InitializeLifetimeService() {  
  6.         return null;  
  7.     }  
  8.     public void LoadAssembly(string path) {  
  9.         _assembly = Assembly.Load(AssemblyName.GetAssemblyName(path));  
  10.     }  
  11.     public object ExecuteStaticMethod(string strModule, string methodName, params object[] parameters) {  
  12.         foreach(System.Type type in _assembly.GetTypes()) {  
  13.             if (String.Compare(type.Name, "MyClass"true) == 0) {  
  14.                 MyType = type;  
  15.                 inst = _assembly.CreateInstance(type.FullName);  
  16.                 break;  
  17.             }  
  18.         }  
  19.         MethodInfo MyMethod = MyType.GetMethod(methodName, new Type[] { typeof(int), typeof(string), typeof(string), typeof(string) });  
  20.         MyMethod.Invoke(inst, BindingFlags.InvokeMethod, null, parameters, null);  
  21.         return null;  
  22.     }