C# .NET - Application Domain

Asked By K S
23-Jun-08 08:13 AM
How to create a Appliction Domain, and how one application domain can call another

See this abt the Application Domain  See this abt the Application Domain

23-Jun-08 08:26 AM

Application Domain

An Application Domain is a light-weight process.  It is a logical and physical unit of isolation built around every .NET application by the Common Language Runtime (CLR) and contains its own set of code, data and configuration settings.  Multiple application domains can exist simultaneously in the same process.  The default application domain is created when the Common Language Runtime is first loaded into a process.  From then on, the CLR loads an assembly implicitly into an Application Domain the first time it encounters and references a type in the MSIL code. Assemblies can also be created explicitly loaded in Application Domains.  This article discusses what Application Domains are and the differences between Application Domains and Processes.  It also discusses Default Domains and how Application Domains can be created, loaded and unloaded explicitly.

Creating Application Domain

The AppDomain class abstracts application domains.  An Application domain can be created using AppDomain class of System namespace, but in most cases they are created and managed by the runtime hosts that execute the application's code.  An Application Domain is created using one of the following overloaded CreateDomain methods of the System.AppDomain class.

public static AppDomain CreateDomain(String appDomainName)
public static AppDomain CreateDomain(String appDomainName, Evidence securityInformation)
public static AppDomain CreateDomain(String appDomainName,
  Evidence securityInformation, AppDomainSetup appDomainSetupInformation)
public static AppDomain CreateDomain(String name, 
  Evidence securityInformation, String appBasePath, String appRelativeSearchPath,
  bool shadowCopyFiles)

All these overloaded methods are static in nature and can be invoked without instantiating the class.  The first overloaded method accepts the name of the Application Domain to be created and creates an Application Domain.  However, the second accepts two parameters; the first one being the name of the Application Domain to be created while the second is a reference of the System.Security.Policy.Evidence for specifying the security policy for the application.  The third overload accepts an additional parameter, a reference of System.AppDomainSetup, which is used to configure how the assemblies would be loaded into the application.  The Evidence parameter refers to a collection of the security information on the application domain.  The last overloaded CreateDomain method accepts some additional parameters without defining a System.AppDomainSetup object reference.  The return value of each of these overloaded methods is an AppDomain object that represents the newly created application domain.

Go thr this link;

This link contain all the info abt the App Domain;

http://aspalliance.com/951#Page2

Best Luck!!!!!!!!!
Sujit.

Appdomain & .net remoting  Appdomain & .net remoting

23-Jun-08 10:13 AM

Check this out. just a little HelloWorld application to create an AppDomain in C#.

This is for a console application :

using System;
using System.Reflection;
using System.Runtime.Remoting;

public class ShowAppDomain : MarshalByRefObject
{
 public string GetAppDomainName()
 {
 return AppDomain.CurrentDomain.FriendlyName; // gets the Current application domain thread and returns it's nameAppDomain name
 }
}

public class CallMyHelloWorld
{
public static void Main ()
{
AppDomain ad = AppDomain.CreateDomain ("Yupee! My AppDomain!"); // create a new domain
ShowAppDomain ad2 = (ShowAppDomain) ad.CreateInstanceAndUnwrap(Assembly.GetCallingAssembly().GetName().Name,"ShowAppDomain");

/*
We use the AppDomain.CreateInstanceAndUnwrap method to Create a new instance of a specified type.
Assembly.GetCallingAssembly().GetName().Name returns The Assembly object and the name of the method that calls the presently executing method.
*/

Console.WriteLine("Here's my own AppDomain " + ad2.GetAppDomainName()); // Voila!
}
}

If you want one AppDomain to talk to another, then you should go for .net remoting, check the below article for moreinfo,

.NET Remoting and Cross Domain Marshalling

http://69.10.233.10/KB/IP/CrossDomainRemoting.aspx

Reply  Reply

23-Jun-08 01:41 PM

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())
Check thius out, it very good article:
http://www.beansoftware.com/NET-Tutorials/Application-Domain.aspx
Create New Account
help
have an idea of something I'm missing? Thanks ThunderMusic [Code] * ** Main App (calling method) * ** AppDomainSetup ads = new AppDomainSetup(); ads.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory; ads.PrivateBinPath = AppDomain.CurrentDomain.BaseDirectory; ads.DisallowBindingRedirects = false; ads.DisallowCodeDownload true; m_CurrentMonitorAppDomain = AppDomain.CreateDomain("MonitorLoader", null, ads); / / The following call fails MonitoringModuleLoader mml = (MonitoringModuleLoader)m_CurrentMonitorAppDomain.CreateInstanceAndUnwrap("ModuleLoader.dll", * ** The "ModuleLoader" library * ** namespace ModuleLoader { public class MonitoringModuleLoader : MarshalByRefObject { public MonitoringModuleLoader() { } public MonitorBase Load(byte[] LibBytes, string ModuleName) { return null; } } } [ / Code] .NET Discussions CurrentMonitorAppDomain.CreateInstanceAndUnwrap (1) AppDomain.CurrentDomain.BaseDirectory (1) ModuleLoader.MonitoringModuleLoader (1) CreateInstanceFromAndUnwrap (1 MarshalByRefObject (1) CreateInstanceAndUnwrap (1) AppDomain.CreateDomain (1) AppDomainSetup (1) finally, I found the problem. . . I was using 'CreateInstanceAndUnwrap' and should have used 'CreateInstanceFromAndUnwrap
es funktioniert nicht. Was muss ich tun? Hier mein Code . . . . Forms WPF exe . . . . void Start() { AppDomainSetup setup; string asmFilename = "AppDomainLibrary.dll"; if (!File.Exists(asmFilename)) { MessageBox.Show("Assembly: '" + asmFilename + "' existiert nicht!"); return; } setup = new AppDomainSetup(); domain = AppDomain.CreateDomain("NewAppDomain", new Evidence, setup); object obj = domain.CreateInstanceAndUnwrap("AppDomainLibrary", MethodInfo mi = obj.GetType().GetMethod("Run"); mi.Invoke(obj, null); obj = null; mi = null void Run() { / / Tu was } . . . Danke im Voraus Mfg Michael C# - German Discussions AppDomainLibrary.Class1 (1) CreateInstanceAndUnwrap (1) AppDomain.CreateDomain (1) NewAppDomain.Unload (1) AppDomainSetup (1) MethodInfo (1) AppDomain (1) Domain.CreateInstanceAndUnwrap (1) Hallo Michael, Deine Frage ist etwas verwirrend! ;-) Du möchtest AppDomainLibrary.dll austauschen oder Dirk . . .hier wäre ein guter Einstieg: http: / / msdn.microsoft.com / de-de / library / system.marshalbyrefobject.aspx . . .und hier wird sogar die Lösung aufgezeigt: http: / / weblogs.asp.net / ralfw / archive
Möglichkeit haben eine DLL Dynamisch zu laden und zu entladen. folgendes habe ich programmiert: . . . AppDomainSetup setup = new AppDomainSetup(); setup.ShadowCopyFiles = true; AppDomain domain = new AppDomain.CurrentDomain(); domain.CreaterDomain("C: \ Assambly.dll", null, setup Methode Run der Klasse Class1 laden. Was muss ich tun? Mfg Michael C# - German Discussions AppDomainSetup.ShadowCopyDirectories (1) AppDomain.CurrentDomain.Evidence (1) AppDomainSetup.ShadowCopyFiles (1) MarshalByRefObject (1) CreateInstanceAndUnwrap (1) AppDomain.CurrentDomain (1) AppDomain.CreateDomain (1) AppDomainSetup (1) Hallo Michael, namisch s1 Ein Beispiel: _ __ __ __ __ Class1.cs: namespace MyAssembly { public class Class1 MarshalByRefObject { public void Run() { MessageBox.Show("Methode Run aufgerufen."); } } } _ __ __ __ _ Form1.cs: private void Form1_Load(object
ein Thread gestartet, der in eine Datei schreiben soll. Code zum erzeugen der APP Domain: . . . AppDomainSetup setup; string asmFilename = "MyAssabmly.dll"; if (!File.Exists(asmFilename)) { MessageBox.Show("Assembly: '" +asmFilename+ "' existiert nicht!"); return; } setup = new AppDomainSetup(); setup.ShadowCopyFiles = "true"; domain = AppDomain.CreateDomain("NewAppDomain", new Evidence(), setup); object obj = domain CreateInstanceAndUnwrap("AppDomainLibrary", MethodInfo mi = obj.GetType().GetMethod("Run"); mi.Invoke(obj, null); . . . . Code der MyAssambly DLL: . . . public class Class1 : MarshalByRefObject { private static System.Threading.Thread m_pThreadMainMethod; public void Run() { / / Starten des Mainthreads m_pThreadMainMethod = new System Discussions System.Threading.Thread.Sleep (1) FileIOPermissionAccess.Write (1) FileIOPermissionAccess (1) System.IO.StreamWriter (1) MarshalByRefObject (1) AppDomainLibrary.Class1 (1) System.Threading.Thread (1) CreateInstanceAndUnwrap (1) Hallo Michael, Nun, da wird intern auf jeden Fall: FileIOPermissionAccess.Write gepr = FCft, was
Discussions System.Windows.Threading.DispatcherTimer (1) WpfBrowserApplication1.MarshalByRefType (1) AppDomain.CurrentDomain.BaseDirectory (1) CreateIDispatchSTAForwarder (1) MarshalByRefObject (1) CreateInstanceAndUnwrap (1) AppDomain.CreateDomain (1) RoutedEventArgs (1) Hello Steven Tang, Thanks for using Microsoft Newsgroup Support object sender, ExitEventArgs e) { Debug.Print("Application Exited"); } private void Page_Loaded(object sender, RoutedEventArgs e) { AppDomainSetup domaininfo = new AppDomainSetup(); domaininfo.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory; Application.Current.Exit + = new ExitEventHandler(Current_Exit); AppDomain newDomain = AppDomain.CreateDomain("NewDomain", null, domaininfo); MarshalByRefType mbrt = newDomain.CreateInstanceAndUnwrap(Assembly.GetExecutingAssembly().FullName, mbrt.SetTimer(); } } public class MarshalByRefType MarshalByRefObject { public void SetTimer() { System.Windows.Threading.DispatcherTimer timer = new System.Windows.Threading.DispatcherTimer(); timer.IsEnabled