AppDomain-Klasse

Stellt eine Anwendungsdomäne dar. Dies ist eine isolierte Umgebung, in der Programme ausgeführt werden. Diese Klasse kann nicht vererbt werden.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

Syntax

'Declaration
<ClassInterfaceAttribute(ClassInterfaceType.None)> _
<ComVisibleAttribute(True)> _
Public NotInheritable Class AppDomain
    Inherits MarshalByRefObject
    Implements _AppDomain, IEvidenceFactory
'Usage
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

Hinweise

Anwendungsdomänen werden durch AppDomain-Objekte dargestellt und unterstützen die Bereitstellung von Begrenzungen der Isolation, Entladung und Sicherheit für die Ausführung von verwaltetem Code.

  • Verwenden Sie Anwendungsdomänen, um Aufgaben zu isolieren, die einen Prozess zum Absturz bringen könnten. Wenn der Zustand der AppDomain, in der eine Aufgabe ausgeführt wird, instabil wird, kann die AppDomain ohne Auswirkungen auf den Prozess entladen werden. Dies ist wichtig, wenn ein Prozess über einen längeren Zeitraum ohne Neustart ausgeführt werden muss. Sie können auch Anwendungsdomänen verwenden, um Aufgaben zu isolieren, die keine Daten gemeinsam verwenden sollten.

  • Wenn eine Assembly in die Standardanwendungsdomäne geladen wird, kann sie während der Ausführung des Prozesses nicht aus dem Speicher entladen werden. Wenn Sie hingegen eine zweite Anwendungsdomäne zum Laden und Ausführen der Assembly öffnen, wird die Assembly beim Entladen dieser Anwendungsdomäne entladen. Verwenden Sie dieses Verfahren, um das Workingset von Prozessen mit langer Laufzeit zu minimieren, die gelegentlich große DLLs verwenden.

In einem Prozess können mehrere Anwendungsdomänen ausgeführt werden. Allerdings besteht keine Eins-zu-Eins-Entsprechung zwischen Anwendungsdomänen und Threads. Zu einer Anwendungsdomäne können mehrere Threads gehören. Obwohl ein Thread nicht auf eine einzige Anwendungsdomäne beschränkt ist, wird ein Thread immer nur in einer einzigen Anwendungsdomäne ausgeführt.

Anwendungsdomänen werden mit der CreateDomain-Methode erstellt. AppDomain-Instanzen werden zum Laden und Ausführen von Assemblys (Assembly) verwendet. Wenn eine AppDomain nicht mehr verwendet wird, kann sie entladen werden.

Die AppDomain-Klasse implementiert eine Gruppe von Ereignissen, die es Anwendungen ermöglichen zu antworten, wenn eine Assembly geladen, eine Anwendungsdomäne entladen oder eine nicht behandelte Ausnahme ausgelöst wird.

Weitere Informationen über die Verwendung von Anwendungsdomänen finden Sie unter Anwendungsdomänen.

Diese Klasse implementiert die MarshalByRefObject-Schnittstelle, die _AppDomain-Schnittstelle und die IEvidenceFactory-Schnittstelle.

Erstellen Sie unter keinen Umständen einen remotefähigen Wrapper für ein AppDomain-Objekt. Dadurch wird möglicherweise ein Remoteverweis auf diese AppDomain veröffentlich, und Methoden wie CreateInstance werden für einen Remotezugriff offen gelegt, sodass die Codezugriffssicherheit für diese AppDomain zunichte gemacht wird. Böswillige Clients, die eine Verbindung mit der Remote-AppDomain herstellen, können sich Zugriff auf eine beliebige Ressource verschaffen, auf die die AppDomain selbst Zugriff hat. Erstellen Sie keine remotefähigen Wrapper für von MarshalByRefObject abgeleitete Typen und für Typen, die Methoden implementieren, mit denen böswillige Clients das Sicherheitssystem umgehen können.

Warnung

Der Standardwert für die AppDomainSetup.DisallowCodeDownload-Eigenschaft ist false. Diese Einstellung ist für Dienste unsicher. Legen Sie die diese Eigenschaft auf true fest, um zu verhindern, dass Dienste teilweise vertrauenswürdigen Code downloaden.

Hinweis zu Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows CE: Das Laden von Assemblys in einen domänenneutralen Codebereich für die Verwendung durch mehrere Anwendungsdomänen wird nicht unterstützt.

Thema Position
Gewusst wie: Laden von Assemblys in eine Anwendungsdomäne .NET Framework: Grundlagen der Programmierung
Gewusst wie: Konfigurieren einer Anwendungsdomäne .NET Framework: Grundlagen der Programmierung
Gewusst wie: Erstellen einer Anwendungsdomäne .NET Framework: Grundlagen der Programmierung
Gewusst wie: Entladen einer Anwendungsdomäne .NET Framework: Grundlagen der Programmierung
Gewusst wie: Konfigurieren einer Anwendungsdomäne .NET Framework: Grundlagen der Programmierung
Gewusst wie: Laden von Assemblys in eine Anwendungsdomäne .NET Framework: Grundlagen der Programmierung
Gewusst wie: Erstellen einer Anwendungsdomäne .NET Framework: Grundlagen der Programmierung
Gewusst wie: Entladen einer Anwendungsdomäne .NET Framework: Grundlagen der Programmierung

Beispiel

In diesem Beispiel wird veranschaulicht, wie eine neue AppDomain erstellt, in dieser neuen AppDomain ein Typ instanziiert und mit dem Objekt dieses Typs kommuniziert werden kann. Außerdem veranschaulicht dieses Beispiel das Entladen der AppDomain, sodass das Objekt von der Garbage Collection freigegeben wird.

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.
 */

Vererbungshierarchie

System.Object
   System.MarshalByRefObject
    System.AppDomain

Threadsicherheit

Alle öffentlichen statischen (Shared in Visual Basic) Member dieses Typs sind threadsicher. Bei Instanzmembern ist die Threadsicherheit nicht gewährleistet.

Plattformen

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0, 1.0

Siehe auch

Referenz

AppDomain-Member
System-Namespace