Microsoft MVP성태의 닷넷 이야기
.NET Framework: 447. w3wp.exe AppPool 재생(recycle)하는 방법 정리 [링크 복사], [링크+제목 복사],
조회: 21540
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
 

(시리즈 글이 7개 있습니다.)
.NET Framework: 174. 작업자 프로세스(w3wp.exe)가 재시작되는 시점을 알 수 있는 방법
; https://www.sysnet.pe.kr/2/0/841

.NET Framework: 447. w3wp.exe AppPool 재생(recycle)하는 방법 정리
; https://www.sysnet.pe.kr/2/0/1704

개발 환경 구성: 246. IIS 작업자 프로세스의 20분 자동 재생(Recycle)을 끄는 방법
; https://www.sysnet.pe.kr/2/0/1774

.NET Framework: 643. 작업자 프로세스(w3wp.exe)가 재시작되는 시점을 알 수 있는 방법 - 두 번째 이야기
; https://www.sysnet.pe.kr/2/0/11145

개발 환경 구성: 702. IIS - AppPool의 "Disable Overlapped Recycle" 옵션
; https://www.sysnet.pe.kr/2/0/13514

닷넷: 2196. IIS - AppPool의 "Disable Overlapped Recycle" 옵션의 부작용
; https://www.sysnet.pe.kr/2/0/13516

닷넷: 2274. IIS - (프로세스 종료 없는) AppDomain Recycle
; https://www.sysnet.pe.kr/2/0/13672




w3wp.exe AppPool 재생(recycle)하는 방법 정리

사실 w3wp.exe에 대한 재생 방법은 검색해 보면 많은 글이 나옵니다.

Restarting (Recycling) an Application Pool
; http://stackoverflow.com/questions/249927/restarting-recycling-an-application-pool

IIS 7 이상이라면 간단하게 Microsoft.Web.Administration.dll 어셈블리에서 제공하는 ServerManager 객체를 이용할 수 있습니다.

ServerManager svr = new ServerManager(); 
ApplicationPool appPool = svr.ApplicationPools[appPoolId]; // appPoolId == IIS AppPools에 등록된 Application Pool 이름

if (appPool != null)
{
    appPool.Recycle();
}

하지만 IIS 6 이하에서는 Directory Services를 이용해 처리해야 합니다.

string appPoolPath = "IIS://localhost/W3SVC/AppPools/" + appPool;

using (DirectoryEntry appPoolEntry = new DirectoryEntry(appPoolPath))
{
    appPoolEntry.Invoke("Recycle", null);
    appPoolEntry.Close();
}

위의 방법은 IIS 7이상에서도 잘 동작하지만 한 가지 제약이 있습니다. 다음과 같이 "IIS 6 Management Compatibility" 옵션이 켜져있어야 하는데, 생각보다 이 옵션이 켜져 있는 IIS 7(+) 시스템이 많지 않습니다.

recycle_app_pool_1.png

그래서 안전하게 하고 싶다면 IIS 6과 7을 구분해서 각각의 recycle 명령을 호출하도록 바꿔야 합니다.

public static bool IsVistaOrLater
{
    get { return Environment.OSVersion.Version.Major >= 6; }
}

참고로 "HttpRuntime.UnloadAppDomain();" 메서드가 recycle과 유사한 효과를 내긴 합니다. 하지만 이 메서드는 w3wp.exe에 대한 재생을 하는 것은 아니고 내부의 웹 가상 응용 프로그램에 대한 AppDomain만 내렸다가 다시 올립니다. 말로만 듣던 프로세스(EXE) 내부의 Application Domain 격리 효과를 직접 눈으로 볼 수 있는 몇 안되는 사례입니다.




그런데 ServerManager를 사용하면 한 가지 문제가 있습니다. 바로 GAC에 등록된 Microsoft.Web.Administration.dll 어셈블리를 로드하는 방법입니다. 제 컴퓨터에 보니 7.0.0.0 버전과 7.9.0.0 버전이 있는데, 향후 지원을 생각한다면 버전을 고정할 수도 없고... 다소 애매합니다. (사실 쓸데없는 걱정일 수 있습니다. 윈도우 2008부터 2012까지 모두 7.0.0.0 버전이 등록되어 있습니다.)

그래서 제가 찾아봤던 것이 다음의 방법입니다.

Assembly.Load를 이용해 GAC에 등록된 어셈블리를 로드하는 방법
; https://www.sysnet.pe.kr/2/0/1703

이 방법을 이용해서 .NET Reflection과 결합해 다음과 같이 자연스럽게 문제를 해결하는 듯했습니다. ^^

try
{
    string gacAsmName = GetAssemblyPath("Microsoft.Web.Administration"); // 7.9.0.0 버전의 어셈블리 로드
    Assembly asm = Assembly.LoadFrom(gacAsmName);
    if (asm == null)
    {
        return;
    }

    System.Type targetType = asm.GetType("Microsoft.Web.Administration.ServerManager");
    if (targetType == null)
    {
        return;
    }

    object objValue = Activator.CreateInstance(targetType);
    if (objValue == null)
    {
        return;
    }

    PropertyInfo apProps = targetType.GetProperty("ApplicationPools", BindingFlags.Instance | BindingFlags.Public);
    if (apProps == null)
    {
        return;
    }

    object appPoolColl = apProps.GetValue(objValue, null);

    if (appPoolColl == null)
    {
        return;
    }

    object appPoolElem = null;
    foreach (PropertyInfo prop in appPoolColl.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance))
    {
        foreach (ParameterInfo propParam in prop.GetIndexParameters())
        {
            if (propParam.ParameterType.FullName == "System.String")
            {
                appPoolElem = prop.GetValue(appPoolColl, new object[] { appPoolId });
                break;
            }
        }
    }

    if (appPoolElem == null)
    {
        return;
    }

    MethodInfo recycleMethod = appPoolElem.GetType().GetMethod("Recycle");
    recycleMethod.Invoke(appPoolElem, null);
}
catch { }

그런데 IIS + ASP.NET에서 실행했더니 다음과 같은 예외가 발생합니다.

System.Reflection.TargetInvocationException was caught
  Message=Exception has been thrown by the target of an invocation.
  Source=mscorlib
  StackTrace:
       at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
       at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
       at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
       at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
       at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture)
       at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, Object[] index)

  InnerException: System.IO.FileNotFoundException
       Message=Filename: redirection.config
Error: Cannot read configuration file

문제 범위를 축소하기 위해 .NET Reflection이 아닌 ServerManager 객체를 곧바로 사용해 보았는데, ApplicationPools 객체를 반환받는 과정에서 오류가 나는 것을 확인했습니다.

ServerManager svr = new ServerManager();

ApplicationPoolCollection apPools = svr.ApplicationPools; // 예외 발생

foreach (var item in appPools)
{
    Console.WriteLine(item.Name);
}

System.IO.FileNotFoundException occurred
  HResult=-2147024894
  Message=Filename: redirection.config
Error: Cannot read configuration file

  Source=""
  StackTrace:
       at Microsoft.Web.Administration.Interop.AppHostWritableAdminManager.GetAdminSection(String bstrSectionName, String bstrSectionPath)
       at Microsoft.Web.Administration.Configuration.GetSectionInternal(ConfigurationSection section, String sectionPath, String locationPath)
  InnerException: 

아니... 뜬금없이 redirection.config 파일을 찾을 수 없다고 나옵니다. 좀 더 문제 범위를 축소하기 위해 IIS + ASP.NET이 아닌 단순 콘솔 프로그램을 만들어 테스트 했는데, 이번에는 문제 없이 잘 동작해서 다음과 같은 출력 결과를 얻었습니다.

Clr4IntegratedAppPool
Clr4ClassicAppPool
Clr2IntegratedAppPool
Clr2ClassicAppPool
UnmanagedClassicAppPool

오호~~~ 그런데 이상하군요. 콘솔에서 잘 동작하는 것은 둘째치고, 출력 결과로 나온 AppPool의 목록이 IIS 서버의 것이 아닌 IIS Express의 것처럼 보였습니다. 확인을 위해 "%USERPROFILE%\My Documents\IISExpress\config\applicationhost.config" 파일을 열고 appPool 목록을 확인하니 일치했습니다.

더욱 재미있는 것은, 7.0.0.0 버전의 Microsoft.Web.Administration.dll 어셈블리를 참조하는 경우 정상적으로 IIS 서버의 것을 가져왔다는 점입니다.

DefaultAppPool
Classic .NET AppPool
.NET v2.0 Classic
.NET v2.0
.NET v4.5 Classic
.NET v4.5

현상을 정리해 보면, 7.9.0.0 버전의 Microsoft.Web.Administration.dll 어셈블리를 IIS에서 직접 호스팅하는 웹 애플리케이션(ASP.NET)의 페이지(.aspx)에서 테스트하면 오류가 발생하고 7.0.0.0 버전의 경우에는 동작을 합니다.

이에 기반해서 검색을 해보니 답이 나오더군요. ^^

Microsoft.Web.Administration.ServerManager looking in wrong directory for IISExpress applicationHost.config
; http://stackoverflow.com/questions/11208270/microsoft-web-administration-servermanager-looking-in-wrong-directory-for-iisexp

아래의 글이 눈에 띄는데요.

How are you trying to get the application pools? Are you using MWH (Microsoft.Web.Administration) APIs?
1.Full IIS ships with Microsoft.Web.Administration.dll (version 7.0.0.0). 
2.IIS Express ships with a different version of Microsoft.Web.Administration.dll (version 7.9.0.0). 

Microsoft.Web.Administration (MWA) version 7.9.0.0 is shipped with IIS Express 7.5 and it is only used by IIS Express. 

한마디로, Microsoft.Web.Administration.dll 어셈블리의 7.0.0.0 버전은 "IIS 서버"를 위한 것이고, 7.9.0.0 버전은 "IIS Express"를 위한 것입니다. 이후에 나온 IIS Express 7.5는 Microsoft.Web.Administration 어셈블리 사용에 대한 동작을 자연스럽게 일치시키기 위해 aspnet.config 파일에 다음과 같이 7.0.0.0을 7.9.0.0으로 사용하도록 bindingRedirect를 지정한다는 설명도 나옵니다.

<dependentAssembly>
  <assemblyIdentity name="Microsoft.Web.Administration"
                    publicKeyToken="31bf3856ad364e35"
                    culture="neutral" />
  <bindingRedirect oldVersion="7.0.0.0"
                   newVersion="7.9.0.0" />
  <codeBase version="7.9.0.0" 
            href="FILE://%FalconBin%/Microsoft.Web.Administration.dll" />
</dependentAssembly>

여기까지 보고 나니 모든 상황이 이해가 되었습니다. 제가 7.9.0.0 버전에 포함된 ServerManager를 테스트했던 웹 사이트는 "Local SYSTEM"에서 구동했기 때문에 "%USERPROFILE%" 경로가 "C:\Windows\system32\config\systemprofile"로 설정되었고, 그 하위의 "\Documents\IISExpress\config" 폴더에서 redirection.config 파일을 찾으니 그와 같은 예외가 발생한 것입니다.

redirection.config 파일은 IIS Express 설정 폴더에는 제공되지만 IIS 서버에는 제공되지 않습니다.

반면, 7.0.0.0은 무조건 IIS 서버를 바라보기 때문에 redirection.config같은 것은 찾지 않으므로 정상 동작하는 것이었고!

결국, 최신 버전의 Microsoft.Web.Administration.dll을 사용하도록 배려한 것이 잘못된 결과를 가져왔습니다. IIS 서버를 위해서는 7.0.0.0 버전으로 고정해서 어셈블리를 사용해야만 하는 제약이 생긴 것입니다. 과연... 마이크로소프트가 향후 이 2가지 기능의 Microsoft.Web.Administration 어셈블리 파일을 어떤 식으로 버전 개정을 해나갈지 기대(?)가 되는군요. ^^





[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]







[최초 등록일: ]
[최종 수정일: 7/10/2024]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 




1  [2]  3  4  5  6  7  8  9  10  11  12  13  14  15  ...
NoWriterDateCnt.TitleFile(s)
13893정성태2/27/20252211Linux: 115. eBPF (bpf2go) - ARRAY / HASH map 기본 사용법
13892정성태2/24/20252963닷넷: 2325. C# - PowerShell과 연동하는 방법파일 다운로드1
13891정성태2/23/20252489닷넷: 2324. C# - 프로세스의 성능 카운터용 인스턴스 이름을 구하는 방법파일 다운로드1
13890정성태2/21/20252316닷넷: 2323. C# - 프로세스 메모리 중 Private Working Set 크기를 구하는 방법(Win32 API)파일 다운로드1
13889정성태2/20/20253038닷넷: 2322. C# - 프로세스 메모리 중 Private Working Set 크기를 구하는 방법(성능 카운터, WMI) [1]파일 다운로드1
13888정성태2/17/20252483닷넷: 2321. Blazor에서 발생할 수 있는 async void 메서드의 부작용
13887정성태2/17/20253063닷넷: 2320. Blazor의 razor 페이지에서 code-behind 파일로 코드를 분리 및 DI 사용법
13886정성태2/15/20252572VS.NET IDE: 196. Visual Studio - Code-behind처럼 cs 파일을 그룹핑하는 방법
13885정성태2/14/20253230닷넷: 2319. ASP.NET Core Web API / Razor 페이지에서 발생할 수 있는 async void 메서드의 부작용
13884정성태2/13/20253503닷넷: 2318. C# - (async Task가 아닌) async void 사용 시의 부작용파일 다운로드1
13883정성태2/12/20253255닷넷: 2317. C# - Memory Mapped I/O를 이용한 PCI Configuration Space 정보 열람파일 다운로드1
13882정성태2/10/20252577스크립트: 70. 파이썬 - oracledb 패키지 연동 시 Thin / Thick 모드
13881정성태2/7/20252823닷넷: 2316. C# - Port I/O를 이용한 PCI Configuration Space 정보 열람파일 다운로드1
13880정성태2/5/20253167오류 유형: 947. sshd - Failed to start OpenSSH server daemon.
13879정성태2/5/20253389오류 유형: 946. Ubuntu - N: Updating from such a repository can't be done securely, and is therefore disabled by default.
13878정성태2/3/20253182오류 유형: 945. Windows - 최대 절전 모드 시 DRIVER_POWER_STATE_FAILURE 발생 (pacer.sys)
13877정성태1/25/20253236닷넷: 2315. C# - PCI 장치 열거 (레지스트리, SetupAPI)파일 다운로드1
13876정성태1/25/20253691닷넷: 2314. C# - ProcessStartInfo 타입의 Arguments와 ArgumentList파일 다운로드1
13875정성태1/24/20253123스크립트: 69. 파이썬 - multiprocessing 패키지의 spawn 모드로 동작하는 uvicorn의 workers
13874정성태1/24/20253542스크립트: 68. 파이썬 - multiprocessing Pool의 기본 프로세스 시작 모드(spawn, fork)
13873정성태1/23/20252969디버깅 기술: 217. WinDbg - PCI 장치 열거파일 다운로드1
13872정성태1/23/20252880오류 유형: 944. WinDbg - 원격 커널 디버깅이 연결은 되지만 Break (Ctrl + Break) 키를 눌러도 멈추지 않는 현상
13871정성태1/22/20253291Windows: 278. Windows - 윈도우를 다른 모니터 화면으로 이동시키는 단축키 (Window + Shift + 화살표)
13870정성태1/18/20253730개발 환경 구성: 741. WinDbg - 네트워크 커널 디버깅이 가능한 NIC 카드 지원 확대
13869정성태1/18/20253452개발 환경 구성: 740. WinDbg - _NT_SYMBOL_PATH 환경 변수에 설정한 경로로 심벌 파일을 다운로드하지 않는 경우
13868정성태1/17/20253109Windows: 277. Hyper-V - Windows 11 VM의 Enhanced Session 모드로 로그인을 할 수 없는 문제
1  [2]  3  4  5  6  7  8  9  10  11  12  13  14  15  ...