How to load and use dll in a separate appdomain

Apr 16 2009 4:15 AM

I am having a peculiar problem. I am trying to explain the problem.

I have few dlls namely 1: abc.dll - version 1.0.0.0, 2: efg.dll - version 1.0.0.0 in a folder  namely "Previous Version"

I also have another set of dlls namely 1: abc.dll - version 1.1.0.0, 2: xyz.dll - version 1.0.0.0.

Now, as you can see I have two version of abc.dll, this is because abc.dll (version 1.0.0.0) is not required in my current WebApplication but will be used for backward compatibility. The second abc.dll (Version 1.1.0.0) is not extended from the previous version but been completely re-written. My WebApplication do have refrence to the abc.dll (version 1.1.0.0). But for backward compatibility I need to load the previous version of abc.dll (version 1.0.0.0) and efg.dll(version 1.0.0.0) dynamically in a separtae AppDomain and need to instantiate the classes and methods in the dlls.

But the problem is, whenever I am loading the previous versions in a separate appdomain, and trying to read the same, My Web Application is reading from the Bin folder (i.e. reading only the dll which been referenced, in this case abc.dll version 1.1.0.0). Though both the dlls are in separate folder, my application is not reading the older version at all. Following is the code snippets I have used to test.

 

StringBuilder strAssemblyName = new StringBuilder();

// The directory name can be stored in web.config file and can be retrieved accordingly"

string directoryName = @"D:\TestProjects\Server\WebDEServer\Bin\22";

AppDomainSetup appDomainSetup = new AppDomainSetup();

appDomainSetup.ApplicationBase = directoryName;

AppDomain previousVersion = AppDomain.CreateDomain("PreviousVersion", null, appDomainSetup);

 

AssemblyName loadAssembly = new AssemblyName();

string[] filesInTheDirectory = Directory.GetFiles(directoryName);

foreach (string files in filesInTheDirectory)

{

loadAssembly.CodeBase = files;

previousVersion.Load(loadAssembly);

}

 

Assembly[] loadedAssembly = previousVersion.GetAssemblies();

foreach (Assembly a in loadedAssembly)

{

strAssemblyName.Append("Name: ");

strAssemblyName.Append(a.GetName().Name);

strAssemblyName.Append("-------------------");

strAssemblyName.Append(" Version: ");

strAssemblyName.Append(a.GetName().Version);

strAssemblyName.AppendLine();

strAssemblyName.Append(" Classes ");

strAssemblyName.AppendLine();

strAssemblyName.Append(" ---------------------------");

strAssemblyName.AppendLine();

 

Type[] loadedAssemblyTypes = a.GetTypes();

foreach (Type t in loadedAssemblyTypes)

{

strAssemblyName.Append(" ");

strAssemblyName.Append(t);

strAssemblyName.AppendLine();

strAssemblyName.Append(" ------- Methods: ");

if (t.IsClass)

{

MethodInfo[] methodsList = t.GetMethods(BindingFlags.Public |

BindingFlags.Static | BindingFlags.DeclaredOnly);

foreach (MethodInfo mtdInfo in methodsList)

{

strAssemblyName.Append(mtdInfo.Name);

strAssemblyName.Append(",");

}

}

}

strAssemblyName.AppendLine();

}

tbDetails.Text = strAssemblyName.ToString();

}

 

With this code, If I check the version and method, My Application is only showing the newer version and the classes and methods present in the newer version.

 

Can you please help me to read the correct dll and use the same. I cannot remove the reference of the newer version as thaty is been used through out my application and also cannot re-name the dlls.