Create a second level AppDomain

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(); //wait forever } } 

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?

+4
source share
1 answer

I had a similar problem once (I did not do the same, but the failure to create a third domain from the second domain occurred in my memory, and then FileNotFoundException confirmed "my fears").

This is nothing good. Evidence can't fix it. As MSDN states: http://msdn.microsoft.com/en-us/library/system.security.policy.evidence.aspx

Evidence Defines a set of information that contributes to security policy decisions. This class cannot be inherited.

So just do it:

 class MainClass { public static void Main(string[] args) { var currentEvidence = AppDomain.CurrentDomain.Evidence; var domain = AppDomain.CreateDomain("server", securityInfo: currentEvidence); new Task(() => domain.ExecuteAssembly(PathToSecondAssembly)).Start(); new ManualResetEvent(false).WaitOne(); //wait forever } } 

And if you plan to launch the 4th domain (and so on), continue to transfer evidence from one "truster" to another "trustee":

 class MainClass { public static void Main(string[] args) { var evAgain = AppDomain.CurrentDomain.Evidence; var domain = AppDomain.CreateDomain("name", securityInfo: evAgain); 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(); } } 

Hope this was a problem.

0
source

All Articles