I've been searching for the past few days how to load an assembly in a separate app domain than the default one and I can't find an answer on how to do so. I will briefly explain the majority of things I have tried.
I have used CreateInstanceFromAndUnwrap and this works....Mostly but there are 2 problems with this method. The first problem is that when I call
newDomain.GetAssemblies()
I get a FileNotFoundException and the only way to resolve this exception is to implementAppDomain.CurrentDomain.AssemblyResolve
. The problem with this is that it loads the assembly into the default domain, meaning I can't unload the Assembly. The Second problem is that I have a base abstract class in the running assembly called Task and I have child classes in other assemblies that derive from Task. When I callvar obj = newDomain.CreateInstanceFromAndUnwrap(param1, param2)
it returns me a MarshalByRefObject (which it should) but when I cast it to the abstract class:var obj = (Task)newDomain.CreateInstanceFromAndUnwrap(param1, param2)
, the type of obj is "Task". Now I know that it's an instance of ChildTask but it says that the type is Task and I need the type to be ChildTask but I can't convert it to ChildTask without loading the assembly into the Default Domain.I have created a Proxy class that inherits from MarshalByRefObject and loaded the Assembly inside the Proxy class (as many of the answers on this topic suggest) but all it does is load the assembly into the default domain and not the created one. Therefore not allowing me to unload it once I'm finished with it.
I have used
newDomain.DoCallback(CallbackMethod)
in order to load an assembly without my new appdomain. The problem is that once the method has finished executing, the changes done in the Callback method have no effect on the Default Domain for example:public void DoSomething() { newDomain.DoCallback(CallbackMethod) Type type = newDomain.GetData("TYPE") // This returns null (as if nothing happened in CallbackMethod) } public void CallbackMethod() { newDomain.SetData("TYPE", newDomain.CreateInstanceFromAndUnwrap(param1, param2).GetType()) } // At this point "TYPE" will be set to ChildTask
This makes sense because they are in separate domains so changes in one domain shouldn't effect another domain. I don't know how to allow them to interact with each other so that I can get a set value from CallbackMethod()
I have used
newDomain.Load()
(even though it's deprecated) and I get a FileNotFoundException even while using AppDomainSetupI have tried using Serialization but I keep getting a bunch of serialization exceptions saying that a Type can't be resolved, etc and the only way to fix these exceptions is to using Assembly Resolve which loads the assembly into the Default Domain.
Is there something I'm missing? All I really want to do is be able to load an assembly into a new appdomain and get a type from that assembly, but it seems that I can't load assemblies unless they're loaded into the Default Domain. Is it possible to do this? Or will I just have to find a workaround?