Background:
The company I'm working for is developing an enterprise application.
We need the ability to load up multiple versions of our application releases into the same memory space.
The applications have the similar assembly names, namespaces, and classes.
The directory structure would look like this:
baseApplicationDirectory\ApplicationLoader.DLL
baseApplicationDirectory\bin\V1.0\AppModule1.DLL
baseApplicationDirectory\bin\V1.0\AppModule2.DLL
baseApplicationDirectory\bin\V2.0\AppModule1.DLL
baseApplicationDirectory\bin\V2.0\AppModule2.DLL
This sounds like the perfect oportunity to use AppDomain.
The Problem:
I successfully create the AppDomain, and everything looks fine till I try to perform a cast.
AppModule1Namespace.Class1 implements interface IFoo. AND MarshalByRefObject
This is how I create an instance of of Class1
public static IFoo New(string classType) { IFoo sysFoo = null; bool canEdit = true; //Go out to the database and see what dll file //the classType is located in string dllName = _storage.GetLatestDllPath(classType, "", out canEdit); if (File.Exists(dllName)) { try { Assembly asm = Assembly.LoadFile(dllName); object objMgr = (Activator.CreateInstance(asm.GetName().Name, classType).Unwrap()); if (objMgr != null && objMgr is IFoo) { sysFoo = (IFoo)objMgr; } } catch { } } return sysFoo; } }
The line if (objMgr != null && objMgr is IFoo) Fails. objMgr is the appropriate class. However the cast test fails. Forcing the cast causes an exception.
The above routine worked up till the point I tried to use app domains.
I'm suspecting that when I invoked the Activator.CreateInstance, it created a new instance of the object in a seperate AppDomain then the executing code.
I was wondering if there was a way to say, "Switch AppDomain Context" so when you are resolving class creation/activation, use this domain.
Edit: Passing the AppDomain to the Activator class does not help with the casting issue.
Curiously, when I try to move the execution point (set next statement) it pops up a dialog that says ,"[Resolve Ambiguity] Choose Specific Location" with two lines to the same New routine that look exactly the same.