Saturday, May 1, 2010

How does an AppDomain get created?

AppDomains are usually created by hosts like Windows Shell, ASP.NET and IE etc.
When you run a .NET application from the command-line the host is the Shell. The Shell creates a new AppDomain for every application.

AppDomains can also be explicitly created by .NET applications. Here is a C# sample which creates an AppDomain creates an instance of an object inside it, and then
executes one of the object's methods.

using System;
using System.Runtime.Remoting;

public class AppDomainInfo : MarshalByRefObject

{
public string GetAppDomainInfo()

{
return "AppDomain = " + AppDomain.CurrentDomain.FriendlyName;
}
}

public class App
{

public static int Main()
{
AppDomain ad = AppDomain.CreateDomain( "My new domain", null, null );
ObjectHandle objh = ad.CreateInstance( "appdomaintest", "GetAppDomainInfo");
GetAppDomainInfo myInfo = (GetAppDomainInfo)(objh.Unwrap());
string info = myInfo.GetAppDomainInfo();
Console.WriteLine( "AppDomain : " + info );
return 0;
}
}

No comments: