Recipe20.9.Running Code in Its Own appdomain


Recipe 20.9. Running Code in Its Own appdomain

Problem

You want to run code isolated from the main part of your application.

Solution

Create a separate appdomain to run the code using the appdomain.CreateDomain method. CreateDomain allows the application to control many aspects of the appdomain being created like the security environment, the appdomain settings, and base paths for the appdomain. To demonstrate this, the code creates an instance of the RunMe class (shown in full later in this recipe) and calls the PrintCurrentappdomainName method. This prints the name of the appdomain where the code is running.

 public static void RunCodeInNewappdomain() {     appdomain myOwnappdomain = appdomain.CreateDomain("MyOwnappdomain");     // Print out our current appdomain name.     RunMe rm = new RunMe();     rm.PrintCurrentappdomainName(); 

Now you create an instance of the RunMe class in the "MyOwnappdomain" appdomain by calling CreateInstance on the appdomain. We pass CreateInstance the module and type information necessary for constructing the type, and it returns an ObjectHandle. We can then retrieve a proxy to the instance running in the appdomain by taking the returned ObjectHandle and casting it to a RunMe reference using the Unwrap method.

 // Create our RunMe class in the new appdomain. Type adType = typeof(RunMe); ObjectHandle objHdl =     myOwnappdomain.CreateInstance(adType.Module.Assembly.FullName,                     adType.FullName); // Unwrap the reference. RunMe adRunMe = (RunMe)objHdl.Unwrap(); 

The PrintCurrentappdomainName method is called on the RunMe instance in the "MyOwnappdomain" appdomain and it prints out "Hello from MyOwnappdomain!". The appdomain is unloaded using appdomain.Unload and the program terminates.

     // Make a call on the toolbox.     adRunMe.PrintCurrentappdomainName();     // Now unload the appdomain.     appdomain.Unload(myOwnappdomain); } 

The RunMe class is defined here. It inherits from MarshalByRefObject, as that allows you to retrieve the proxy reference when you call Unwrap on the ObjectHandle and have the calls on the class remoted into the new appdomain. The PrintCurrentApp-DomainName method simply accesses the FriendlyName property on the current appdomain and prints out the "Hello from {appdomain}!" message.

 public class RunMe : MarshalByRefObject {     public RunMe()     {         PrintCurrentappdomainName();     }     public void PrintCurrentappdomainName()     {         string name = appdomain.CurrentDomain.FriendlyName;         Console.WriteLine("Hello from {0}!", name);     } } 

The output from this example is shown here:

 Hello from CSharpRecipes.vshost.exe! Hello from CSharpRecipes.vshost.exe! Hello from MyOwnappdomain! Hello from MyOwnappdomain! 

Discussion

Isolating code in a separate appdomain is overkill for something as trivial as this example, but it demonstrates that code can be executed remotely in an appdomain created by your application. There are six overloads for the CreateDomain method and each adds a bit more complexity to the appdomain creation. In situations in which the isolation or configuration benefits outweigh the complexities of not only setting up a separate appdomain but debugging code in it as well, it is a useful tool. A good realworld example is hosting a separate appdomain to run ASP.NET pages outside of the normal ASP.NET environment though this is truly a nontrivial usage.

See Also

See the "appdomain Class," "appdomain.CreateDomain Method," and "ObjectHandle Class" topics in the MSDN documentation.



C# Cookbook
Secure Programming Cookbook for C and C++: Recipes for Cryptography, Authentication, Input Validation & More
ISBN: 0596003943
EAN: 2147483647
Year: 2004
Pages: 424

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net