I have two assemblies with basic methods.
- The first performs the other.
- The second assembly creates an object in the new AppDomain.
- This object is about to create a file or print something on the screen.
First build (simplified example):
class MainClass { public static void Main(string[] args) { var domain = AppDomain.CreateDomain("server"); new Task(() => domain.ExecuteAssembly(PathToSecondAssembly)).Start(); new ManualResetEvent(false).WaitOne();
Second:
class MainClass { public static void Main(string[] args) { var domain = AppDomain.CreateDomain("name"); Console.WriteLine ("A"); domain.CreateInstance(Assembly.GetExecutingAssembly().FullName, typeof(Run).FullName, false, 0, null, new object[0], null, new object[0]); Console.WriteLine ("B"); new ManualResetEvent(false).WaitOne(); } } class Run { public Run() { File.Create("something"); Console.WriteLine ("C"); } }
The result depends on the program you are running.
If I run the second Main , I get:
A C B
and the file will be created. I see this as evidence that the second application is working.
When I run the first Main , I only get:
A
and the file is not displayed. The application does not crash, but it freezes.
I tested it on both .NET 4 and Mono 2.10.9 and 3.0.3 (~ git head).
Why? How can I override this problem?
== EDIT ==
(tested on .NET)
I am becoming more and more confused.
I have a problem with PathToSecondAssembly . When the binaries are in the save folder, everything seems perfectly fine (tested on .NET 4.0, but I also assume mono).
When I use a relative path or a path to another directory, the results are as follows:
- in Debug in Visual Studio 2010 I get a
FileNotFoundException , but A is displayed .
Message:
Could not load file or assembly 'test2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies.
An exception occurs in the line of the first assembly:
new Task(() => domain.ExecuteAssembly(PathToSecondAssembly)).Start();
- when launched without debugging, only A is printed, but then nothing happens.
So, the assembly is found (A is printed), but for some reason an exception is thrown.
I really don't know why. Are there any permissions? How to overcome this situation?