none
Problem loading assembly into AppDomain, but only when running as a Visual Studio Unit test. RRS feed

  • Question

  • I am trying to load an assembly into a second AppDomain and then create an instance of a type defined in that assembly.

    The following program works for me without incident:

      class Program
      {
        static void Main(string[] args)
        {
          AppDomain pserver1Domain = AppDomain.CreateDomain("PServer1Domain");
          Type typeToLoad = typeof(PServerCreation);
          pserver1Domain.Load(File.ReadAllBytes(typeToLoad.Assembly.Location));
          PServerCreation obj = (PServerCreation)pserver1Domain.CreateInstanceAndUnwrap(typeToLoad.Assembly.FullName, typeToLoad.FullName);
          Console.WriteLine(obj);
          Console.Read();
        }
      }
    

     

    but when I try and execute the same code in a Visual Studio Unit Test:

      [TestClass]
      public class UnitTest1
      {
        [TestMethod]
        public void TestMethod1()
        {
          AppDomain pserver1Domain = AppDomain.CreateDomain("PServer1Domain");
          Type typeToLoad = typeof(PServerCreation);
          pserver1Domain.Load(File.ReadAllBytes(typeToLoad.Assembly.Location));
          pserver1Domain.CreateInstanceAndUnwrap(typeToLoad.Assembly.FullName, typeToLoad.FullName);
        }
     
      }
    

    ... the CreateInstanceAndUnwrap method throws the following exception:

     

    Could not load file or assembly 'ATT Help, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies.

    The system cannot find the file specified.

     

    The assembly referenced in the exception is the one which defines PServerCreation.  I'm not sure why the behavior is different considering that I am loading the assembly by explicitly providing bytes as opposed to searching for an assembly name on the bin path. As a side note, the ATT Help assembly only references System.

     

    Any thoughts on why this might be the case?

    Wednesday, January 19, 2011 8:55 PM

Answers