Nov 12, 2011

AppDomain


What is AppDomain
                AppDomain is a modal which defines boundary for code execution. AppDomain provides isolation within a process. AppDomain will be hosted into the OS process. The OS process can host multiple AppDomain.

                The  code running in an AppDomain will not affect the code running in another AppDomain. By this way it provides Security to your application.
AppDomain can be unloaded from the process at any time of execution. But the AppDomain should be idle during unload.
                By providing isolation, the AppDomain exceptions, memory are handled separately for each AppDoamin

Create your own appdomain,

AppDomain domain = AppDomain.CreateDomain("MyDomain");

Unload AppDomain,
Using the System.AppDomain.Unload method

Exception:  CannotUnloadAppDomainException.
Namespace: System.AppDomain

Setting AppDomain Setup using the AppDomainSetup.
// Create application domain setup information.
        AppDomainSetup domaininfo = new AppDomainSetup();
        domaininfo.ApplicationBase = "f:\\work\\development\\latest";

        // Create the application domain.
        AppDomain domain = AppDomain.CreateDomain("MyDomain", null,  domaininfo);

Use the LoadFrom to the assembly into AppDomain.
// Use the file name to load the assembly into the current
        // application domain.
        Assembly a = Assembly.Load("example");
        // Get the type to use.
        Type myType = a.GetType("Example");
        // Get the method to call.
        MethodInfo myMethod = myType.GetMethod("MethodA");
        // Create an instance.
        object obj = Activator.CreateInstance(myType);
        // Execute the method.
        myMethod.Invoke(obj, null);
    

No comments: