Thursday, March 3, 2011

App Domain Logic

//1. What is application domain.?
//Ans: An application domain is a logical container that allows multiple assemblies to run
//e.g If 10 persons browse same website at a time than the worker process will create 10 app
//domain for individual user,they will be isolated from each other.
//Note: This could be also performed if 10 process will be start for execution of individual user
//but switching between the processes would consume processor time, thus decreasing performance.

//2. Show Logical diagram.
//!!Operating System---(Worker)Process-->.Net framework Runtime---1.App Domain----Assembly
//2.App Domain----Assembly

//3. Why use of application domain?
//Ans:Reliability--> Use application domains to isolate tasks that might cause a process to terminate.
//If the state of the application domain that’s executing a task becomes unstable, the application domain can be unloaded without affectingthe process.
//This technique is important when a process must run for long periods without restarting. You can also use application domains to isolate tasks that should not share data.
// Efficiency--> If an assembly is loaded into the default application domain, the
//assembly cannot be unloaded from memory while the process is running. However,if you open a second application domain to load and execute the assembly,
//the assembly is unloaded when that application domain is unloaded. Use this technique to minimize the working set of long-running processes that occasionally
//use large dynamic-link libraries (DLLs).

//Creating a new Application domain
AppDomain d = AppDomain.CreateDomain("Loader");

//Below is the example of creating application domain inside a assembly and running a different assembly in that app domain
//In that manner increasing the security.
MessageBox.Show("Host domain: " + AppDomain.CurrentDomain.FriendlyName);
MessageBox.Show("Child domain: " + d.FriendlyName);
//1. Executing an assembly in seperate application domain by path
d.ExecuteAssembly(@"C:\Documents and Settings\BS26691\Desktop\SampleEXE\SampleEXE\bin\Debug\SampleEXE.exe");
//Executing the assembly by name.
//d.ExecuteAssemblyByName("SampleEXE");
//Unloading the created one Application domain
AppDomain.Unload(d);

//============================Theory OF securing an Assembly via access rights===============================
//1. Define "EVIDENCE" & "CODEGROUP".
//Ans 1:EVIDENCE: It says about the code group.
//Ans 2:CODEGROUP: It tells about the access rights (privelages)of assembly i.e it requires admin right or something else?
//Note: Providing evidence to an assembly means talking about access rights of that assembly.

//2. How to provide an evidence to a assembly?

//Note : The simplest way to control the permissions assigned to an assembly in an application
//domain is to pass Zone evidence by using a System.Security.Policy.Zone object
//and the System.Security.SecurityZone enumeration. The following code demonstrates
//using the Evidence constructor that requires two object arrays by creating a Zone
//object, adding it to an object array named hostEvidence, and then using the object
//array to create an Evidence object named internetEvidence. Finally, that Evidence object
//is passed to the application domain’s ExecuteAssembly method

object[] hostEvidence = { new Zone(System.Security.SecurityZone.Internet) };
// { new Zone(System.Security.SecurityZone.Intranet)};
// { new Zone(System.Security.SecurityZone.MyComputer )};
// { new Zone(System.Security.SecurityZone.Trusted )};
// { new Zone(System.Security.SecurityZone.Untrusted)};
Evidence evd = new Evidence(hostEvidence, null);
//Creating a new Application domain
AppDomain d1 = AppDomain.CreateDomain("Loader");

d1.ExecuteAssembly(@"C:\Documents and Settings\BS26691\Desktop\SampleEXE\SampleEXE\bin\Debug\SampleEXE.exe",evd );
//It will prompt a simple warning message , if we say untrusted instead of Internet zone than we will get access right issue.

AppDomain.Unload(d1);