Application Domains in C#

Historically, process boundaries have been used to isolate applications running on the same computer. Each application is loaded into a separate process, which isolates the application from other applications running on the same computer.

The applications are isolated because memory addresses are process-relative; a memory pointer passed from one process to another cannot be used in any meaningful way in the target process. In addition, you cannot make direct calls between two processes. Instead, you must use proxies, which provide a level of indirection.

Application domains provide a more secure and versatile unit of processing that the common language runtime can use to provide isolation between applications. You can run several application domains in a single process with the same level of isolation that would exist in separate processes, but without incurring the additional overhead of making cross-process calls or switching between processes. The ability to run multiple applications within a single process dramatically increases server scalability.

Application domains has the following benefits:

1. Faults in one application cannot affect other applications.
2. Individual applications can be stopped without stopping the entire process.
3. Code running in one application cannot directly access code or resources from another application.Objects that pass between domains are either copied or accessed by proxy.
4. Permissions granted to code can be controlled by the application domain in which the code is running.

To access the code from another application, you can either load the assembly into the current application domain or create a new application domain and load the assembly into it.

Accessing Assembly Code in Current Application Domain

Suppose you have an assembly named MyNameSpace.exe in the c:\ that you want to access in current application domain.

C# Copy Code
static void Main()
{
// Load the assembly into the current appdomain:
System.Reflection.Assembly newAssembly = System.Reflection.Assembly.LoadFrom(@"c:\MyNameSpace.exe");

// Instantiate RemoteObject:
newAssembly.CreateInstance("MyNameSpace.MyClass");
}

Accessing Assembly Code in Another Application Domain

When loading the assembly into a separate application domain, use AppDomain.ExecuteAssembly to access the default entry point or AppDomain.CreateInstance to create an instance of the class.

C# Copy Code
static void Main()
{
System.AppDomain NewAppDomain = System.AppDomain.CreateDomain("NewApplicationDomain");

// Load the assembly and call the default entry point:
NewAppDomain.ExecuteAssembly(@"c:\MyNameSpace.exe");

// Create an instance of RemoteObject:
NewAppDomain.CreateInstanceFrom(@"c:\MyNameSpace.exe", "MyNameSpace.MyClass");
}

Reference:

http://msdn2.microsoft.com/en-us/library/2bh4z9hs(VS.80).aspx
http://msdn2.microsoft.com/en-us/library/ms173139(VS.80).aspx

1 comment:

Unknown said...

what is the point of coping MSDN content???