Dll Heaven: Multiple Versions One Assembly
Join the DZone community and get the full member experience.Join For Free
Everyone wants their application to be backward compatible and if the application is based on a plug-in based architecture then such a feature can be a nice addition. When can such a feature be useful? Suppose we have a plug-in subsystem which we have upgraded to newer plug-in system and we want to support older systems.
So if we want to load 2 different versions of a same assembly what do we need to do?First, each version needs to be loaded into 2 different AppDomains.
Because when an assembly is loaded it cannot be unloaded so if we have 2 different assemblies once a type has been loaded from one of them, it will be reused each time we want to instantiate a type. But an assembly loaded into an AppDomain can be unloaded by unloading the AppDomain itself. I am going to demonstrate how to load 2 versions of the same assembly into same executable and run them simultaneously.
Example Step 1: In order to make it simple and avoid the reflection trouble I am going to create an interface and put it into a dll and reference the interface to the concrete dll. So here is code for the interface class in a simple dll that will be used to invoke methods.
public interface IExecute
Example Step 2:I will build this into a single dll and then reference it to the interface. We need to make the class inherit from MarshalByRefObject since we want to communicate across AppDomains.
public class SimpleClass : MarshalByRefObject,
public string Execute()
return "This is executed from version 1";
So we have a class that is referenced from MarshalByRefObject and implement our interface. Lets compile this dll and after doing so ... take the dll and rename it to AssemblyVersionTest1.dll
Lets now change this line
return "This is executed from version 1;
return "This is executed from version 2";
Then again compile the dll and rename it to AssemblyVersionTest2dll
Example Step 3:Now we come to our third application, the console executable. Lets put the 2 versions of the dll to the bin path of the console executable and use the following code:
// Create an app domain
AppDomain appDomain1 = AppDomain.CreateDomain("Version1");
// Instantiate the object from the app doamin and the first version file
object obj= appDomain1.CreateInstanceFromAndUnwrap(
AppDomain.CurrentDomain.BaseDirectory + "\\AssemblyVersionTest1.dll",
IExecute iex = (IExecute)obj;
// Instantiate the object from the app doamin and the second version file
AppDomain appDomain2 = AppDomain.CreateDomain("Version2");
object obj2 = appDomain2.CreateInstanceFromAndUnwrap(
AppDomain.CurrentDomain.BaseDirectory + "\\AssemblyVersionTest2.dll",
IExecute iex2 = (IExecute)obj2;
Now observe that we have had renamed the different versions of the dlls and loaded them explicitly. The output of the above code should be ...
This is executed from version 1
This is executed from version 2
Now we can see that the domains have loaded 2 different versions of the same class in the same executable and executed them.
Published at DZone with permission of Shafqat Ahmed. See the original article here.
Opinions expressed by DZone contributors are their own.