|
The application Domain is used to isolate an application from other applications. One process has its own virtual memory and does not over lap the other process's virtual memory; due to this one process can not crash the other process. As a result any problem or error in one process does not affect the other process. In .Net they went one step ahead introducing application domains. In application domain multiple applications can run in same process with out influencing each other. If one of the application domains throws error it does not affect the other application domains.
Win32 processes provide isolation by having distinct memory address spaces. This is effective, but it is expensive and doesn't scale well. The .NET runtime enforces AppDomain isolation by keeping control over the use of memory - all memory in the AppDomain is managed by the .NET runtime, so the runtime can ensure that AppDomains do not access each other's memory. Objects in different application domains communicate either by transporting copies of objects across application domain boundaries, or by using a proxy to exchange messages.
MarshalByRefObject is the base class for objects that communicate across application domain boundaries by exchanging messages using a proxy. Objects that do not inherit from MarshalByRefObject are implicitly marshal by value. When a remote application references a marshal by value object, a copy of the object is passed across application domain boundaries.
With the .NET architecture we have a new boundary for applications: application domains . With managed IL code the runtime can ensure that access to the memory of another application inside a single process can't happen. Multiple applications can run in a single process within multiple application domains.
.gif)
Creating Application Domain
The AppDomain class is used to create and terminate application domains, load and unload assemblies and types, and to enumerate assemblies and threads in a domain. AppDomains are usually created by hosts. Examples of hosts are the Windows Shell, ASP.NET and IE. 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.
Example: Demonstrate Application Domain
Create a C# Console Application i.e. “FirstApplication”
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting;
namespace ConsoleApplication1
{
class Program1
{
public Program1() {
Console .WriteLine( "Constructor in First Application" );
}
static void Main ( string [] args)
{
Console .WriteLine( " Main in domain {0} called" , AppDomain .CurrentDomain.FriendlyName);
display();
}
static void display() {
Console .WriteLine( "Display Calls" );
}
}
}
Here we are creating one application that have constructor and on method. We here display the value of the current application domain that this application belongs too.
Output:
G:\cSharp-Examples\ConsolesApplications\FirstApplication\FirstApplication\bin\ Debug >FirstApplication.exe
Main in domain FirstApplication.exe called Display Calls
Note:-The second project created is again a C# Console Application: “SecondApplication”. First, display the name of the current domain using the property FriendlyName of the AppDomain class. With the CreateDomain() method, a new application domain with the friendly name New AppDomain is created. Then load the assembly “FirstApplication” into the new domain and call the Main () method by calling ExecuteAssembly ():
.
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication2
{
class Program2
{
static void Main ( string [] args)
{
AppDomain currentDomain = AppDomain .CurrentDomain;
Console .WriteLine(currentDomain.FriendlyName);
AppDomain secondDomain = AppDomain .CreateDomain( "New AppDomain" );
secondDomain.ExecuteAssembly( "FirstApplication.exe" );
}
}
}
Note:- Befor running this application exe, place the “FirstApplication.exe” inside the bin folder under this application directory
Output:
G:\cSharp-Examples\ConsolesApplications\SecondApplication\SecondApplication\bin\Debug>SecondApplication.exe
SecondApplication.exe
Main in domain New AppDomain called Display Calls
|