Introduction
Problem: In .NET, assemblies that are loaded cannot be directly unloaded. There no Assembly. Unload
. Method. Why?
Using the code
We can unload assemblies by using AppDomain class. We can open make our own appDomain dynamically and ask a particular assembly to run in that appDomain. Also we can easily unload that appDomain.
AppDomain is a virtual location in memory where a process runs. Usually, each process runs in its own space and if first process making a call to second process, and if second process crashes, so first will also crash. So in .NET to solve this problem give us concept of AppDomains. The main process is created by the .NET CLR and then, each assembly runs in its own AppDomain. And many AppDomains can exist in a single process space. So, if one AppDomain crashes, only its space is released and the process remains as is. This reduces the chances of a system crash. Also, if an AppDomain does crash, the CLR throws back an exception which can be handled in the caller appDomain.AppDomain Class: The AppDomain class is the programmatic interface to application domains. This class includes methods to create and unload domains, to create instances of types in domains, and to register for various notifications such as application domain unloading. The following table lists commonly used AppDomain methods.
Some AppDomain Method
1. CreateDomain : Creates a new application domain. We can set the properties of a new domain, such as the application base, or root directory for the application; the location of the configuration file for the domain; and the search path that the common language runtime is to use to load assemblies into the domain.
2. ExecuteAssembly : Executes an assembly in the application domain. This is an instance method, so it can be used to execute code in another application domain to which you have a reference.
3. CreateInstanceAndUnwrap : Creates an instance of a specified type in the application domain, and returns a proxy. Use this method to avoid loading the assembly containing the created type into the calling assembly.
4. Unload: Performs a graceful shutdown of the domain. The application domain is not unloaded until all threads running in the domain have either stopped or are no longer in the domain.
5. Load : Overloaded Loads an Assembly into this application domain.
Example:
AppDomain newDomainName = AppDomain.CreateDomain("newDomainName");
try
{
Assembly testLibrary = newDomainName.Load(LibraryName);
}
finally
{
AppDomain.Unload(newDomainName);
}
For Plug-in Environment:
for plug-in systems, this means that without the aid of a secondary AppDomain
, once a plug-in has been loaded, the entire application must be shut down and restarted in order for a new version of a plug-in to be reloaded. To address this problem, a secondary AppDomain
must be created and all plug-ins have to be loaded into that AppDomain
. Application domains are created using the CreateDomain method. Then we can direct unload that Domain.
// Construct and initialize settings for a second AppDomain.
AppDomainSetup ads = new AppDomainSetup ();
ads.ApplicationBase = System.Environment.CurrentDirectory; ads.DisallowBindingRedirects = false;
ads.DisallowCodeDownload = true;
ads.ConfigurationFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
// create the second AppDomain.
AppDomain ad2 = AppDomain.CreateDomain ("AD #2", null, ads);
MarshalByRefType marts = (MarshalByRefType) ad2.CreateInstanceAndUnwrap (exeAssembly, type of(MarshalByRefType).FullName );
// Call a method on the object via the proxy, passing the // default AppDomain's friendly name in as a parameter.
mbrt.SomeMethod (callingDomainName);
// unload the second AppDomain. This deletes its object and // invalidates the proxy object.
AppDomain.Unload (ad2);
MarshalByRefObject is the base class for objects that communicate across application domain boundaries by exchanging messages using a proxy Types must inherit from MarshalByRefObject when the type is used across application domain boundaries, and the state of the object must not be copied because the members of the object are not usable outside the application domain where they were created.
For more details:
http://msdn2.microsoft.com/en-us/library/system.appdomain.createdomain.aspx
http://msdn2.microsoft.com/en-us/library/system.marshalbyrefobject.aspx
http://msdn2.microsoft.com/en-us/library/system.appdomain.aspx