Home
    Shop
    Advertise
    Write For Us
    Affiliate
    Newsletter
    Contact

.NET Application Domains

Overview

Before .NET framework 2.0 technology, the only way used to isolate applications running on the same machine is by the means of process boundaries. Each application run within a process, and each process has its own boundaries and memory addresses relative to it and this is how isolation from other processes was performed.

 

.NET framework 2.0 introduces a new boundary called the Application Domains. Each application running within its main process boundaries and its application domain boundaries. So, you can think of the application domain as an extra shell to isolate the application and making it more secure and robust.

The above is not the main advantage of application domains. The main advantage is the ability to run several applications domains in a single process or application. All of this is performed while maintaning the same level and quality of isolation that would exist in separate processes, without the need of making cross-process calls or switching between processes.

Advantages

You may ask, why should I create more than one application domain within my application?

The following advantages of application domains answer this question.

  • In terms of isolation, code running in one application domain can not access code or resources running in another application domain.
  • In terms of security, you can run more than one set of web controls in a single browser process. Each set of them is running in a separate application domain so each one can not access the data or resources of the other sets. You can control the permissions granted to a given piece of code by controlling the application domain inside which the code is running.

  • In terms of robustness, fault in code running in one application domain can not affect other applications although they all are running inside the same process. Individual application domain can be stopped without stopping the entire process, you can simply unload the code running in a single application domain.

So, from the above advantages, you can observe that by using application domains you can create rather robust .NET applications. It increases isolation, stability, and security of your application.

Relation Between Application Domains and Assemblies

Most development and runtime environments has a definition for the building blocks of an application. Assemblies are the building blocks of .NET framework applications. They are the fundamental unite of deployment. An assembly consists of types and resources working together to form a logical unit of the functionality of your application. You can divide your .NET application into assemblies. The assembly file can have an .EXE or a .DLL extension.

As we mentioned previously, you can run more than one application domain within your application. Each application domain will run a given piece of code. An assembly is simply the piece of code we mean here. So, each application domain can run an assembly within the entire application. This is the relation between application domains and assemblies.

Creating an Application Domain

"System.AppDomain" is the main class you can use to deal with application domains. To create an application domain use one of the overloaded "CreateDomain" methods in this class.

The following piece of code creates an application domain and assign a name to it.

        Dim NDomain As AppDomain
        NDomain = AppDomain.CreateDomain("Domain1")

The above code declare an "NDomain" variable of type "AppDomain", then calls the "CreateDomain" method giving it a string that represents the name of the newly created application domain.

You can configure the newly created application domain by using the "AppDomainSetup" class with its most important property "ApplicationBase" which defines the root directory for this application domain. You can also use this class to control many settings for the newly created application domain like application name, cache path, configuration file, license file, and others.

You can use this class as shown in the following code.

        Dim NDomainInfo As New AppDomainSetup
        NDomainInfo.ApplicationBase = "C:\AppDomains\Ex2"
 
        Dim NDomain As AppDomain
        NDomain = AppDomain.CreateDomain("Domain1", Nothing, NDomainInfo)
 
        MsgBox(NDomain.BaseDirectory())

When you run this code, you will get the following message box showing the base directory of the newly created application domain.


Figure 1 - The message box showing the base directory of the created application domain

You can obtain setup information of a newly created application domain by using the newly created instance of the "AppDomain" class. You can obtain information like the friendly name of the application domain, the base directory of the application domain, the relative search path of the application domain, and others.

The following code shows how we can obtain these information.

        Dim NDomainInfo As New AppDomainSetup
        NDomainInfo.ApplicationBase = "C:\AppDomains\Example2"
 
        Dim NDomain As AppDomain
        NDomain = AppDomain.CreateDomain("Domain1", Nothing, NDomainInfo)
 
        MsgBox(NDomain.FriendlyName)
        MsgBox(NDomain.BaseDirectory())
        MsgBox(NDomain.RelativeSearchPath)        

Loading Assemblies

As we mentioned above, each application domain can run an assembly. The running assembly can be shared between application domains by creating a private copy of that assembly for each domain. These copies are created by the runtime host (.NET) so it is not your responsibility anyway.

You can load an assembly into an application domain by using more than one method. Each method uses a different class and technique.

You can use one of the many overloaded "Load" methods provided by the "System.AppDomain" class. These methods are mainly used for COM interoperability but they can be used successfully to load an assembly in this instance of application domain. You can also use the "CreateInstance" method of the "System.AppDomain" class for the same reason.

You can also use the two static methods "Load" and "LoadFrom" of the "System.Reflection.Assembly" to load an assembly into the caller domain.

To use the "Load" method of the "System.AppDomain" class, you have to define an assembly object first. You will pass to that object the file path of your assembly EXE or DLL file. After that you will need to get the display name of this assembly and to pass it to the "Load" function.

You can also use "System.AppDomain.ExecuteAssembly" method to execute an assembly given its file name path as shown in the following line of code.

        NDomain.ExecuteAssembly("C:\AppDomains\Example2.exe")

When you run the above code the EXE file located at "C:\AppDomain\" will be carried out and will display it's form (a hello message) as shown in the next figure.


Figure 2 - Example2.exe running from Example1.exe in a separate application domain

To use the "LoadFrom" method of the "System.Reflection.Assembly" class, see the following code.

        Dim Assm As System.Reflection.Assembly
        Assm = System.Reflection.Assembly.LoadFrom( _
        "C:\AppDomains\Example2.exe")

This will load the assembly "Example2.exe" in the currently running application domain. You can use the "Assm" instance to access many information and properties about the "Example2.exe" assembly like its name, version, modules, and functions. You can also invoke a specified method that is located inside the "Example2.exe" assembly.

The following code displaying a set of message boxes, the first displays the location of the assembly, the second displays the name of the assembly, and the third displays the version of the loaded assembly.

        MsgBox(Assm.Location)
        MsgBox(Assm.GetName.Name)
        MsgBox(Assm.GetName.Version.ToString)
 

Figure 3 - Message box showing the name of the loaded assembly
 

Figure 4 - Message box showing the version of the loaded assembly

Unloading

When you have finished using a newly created application domain, you have to unload it using the "System.AppDomain.Unload" method to shutdown the application domain and to free all its resources. To unload an assembly from an application domain, you simply unload the application domain by using the "Unload" method.

To download the examples used in this tutorial click here.


For further information

Refer to the online copy of Microsoft Developers Network at http://msdn.microsoft.com or use your own local copy of MSDN.


Tutorial toolbar:  Tell A Friend  |  Add to favorites  |  Feedback  |   


comments powered by Disqus