Prizes & Awards
My Profile
Active Members
TodayLast 7 Days
more...
|
Resources » Articles » .NET Framework »
Simple Way to Creating Application Domain
|
Simple Way to Creating Application Domain Simple Steps to Create 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 ApplicationDomain = AppDomain.CreateDomain("Domain1")
The above code declare an "ApplicationDomain " 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.
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) How to Load 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.
AttachmentsSimple Way to Creating Application Domain ()
For more details, visit Creating Application Domain,App domain,About application domain
|
Responses
|
No responses found. Be the first to respond and make money from revenue sharing program.
|
|