Application Domains in C#


This article has been excerpted from book "The Complete Visual C# Programmer's Guide" from the Authors of C# Corner.

In .NET, each application runs in an application domain under the control of a host. The host creates the application domain and loads assemblies into it. The host has access to information about the code via evidence. This information can include the zone in which the code originates or the digital signatures of the assemblies in the application domain. The System.AppDomain class provides the application domain functionality and is used by hosts. A host can be trusted if it provides the CLR with all the evidence the security policy requires. 

There are several types of application hosts:

  • Browser host-includes applications hosted by Microsoft Internet Explorer; runs code within the context of a Web site.
  • Server host-regarding ASP.NET, refers to the host that runs the code that handles requests submitted to a server.
  • Shell host-refers to a host that launches applications, namely .exe files, from the operating system shell.
  • Custom-designed host-a host that creates domains or loads assemblies into domains (e.g., dynamic assemblies).

Listing 22.4 executes the managed application MyApp.exe, with specific evidence inside the Internet security zone, in which code has minimal access to local resources. 

Listing 22.4: Running an Application with a Specific Evidence and Zone 

            String myApplication = @"C:\MyApp.exe";
            String[] argsToApp = null;
            String myURL = @"http://www.mindcracker.com";
            SecurityZone myZone = SecurityZone.Internet;
            Evidence myEvidence = new Evidence();
            myEvidence.AddHost(new Zone(myZone));
            myEvidence.AddHost(new Url(myURL));
            AppDomain app = AppDomain.CreateDomain(myApplication, myEvidence);
            app.ExecuteAssembly(myApplication, myEvidence, argsToApp);

Conclusion

Hope this article would have helped you in understanding Application Domains in C#. See other articles on the website on .NET and C#.

visual C-sharp.jpg
The Complete Visual C# Programmer's Guide covers most of the major components that make up C# and the .net environment. The book is geared toward the intermediate programmer, but contains enough material to satisfy the advanced developer.