Showing posts with label AppDomain. Show all posts
Showing posts with label AppDomain. Show all posts

Sunday, February 26, 2012

Hot to get list of all AppDomains inside current process?



In my earlier post, I have explained how to create AppDomain inside process and how we can share data between AppDomains. As per request from one of you, in this post I am going to explain how we can get list of all AppDomains inside process.

To enumerate all the AppDomains inside process we can use class called CorRuntimeHost. This class is available inside mscoree namespace and can be added externally from C:\Windows\Microsoft.NET\Framework\v4.0.30319\mscoree.tlb location. This class provides EnumDomains method which is used to enumerate AppDomains inside process. The NextDomain method of this class is used to get next available AppDomain if no AppDomain found, sets domain to null. 

Let’s have a look on below code snippet.

AppDomain.CreateDomain("AppDomain 1");
AppDomain.CreateDomain("AppDomain 2");
AppDomain.CreateDomain("AppDomain 3");

List<AppDomain> appDomains = new List<AppDomain>();

IntPtr handle = IntPtr.Zero;
CorRuntimeHost host = new CorRuntimeHost();
try
{
    host.EnumDomains(out handle);
    while (true)
    {
        object domain;
        host.NextDomain(handle, out domain);
        if (domain == null)
            break;
        appDomains.Add((AppDomain)domain);
    }
}
finally
{
    host.CloseEnum(handle);
}

foreach (AppDomain appDom in appDomains)
{
    Console.WriteLine(appDom.FriendlyName);
}

Output –
AppDomain 1
AppDomain 2
AppDomain 3


How to get specific AppDomin from process?

We can get specific AppDomain using the same code but with little modification. Let’s have a look on below code.

public void GetAppDomain(string name)
{
    IntPtr handle = IntPtr.Zero;
    CorRuntimeHost host = new CorRuntimeHost();
    try
    {
        host.EnumDomains(out handle);
        while (true)
        {
            object domain;
            host.NextDomain(handle, out domain);
            AppDomain myAppDomain = domain as AppDomain;
            if (myAppDomain == null)
                break;
            if (myAppDomain.FriendlyName.Equals(name))
            {
            Console.WriteLine("AppDomain Name - {0}", myAppDomain.FriendlyName);
            }
        }
    }
    finally
    {
        host.CloseEnum(handle);
    }
}

See Also –

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 -