Friday, August 14, 2009

What is AppDomain and Storing and Retriving data from App Domain




We all know that with the launch of .Net framework there has been lot many new things. One of the new features is “AppDomain”. What are appdomins (application domains)? Before that let us first understand what processes are. Processes are physically isolated memory and resources are needed to maintain themselves. Also every process has at least one thread. With .Net Microsoft has introduced another extra layer of abstraction or isolation with in the processes called as AppDomain. The AppDomain is not a physical isolation but rather a logical isolation with in the process. This means that we can have a single process hosting multiple appdomains.

Why You Should Use AppDomains?

AppDomain can be use to load and Assembly dynamically, and the entire AppDomain can be destroyed, without affecting the Process. I think this illustrates the abtraction/isolation that an AppDomain gives us.

How to store and retrieve data from app domain. Here is the code

class Program
{
static void Main(string[] args)
{
AppDomain objDomain = System.AppDomain.CurrentDomain;
string Name = "MyApp";
string Value = "My Application Domain";
objDomain.SetData(Name, Value);
Console.WriteLine(objDomain.GetData(Name));
Console.ReadLine();
}
}

No comments: