성태의 닷넷 이야기
홈 주인
모아 놓은 자료
프로그래밍
질문/답변
사용자 관리
사용자
메뉴
아티클
외부 아티클
유용한 코드
온라인 기능
MathJax 입력기
최근 덧글
[정성태] Roll A Lisp In C - Reading ; https...
[정성태] Java - How to use the Foreign Funct...
[정성태] 제가 큰 실수를 했군요. ^^; Delegate를 통한 Bein...
[정성태] Working with Rust Libraries from C#...
[정성태] Detecting blocking calls using asyn...
[정성태] 아쉽게도, 커뮤니티는 아니고 개인 블로그입니다. ^^
[정성태] 질문이 잘 이해가 안 됩니다. 우선, 해당 소스코드에서 ILis...
[양승조
] var대신 dinamic으로 선언해서 해결은 했습니다. 맞는 해...
[양승조
] 또 막혔습니다. ㅠㅠ var list = props[i].Ge...
[양승조
] 아. 감사합니다. 어제는 안됐던것 같은데....정신을 차려야겠네...
글쓰기
제목
이름
암호
전자우편
HTML
홈페이지
유형
제니퍼 .NET
닷넷
COM 개체 관련
스크립트
VC++
VS.NET IDE
Windows
Team Foundation Server
디버깅 기술
오류 유형
개발 환경 구성
웹
기타
Linux
Java
DDK
Math
Phone
Graphics
사물인터넷
부모글 보이기/감추기
내용
<div style='display: inline'> <h1 style='font-family: Malgun Gothic, Consolas; font-size: 20pt; color: #006699; text-align: center; font-weight: bold'>IIS 작업자 프로세스 재생(recycle)하는 방법</h1> <p> 아래의 글에 따라,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > How to Programmatically Recycle Application Pools In C# ; <a target='tab' href='http://www.sharepointsecurity.com/sharepoint/how-to-programmatically-recycle-application-pools-in-c/'>http://www.sharepointsecurity.com/sharepoint/how-to-programmatically-recycle-application-pools-in-c/</a> </pre> <br /> System.Management 어셈블리를 참조 추가한 다음 다음과 같이 <a target='tab' href='https://docs.microsoft.com/en-us/previous-versions/iis/6.0-sdk/ms525600(v=vs.90)'>IIsApplicationPool</a>에 대한 객체 목록을 WMI를 통해 구한 후 InvokeMethod를 호출하면 w3wp.exe 프로세스가 재생(recycle)됩니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > ManagementObjectSearcher searcher = new ManagementObjectSearcher (@"root\microsoftiisv2", "SELECT * FROM IIsApplicationPool"); foreach (ManagementObject obj in searcher.Get()) { obj.InvokeMethod("Recycle", null); } </pre> <br /> 원하는 AppPool을 recycle하려면 다음과 같은 식으로 코드를 만들 수 있습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > using System; using System.Management; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { RecycleIISWorkerProcess("DefaultAppPool"); } internal static void RecycleIISWorkerProcess(string targetAppPoolToRecycle) { ManagementObjectSearcher searcher = new ManagementObjectSearcher (@"root\microsoftiisv2", "SELECT * FROM IIsApplicationPool"); foreach (ManagementObject obj in searcher.Get()) { string appPoolName = obj.GetPropertyValue("Name") as string; if ("W3SVC/APPPOOLS/" + targetAppPoolToRecycle == appPoolName) { obj.InvokeMethod("Recycle", null); } } } } } </pre> <br /> 또는 WMI Query에 WHERE 조건을 주어 성능을 쬐끔 더 높일 수 있습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > ManagementObjectSearcher searcher = new ManagementObjectSearcher (@"root\microsoftiisv2", "SELECT * FROM IIsApplicationPool WHERE Name = '" + "W3SVC/APPPOOLS/" + targetAppPoolToRecycle + "'"); foreach (ManagementObject obj in searcher.Get()) { obj.InvokeMethod("Recycle", null); } </pre> <br /> WMI 말고 Directory Services를 이용하는 방법도 있습니다. 이런 경우 System.DirectoryServices.dll 어셈블리를 참조추가하고 다음과 같이 코딩하면 됩니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > private void RecycleAppPoolId(string targetAppPoolToRecycle) { string appPoolPath = "IIS://localhost/W3SVC/AppPools/" + targetAppPoolToRecycle; DirectoryEntry appPoolEntry = new DirectoryEntry(appPoolPath); appPoolEntry.Invoke("Recycle"); } </pre> <br /> 또는 예전에 소개했던,<br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > IIS Metabase 와 ServerManager 개체 활용 ; <a target='tab' href='http://www.sysnet.pe.kr/2/0/973'>http://www.sysnet.pe.kr/2/0/973</a> System.Runtime.InteropServices.COMException (0x80005000) ; <a target='tab' href='http://www.sysnet.pe.kr/2/0/1221'>http://www.sysnet.pe.kr/2/0/1221</a> </pre> <br /> "C:\Windows\System32\inetsrv\Microsoft.Web.Administration.dll" 어셈블리를 참조 추가한 후 ServerManager 타입을 사용해 recycle을 수행할 수 있습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > ServerManager svr = new ServerManager(); ApplicationPool appPool = svr.ApplicationPools[appPoolName]; if (appPool != null) { appPool.Recycle(); } </pre> <br /> <hr style='width: 50%' /><br /> <br /> Recycle 코드를 수행하는 데 한 가지 제약이 있다면 바로 보안 문제입니다. 일반 사용자 권한으로 실행하는 경우 다음과 같은 오류가 발생합니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > Unhandled Exception: System.Management.ManagementException: Provider load failure at System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStatus errorCode) at System.Management.ManagementObjectCollection.ManagementObjectEnumerator.MoveNext() at ConsoleApplication1.Program.RecycleIISWorkerProcess(String targetAppPoolToRecycle) in c:\...\WebApplication1\ConsoleApplication1\Program.cs:line 23 at ConsoleApplication1.Program.Main(String[] args) in c:\...\WebApplication1\ConsoleApplication1\Program.cs:line 14 </pre> <br /> <div style='BACKGROUND-COLOR: #ccffcc; padding: 10px 10px 5px 10px; MARGIN: 0px 10px 10px 10px; FONT-FAMILY: Malgun Gothic, Consolas, Verdana; COLOR: #005555'> Win32: Access is denied.<br /> <br /> Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. <br /> <br /> Exception Details: System.UnauthorizedAccessException: Win32: Access is denied.<br /> <br /> ASP.NET is not authorized to access the requested resource. Consider granting access rights to the resource to the ASP.NET request identity. ASP.NET has a base process identity (typically {MACHINE}\ASPNET on IIS 5 or Network Service on IIS 6 and IIS 7, and the configured application pool identity on IIS 7.5) that is used if the application is not impersonating. If the application is impersonating via <identity impersonate="true"/>, the identity will be the anonymous user (typically IUSR_MACHINENAME) or the authenticated request user. <br /> <br /> To grant ASP.NET access to a file, right-click the file in File Explorer, choose "Properties" and select the Security tab. Click "Add" to add the appropriate user or group. Highlight the ASP.NET account, and check the boxes for the desired access.<br /> </div><br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > Filename: redirection.config Error: Cannot read configuration file due to insufficient permissions </pre> <br /> 따라서 반드시 '관리자 권한'으로 실행시켜야 정상적으로 w3wp.exe가 재생됩니다. (ASP.NET의 경우는 SYSTEM 권한을 AppPool에 주어야 합니다.) 만약 일반 사용자 권한으로 recycle을 하고 싶다면 그 사용자에 권한을 주어야 하는데요. 이에 대해서는 다음의 글에서 설명해 주고 있으니 참고하세요. ^^<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > IIS 7 Delegate Remote Application Pool Recycling for Non Administrator ; <a target='tab' href='http://blogs.msdn.com/b/asiatech/archive/2011/07/21/iis-7-delegate-remote-application-pool-recycling-for-non-administrator.aspx'>http://blogs.msdn.com/b/asiatech/archive/2011/07/21/iis-7-delegate-remote-application-pool-recycling-for-non-administrator.aspx</a> </pre> <br /> 그나저나 아쉽군요. 원래 이 방법을 여러 가지로 탐구했을 때는 프로세스 스스로 재생하는 경우 보안이 필요하지 않는 방법이 없을까...를 찾아본 것이었습니다. 3가지 방법 모두 아쉽게도 무조건 관리자 권한을 요구한다는!<br /> </p><br /> <br /><hr /><span style='color: Maroon'>[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]</span> </div>
첨부파일
스팸 방지용 인증 번호
2923
(왼쪽의 숫자를 입력해야 합니다.)