Introduction to AppDomains


Introduction to AppDomains

Whereas the assembly is a logical unit of deployment, the AppDomain is a logical unit of execution. The AppDomain is a sandbox in which .NET code runs. The AppDomain provides a container in which code can execute safely, knowing that code running outside the AppDomain cannot negatively impact it. As you will see throughout this book, cross-process communication done with Remoting is actually the process of passing information between AppDomains, whether they are on the same machine or different machines.

Throughout the course of an application, your code may load a lot of assemblies creating instances of other classes. Without the use of the AppDomain class, all of those assemblies will remain in memory for the lifespan of your application. If you are coding a long-running server application, this is unacceptable. Using the AppDomain class, however, you can manually control the loading and unloading of assemblies, as well as create an isolated environment to run potentially volatile tasks.

The AppDomain class represents the properties and functionalities of this memory sandbox, as shown in Table 12.1.

Table 12.1. AppDomain Properties and Methods

Property/Method

Description

ApplicationIdentity

Gets the identity of the application in the AppDomain, containing information such as the location and full name.

BaseDirectory

The base directory that will be used to look for dependent assemblies.

CurrentDomain

Gets an instance of the AppDomain class, indicating the current AppDomain.

FriendlyName

Gets the friendly name of the application domain.

SetupInformation

Gets an AppDomainSetup instance containing application and configuration binding data.

CreateCOMInstanceFrom()

Creates an instance of a COM object. You will learn more about this in Chapter 13.

CreateDomain()

Creates a new named AppDomain.

CreateInstance()

Creates an instance of a specified type in the specified assembly.

CreateInstanceFrom()

Creates an instance of the specified type in the specified assembly file (requires the .dll or .exe extension).

ExecuteAssembly()

Executes the assembly file specified, such as another .exe.

GetAssemblies()

Obtains the list of assemblies loaded within the AppDomain.

Getdata()

Gets data contained in a named slot visible to all assemblies in the AppDomain.

Load()

Loads an assembly into the AppDomain.

SetData()

Sets data contained in a named slot visible to all assemblies in the AppDomain.

SetThreadPrincipal()

Sets the principal (security user) that will be presented when a thread is queried for the security context.

Unload()

Unloads an AppDomain and releases all loaded assemblies.




Microsoft Visual C# 2005 Unleashed
Microsoft Visual C# 2005 Unleashed
ISBN: 0672327767
EAN: 2147483647
Year: 2004
Pages: 298

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net