Assembly : mscorlib (dans mscorlib.dll)
Syntaxe<ClassInterfaceAttribute(ClassInterfaceType.None)> _ <ComVisibleAttribute(True)> _ Public NotInheritable Class AppDomain Inherits MarshalByRefObject Implements _AppDomain, IEvidenceFactory
Dim instance As AppDomain
[ClassInterfaceAttribute(ClassInterfaceType.None)] [ComVisibleAttribute(true)] public sealed class AppDomain : MarshalByRefObject, _AppDomain, IEvidenceFactory
[ClassInterfaceAttribute(ClassInterfaceType::None)] [ComVisibleAttribute(true)] public ref class AppDomain sealed : public MarshalByRefObject, _AppDomain, IEvidenceFactory
/** @attribute ClassInterfaceAttribute(ClassInterfaceType.None) */ /** @attribute ComVisibleAttribute(true) */ public final class AppDomain extends MarshalByRefObject implements _AppDomain, IEvidenceFactory
ClassInterfaceAttribute(ClassInterfaceType.None) ComVisibleAttribute(true) public final class AppDomain extends MarshalByRefObject implements _AppDomain, IEvidenceFactory
NotesLes domaines d'application, représentés par des objets AppDomain, permettent de fournir les limites d'isolation, de déchargement et de sécurité pour l'exécution de code managé.
-
Utilisez les domaines d'application pour isoler les tâches qui peuvent interrompre un processus. Si l'état du AppDomain qui exécute une tâche devient instable, le AppDomain peut être déchargé sans affecter le processus. C'est important lorsqu'un processus doit s'exécuter pendant de longues périodes sans redémarrer. Vous pouvez également utiliser les domaines d'application pour isoler les tâches qui ne doivent pas partager de données.
-
Si un assembly est chargé dans le domaine d'application par défaut, il ne peut pas être déchargé de la mémoire tant que le processus s'exécute. Toutefois, si vous ouvrez un deuxième domaine d'application pour charger et exécuter l'assembly, l'assembly est déchargé lorsque ce domaine d'application est déchargé. Utilisez cette technique pour réduire le jeu de travail des longs processus qui utilisent parfois des DLL volumineuses.
Plusieurs domaines d'application peuvent s'exécuter dans un processus unique. Cependant, il n'existe pas de corrélation un à un entre les domaines d'application et les threads. Plusieurs threads peuvent appartenir à un même domaine d'application. Par ailleurs, même si un thread donné n'est pas confiné dans un domaine d'application unique, à un moment donné, il s'exécute dans un domaine d'application unique.
Les domaines d'application sont créés à l'aide de la méthode CreateDomain. Les instances de AppDomain permettent de charger et d'exécuter les assemblys (Assembly). Si AppDomain n'est plus utilisé, il peut être déchargé.
La classe AppDomain implémente un ensemble d'événements permettant aux applications de répondre lorsqu'un assembly est chargé, un domaine d'application est déchargé ou une exception non gérée est levée.
Pour plus d'informations sur l'utilisation des domaines d'application, consultez Domaines d'application.
Cette classe implémente les interfaces MarshalByRefObject, _AppDomain et IEvidenceFactory.
Ne créez jamais de wrapper accessible à distance pour un objet AppDomain. Vous risqueriez alors de publier une référence distante à AppDomain, en exposant des méthodes telles que CreateInstance pour l'accès à distance et en détruisant ainsi la sécurité d'accès du code pour AppDomain. Des clients malveillants qui se connectent à l'objet AppDomain distant pourraient alors accéder à toutes les ressources auxquelles l'objet AppDomain a lui-même accès. Vous ne devez pas créer de wrappers accessibles à distance pour un type qui étend MarshalByRefObject et qui implémente des méthodes dont des clients malveillants sont susceptibles de se servir pour outrepasser le système de sécurité.
Attention |
|---|
| La valeur par défaut de la propriété AppDomainSetup.DisallowCodeDownload est false. Ce paramètre n'est pas sécurisé pour les services. Pour empêcher les services de télécharger du code d'un niveau de confiance partiel, affectez à cette propriété la valeur true. |
Remarque sur la plate-forme Windows Mobile pour Pocket PC, Windows Mobile pour Smartphone, Windows CE : Le chargement des assemblys dans une zone de code neutre d'un domaine pour une utilisation par plusieurs domaines d'application n'est pas pris en charge.
| Topic | Location |
|---|---|
| Comment : charger des assemblys dans un domaine d'application | .NET Framework : notions de base de la programmation |
| Comment : configurer un domaine d'application | .NET Framework : notions de base de la programmation |
| Comment : créer un domaine d'application | .NET Framework : notions de base de la programmation |
| Comment : décharger un domaine d'application | .NET Framework : notions de base de la programmation |
ExempleCet exemple montre comment créer un nouveau AppDomain, instancier un type dans ce nouveau AppDomain, et communique avec l'objet de ce type. De plus, cet exemple montre comment décharger le AppDomain ce qui provoque la récupération de l'objet par le garbage collector.
Imports System Imports System.Reflection Imports System.Threading Module Module1 Sub Main() ' Get and display the friendly name of the default AppDomain. Dim callingDomainName As String = Thread.GetDomain().FriendlyName Console.WriteLine(callingDomainName) ' Get and display the full name of the EXE assembly. Dim exeAssembly As String = [Assembly].GetEntryAssembly().FullName Console.WriteLine(exeAssembly) ' Construct and initialize settings for a second AppDomain. Dim ads As New AppDomainSetup() ads.ApplicationBase = _ "file:///" + System.Environment.CurrentDirectory ads.DisallowBindingRedirects = False ads.DisallowCodeDownload = True ads.ConfigurationFile = _ AppDomain.CurrentDomain.SetupInformation.ConfigurationFile ' Create the second AppDomain. Dim ad2 As AppDomain = AppDomain.CreateDomain("AD #2", Nothing, ads) ' Create an instance of MarshalbyRefType in the second AppDomain. ' A proxy to the object is returned. Dim mbrt As MarshalByRefType = CType( _ ad2.CreateInstanceAndUnwrap(exeAssembly, _ GetType(MarshalByRefType).FullName), MarshalByRefType) ' 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) Try ' Call the method again. Note that this time it fails because ' the second AppDomain was unloaded. mbrt.SomeMethod(callingDomainName) Console.WriteLine("Sucessful call.") Catch e As AppDomainUnloadedException Console.WriteLine("Failed call; this is expected.") End Try End Sub End Module ' Because this class is derived from MarshalByRefObject, a proxy ' to a MarshalByRefType object can be returned across an AppDomain ' boundary. Public Class MarshalByRefType Inherits MarshalByRefObject ' Call this method via a proxy. Public Sub SomeMethod(ByVal callingDomainName As String) ' Get this AppDomain's settings and display some of them. Dim ads As AppDomainSetup = AppDomain.CurrentDomain.SetupInformation Console.WriteLine("AppName={0}, AppBase={1}, ConfigFile={2}", _ ads.ApplicationName, ads.ApplicationBase, ads.ConfigurationFile) ' Display the name of the calling AppDomain and the name ' of the second domain. ' NOTE: The application's thread has transitioned between ' AppDomains. Console.WriteLine("Calling from '{0}' to '{1}'.", _ callingDomainName, Thread.GetDomain().FriendlyName) End Sub End Class 'This code produces output similar to the following: ' ' AppDomainX.exe ' AppDomainX, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null ' AppName=, AppBase=C:\AppDomain\bin, ConfigFile=C:\AppDomain\bin\AppDomainX.exe.config ' Calling from 'AppDomainX.exe' to 'AD #2'. ' Failed call; this is expected.
using System; using System.Reflection; using System.Threading; class Module1 { public static void Main() { // Get and display the friendly name of the default AppDomain. string callingDomainName = Thread.GetDomain().FriendlyName; Console.WriteLine(callingDomainName); // Get and display the full name of the EXE assembly. string exeAssembly = Assembly.GetEntryAssembly().FullName; Console.WriteLine(exeAssembly); // Construct and initialize settings for a second AppDomain. AppDomainSetup ads = new AppDomainSetup(); ads.ApplicationBase = "file:///" + 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); // Create an instance of MarshalbyRefType in the second AppDomain. // A proxy to the object is returned. MarshalByRefType mbrt = (MarshalByRefType) ad2.CreateInstanceAndUnwrap( exeAssembly, typeof(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); try { // Call the method again. Note that this time it fails // because the second AppDomain was unloaded. mbrt.SomeMethod(callingDomainName); Console.WriteLine("Sucessful call."); } catch(AppDomainUnloadedException) { Console.WriteLine("Failed call; this is expected."); } } } // Because this class is derived from MarshalByRefObject, a proxy // to a MarshalByRefType object can be returned across an AppDomain // boundary. public class MarshalByRefType : MarshalByRefObject { // Call this method via a proxy. public void SomeMethod(string callingDomainName) { // Get this AppDomain's settings and display some of them. AppDomainSetup ads = AppDomain.CurrentDomain.SetupInformation; Console.WriteLine("AppName={0}, AppBase={1}, ConfigFile={2}", ads.ApplicationName, ads.ApplicationBase, ads.ConfigurationFile ); // Display the name of the calling AppDomain and the name // of the second domain. // NOTE: The application's thread has transitioned between // AppDomains. Console.WriteLine("Calling from '{0}' to '{1}'.", callingDomainName, Thread.GetDomain().FriendlyName ); } } /* This code produces output similar to the following: AppDomainX.exe AppDomainX, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null AppName=, AppBase=C:\AppDomain\bin, ConfigFile=C:\AppDomain\bin\AppDomainX.exe.config Calling from 'AppDomainX.exe' to 'AD #2'. Failed call; this is expected. */
using namespace System; using namespace System::Reflection; using namespace System::Threading; using namespace System::Security::Policy; // Because this class is derived from MarshalByRefObject, a proxy // to a MarshalByRefType object can be returned across an AppDomain // boundary. ref class MarshalByRefType : MarshalByRefObject { public: // Call this method via a proxy. void SomeMethod(String^ callingDomainName) { // Get this AppDomain's settings and display some of them. AppDomainSetup^ ads = AppDomain::CurrentDomain->SetupInformation; Console::WriteLine("AppName={0}, AppBase={1}, ConfigFile={2}", ads->ApplicationName, ads->ApplicationBase, ads->ConfigurationFile ); // Display the name of the calling AppDomain and the name // of the second domain. // NOTE: The application's thread has transitioned between // AppDomains. Console::WriteLine("Calling from '{0}' to '{1}'.", callingDomainName, Thread::GetDomain()->FriendlyName ); }; }; void main() { // Get and display the friendly name of the default AppDomain. String^ callingDomainName = Thread::GetDomain()->FriendlyName; Console::WriteLine(callingDomainName); // Get and display the full name of the EXE assembly. String^ exeAssembly = Assembly::GetEntryAssembly()->FullName; Console::WriteLine(exeAssembly); // Construct and initialize settings for a second AppDomain. AppDomainSetup^ ads = gcnew AppDomainSetup(); ads->ApplicationBase = "file:///" + 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", AppDomain::CurrentDomain->Evidence, ads); // Create an instance of MarshalbyRefType in the second AppDomain. // A proxy to the object is returned. MarshalByRefType^ mbrt = (MarshalByRefType^) ad2->CreateInstanceAndUnwrap( exeAssembly, MarshalByRefType::typeid->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); try { // Call the method again. Note that this time it fails // because the second AppDomain was unloaded. mbrt->SomeMethod(callingDomainName); Console::WriteLine("Sucessful call."); } catch(AppDomainUnloadedException^) { Console::WriteLine("Failed call; this is expected."); } } /* This code produces output similar to the following: AppDomainX.exe AppDomainX, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null AppName=, AppBase=C:\AppDomain\bin, ConfigFile=C:\AppDomain\bin\AppDomainX.exe.config Calling from 'AppDomainX.exe' to 'AD #2'. Failed call; this is expected. */
Plates-formesWindows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile pour Pocket PC, Windows Mobile pour Smartphone, Windows Server 2003, Windows XP Édition Media Center, Windows XP Professionnel Édition x64, Windows XP SP2, Windows XP Starter Edition
Le .NET Framework ne prend pas en charge toutes les versions de chaque plate-forme. Pour obtenir la liste des versions prises en charge, consultez Configuration requise.
Attention
Outils (masquer)
S'enregistrer
Liste des Membres
Qui est en ligne?
FAQ