How to Create a new instance of class under an AppDomain

2

I am trying to invoke a class to run under a separate AppDomain but I am getting an exception about noting being able to load the class or one of its dependencies. Here is an example of what I am doing.

AppDomain ad = AppDomain.CreateDomain("New domain");
IIdentity identity = new GenericIdentity("NewUser");
IPrincipal principal = new GenericPrincipal(identity, null);
ad.SetThreadPrincipal(principal);
TestClass remoteWorker = (TestClass)ad.CreateInstanceAndUnwrap(
                         binFolder+"\\TestProject.dll",
                         "TestClass");
remoteWorker.Run(data1,data2);

In my separate TestProject I have one class named TestClass:

 public class TestClass : MarshalByRefObject
 {
      public void Run(string name, string path)
      {
           //Some stuff
      }
  }

I've checked all the paths for everything and they are correct. I am getting the error:

Could not load file or assembly 'K:\\Installer\\Bin\\TestProject.dll' 
or one of its dependencies. The given assembly name or codebase was invalid. 
(Exception from HRESULT: 0x80131047)

I've tried changing paths, moving it to the currently executing assembly. Still nothing. The DLL is located in K:\Installer\Bin\TestProject.dll. I am running this under admin so it has permissions to the Dll.

c#
reflection
dll
appdomain
asked on Stack Overflow Jul 26, 2013 by busbina

1 Answer

4

Figured it out. Changed my code to this:

AppDomainSetup domainSetup = new AppDomainSetup 
                             {PrivateBinPath = binFolder+"\\TestProject.dll"};
AppDomain ad = AppDomain.CreateDomain("New domain",null, domainSetup);

TestClass remoteWorker = (TestClass)ad.CreateInstanceFromAndUnwrap(
                          binFolder+"\\TestProject.dll",
                          typeof(TestClass ).FullName);
answered on Stack Overflow Jul 26, 2013 by busbina

User contributions licensed under CC BY-SA 3.0