Welcome to weblogs.com.pk Sign in | Join | Help

Nasir Ali Khan

Believing is easier than thinking. Hence so many more believers than thinkers.
Application Domain, a .NET way of Isolating Applications
Intended Readers: Persons having intermediate knowledge of processes, threads
Level: Expert
 
Introduction
Application domain is the boundary with in which an application runs. This is similar to process that we have in traditional applications. Application domain provides isolation so that application running in one domain can’t affect the other domains. This approach provides flexibility and the options for the developers to create secure and robust applications.
 
One process can contain multiple application domains; application running in one domain can’t directly access the code/data of other. Proxy object is needed to accomplish this and Marshaling is required in inter App domain communication. This isolation of app domains ensures that the failure of application in one domain doesn’t affect other domains in the process.
 
Benefits of Application Domain
  • Application having similar OS resources can be aggregated but isolated in one process through application domain, results efficient utilize of system resources.
  • Inter process communication is much more expensive compared to inter app domain communication, so its better to run two related application in separate domain rather that separate process.
  • Application domain can be create, executes dynamically at run time allows greater application scalability and manageability as compared to process operations which are quite expensive.
Example 1
class Class1
{
      [STAThread]
      static void Main(string[] args)
      {
            AppDomain otherDomain =
                  AppDomain.CreateDomain("MyDomain");
           
            MyTarget(); // call method within current domain
 
            // call method within the context of otherDomain
            otherDomain.DoCallBack(new CrossAppDomainDelegate(MyTarget));
      }
 
      public static void MyTarget()
      {
            Console.WriteLine("My Target called, within the domain " +
 AppDomain.CurrentDomain.FriendlyName);
      }
}
 
The above code is quite self explanatory, in the main method we create a domain named “MyDomain”, after this I called MyTarget method in default domain and then in the newly created domain. 

Example 2
AppDomain2.exe
class Class1
{
      [STAThread]
      static void Main(string[] args)
      {
            Console.WriteLine("This is application domain 2");
            Console.WriteLine(AppDomain.CurrentDomain.FriendlyName);
      }
}
 
AppDomain1.exe
static void Main(string[] args)
{
      Console.WriteLine("Main Assembly is executing...");
 
      AppDomain otherDomain =
            AppDomain.CreateDomain("MyDomain");
 
      // directly executes the main method of assembly
      otherDomain.ExecuteAssembly(@"…\Debug\TrpAppDomain2.exe");
}
 
Since this is single threaded application that’s why execution flow is sequential in both domains, multi threaded application uses app domains powers even more.
 
Your comments and feedbacks are always welcome
Posted: Saturday, May 28, 2005 7:29 PM by nasir

Comments

khurram said:

How do you compare App Domains with traditional way of creating thread and running it? In .NET we can have an associated IPrinciple for security purposes.
# May 28, 2005 8:25 PM
Anonymous comments are disabled