방법: 애플리케이션 도메인 언로드

애플리케이션 도메인 사용을 마쳤으면 AppDomain.Unload 메서드를 사용하여 언로드합니다. Unload 메서드는 지정된 애플리케이션 도메인을 정상적으로 종료합니다. 언로드 프로세스 중에는 새 스레드가 애플리케이션 도메인에 액세스할 수 없으며 모든 애플리케이션 도메인 특정 데이터 구조가 비워집니다.

애플리케이션 도메인에 로드된 어셈블리가 제거되고 더 이상 사용할 수 없습니다. 애플리케이션 도메인의 어셈블리가 도메인 중립적인 경우 해당 어셈블리의 데이터는 전체 프로세스가 종료될 때까지 메모리에 남아 있습니다. 따라서 도메인 중립 어셈블리를 언로드하려면 전체 프로세스를 종료하는 것이 유일한 메커니즘입니다. 경우에 따라 애플리케이션 도메인에 대한 언로드 요청이 제대로 작동하지 않아 CannotUnloadAppDomainException이 발생할 수 있습니다.

다음 예제에서는 MyDomain이라는 새 애플리케이션 도메인을 만들고, 콘솔에 일부 정보를 출력한 다음, 해당 애플리케이션 도메인을 언로드합니다. 이 코드에서는 언로드된 애플리케이션 도메인의 이름을 콘솔에 출력하려고 합니다. 이 동작은 프로그램 끝에 있는 try/catch 문으로 처리되는 예외를 생성합니다.

예시

using namespace System;
using namespace System::Reflection;

ref class AppDomain2
{
public:
    static void Main()
    {
        Console::WriteLine("Creating new AppDomain.");
        AppDomain^ domain = AppDomain::CreateDomain("MyDomain", nullptr);

        Console::WriteLine("Host domain: " + AppDomain::CurrentDomain->FriendlyName);
        Console::WriteLine("child domain: " + domain->FriendlyName);
        AppDomain::Unload(domain);
        try
        {
            Console::WriteLine();
            Console::WriteLine("Host domain: " + AppDomain::CurrentDomain->FriendlyName);
            // The following statement creates an exception because the domain no longer exists.
            Console::WriteLine("child domain: " + domain->FriendlyName);
        }
        catch (AppDomainUnloadedException^ e)
        {
            Console::WriteLine(e->GetType()->FullName);
            Console::WriteLine("The appdomain MyDomain does not exist.");
        }
    }
};

int main()
{
    AppDomain2::Main();
}
using System;
using System.Reflection;

class AppDomain2
{
    public static void Main()
    {
        Console.WriteLine("Creating new AppDomain.");
        AppDomain domain = AppDomain.CreateDomain("MyDomain", null);

        Console.WriteLine("Host domain: " + AppDomain.CurrentDomain.FriendlyName);
        Console.WriteLine("child domain: " + domain.FriendlyName);
        try
        {
            AppDomain.Unload(domain);
            Console.WriteLine();
            Console.WriteLine("Host domain: " + AppDomain.CurrentDomain.FriendlyName);
            // The following statement creates an exception because the domain no longer exists.
            Console.WriteLine("child domain: " + domain.FriendlyName);
        }
        catch (AppDomainUnloadedException e)
        {
            Console.WriteLine(e.GetType().FullName);
            Console.WriteLine("The appdomain MyDomain does not exist.");
        }
    }
}
Imports System.Reflection

Class AppDomain2
    Public Shared Sub Main()
        Console.WriteLine("Creating new AppDomain.")
        Dim domain As AppDomain = AppDomain.CreateDomain("MyDomain", Nothing)

        Console.WriteLine("Host domain: " + AppDomain.CurrentDomain.FriendlyName)
        Console.WriteLine("child domain: " + domain.FriendlyName)
        AppDomain.Unload(domain)
        Try
            Console.WriteLine()
            Console.WriteLine("Host domain: " + AppDomain.CurrentDomain.FriendlyName)
            ' The following statement creates an exception because the domain no longer exists.
            Console.WriteLine("child domain: " + domain.FriendlyName)
        Catch e As AppDomainUnloadedException
            Console.WriteLine(e.GetType().FullName)
            Console.WriteLine("The appdomain MyDomain does not exist.")
        End Try
    End Sub
End Class

참고 항목