Tuesday, December 13, 2011

How to create custom Application Domain?



Each .NET application by default creates one application domain. The default application domain created by CLR automatically when process (application) starts. Application domain is the runtime unit of isolation in which runs .net application in managed memory boundary. Application domain created under process and process can have one or more application domains. See below figures.

Process with single Application Domain


 Process with multiple Application Domains



Creating Custom Application Domains

AppDomain class is provided by .Net to create custom application domain. AppDomain.CreateDomain and AppDomain.Unload methods are provided to create and destroy custom application domain. Let’s have a look on below code.

public class MyClass
{
       public static void Main()
       {
              AppDomain appDomain = AppDomain.CreateDomain("My Domain");
appDomain.DoCallBack(new CrossAppDomainDelegate(HelloMethod));
Console.ReadLine();
AppDomain.Unload(appDomain);
       }
       public static void HelloMethod()
       {
              Console.WriteLine("Hello from " + AppDomain.CurrentDomain.FriendlyName);
       }     
}



In above example, AppDomain.CreateDomain method creates new application domain named “My Domain” and AppDomain.Unload method unloads or removes "My Domain". DoCallBack method is used to execute action on another application domain. The HelloMethod is static so DoCallBack (CrossAppDomainDelegate) delegate is referencing a static method.

AppDomain also provides ExecuteAaasembly method which will execute an assembly file.

AppDomain appDomain = AppDomain.CreateDomain("My Domain");
appDomain.ExecuteAssembly("WPFApplication1.exe");
Console.ReadLine();
AppDomain.Unload(appDomain);

When you create a new application domain within your current process, CLR keeps isolated one application domain from other. So each application domain has its own separate memory and objects which can’t clash with other application domain.

Sharing Data between application domains

We can share data between application domains using named slots. AppDomain instance provides SetData method to set data as name and data pair. While the GetData method retrieves an object (data) based on given name. Below are the signatures of both the methods. 

public void SetData(string name, object data);
public object GetData(string name);

Let’s have a look on below code.

public class MyClass
{
       public static void Main()
       {
              AppDomain appDomain = AppDomain.CreateDomain("My Domain");
appDomain.SetData("AppDomainOwner", "Mitesh Sureja");
appDomain.DoCallBack(new CrossAppDomainDelegate(HelloMethod));
Console.ReadLine();
AppDomain.Unload(appDomain);
       }
       public static void HelloMethod()
       {
              Console.WriteLine("Hello from " + AppDomain.CurrentDomain.FriendlyName);
       Console.WriteLine("Created By " + AppDomain.CurrentDomain.GetData("AppDomainOwner"));
       }     
}


As shown in above example, SetData method set name as “AppDomainOwner” and data as “Mitesh Sureja”. While GetData method retrieves data as an object based on given name.

Another way to share data between multiple application domains is using Remoting.

Hope you liked this post related to application domain. Please feel free to write your comments/feedback in comments section below.

See Also - 

2 comments:

  1. 1 - How can I list AppDomains of a specific process in .net 2/3.5?
    2 - How can I get a specific AppDomain from a Process?
    3 - How can I unload a specific AppDomain of a specific process?

    ReplyDelete
    Replies
    1. Hi Asad,

      for question 1 and 2, you can refer to below post

      http://miteshsureja.blogspot.in/2012/02/hot-to-get-list-of-all-appdomains.html

      and for question 3, you can use AppDomain.Unload method to unload appdomain.

      Delete