방법: 애플리케이션 도메인 언로드
애플리케이션 도메인 사용을 마쳤으면 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
참고 항목
GitHub에서 Microsoft와 공동 작업
이 콘텐츠의 원본은 GitHub에서 찾을 수 있으며, 여기서 문제와 끌어오기 요청을 만들고 검토할 수도 있습니다. 자세한 내용은 참여자 가이드를 참조하세요.
.NET
피드백
https://aka.ms/ContentUserFeedback
출시 예정: 2024년 내내 콘텐츠에 대한 피드백 메커니즘으로 GitHub 문제를 단계적으로 폐지하고 이를 새로운 피드백 시스템으로 바꿀 예정입니다. 자세한 내용은 다음을 참조하세요.다음에 대한 사용자 의견 제출 및 보기