Microsoft MVP성태의 닷넷 이야기
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일

IIS Express - 웹 응용 프로그램의 .NET 버전에 맞는 CLR이 로드되지 않는 경우

개발하다 보면, 프로젝트를 .NET 3.5와 .NET 4.0사이를 왔다 갔다 할 때가 있습니다.

우선, .NET 4.0 웹 프로젝트를 Visual Studio에서 F5 디버그하는 경우, 실행되는 IIS Express의 명령행 라인을 Process Explorer를 통해 확인하면 대충 이렇습니다.

"C:\...\iisexpress.exe" /config:"c:\...\applicationhost.config" /site:"WebTest" /apppool:"Clr4IntegratedAppPool"

그리고, 해당 프로젝트의 대상 프레임워크를 .NET 3.5로 내리는 경우 iisexpress는 다음의 명령행 라인으로 실행됩니다.

"C:\...\iisexpress.exe" /config:"c:\...\applicationhost.config" /site:"WebTest" /apppool:"Clr2IntegratedAppPool"

사실, 달라지는 것은 명령행 뿐만이 아닙니다. "%USERPROFILE%\My Documents\IISExpress\config\applicationhost.config" 파일을 열어 보면 웹 사이트에 대한 정보가 /configuration/system.applicationHost/sites/site 경로에 나오는데,

<site name="WebTest" id="5">
    <application path="/" applicationPool="Clr4IntegratedAppPool">
        <virtualDirectory path="/" physicalPath="D:\test\WebTest" />
    </application>
    <bindings>
        <binding protocol="http" bindingInformation="*:19448:localhost" />
    </bindings>
</site>

보시다시피 이곳의 applicationPool 속성값도 함께 바뀌게 됩니다. 즉, Visual Studio는 웹 프로젝트의 닷넷 버전을 바꾸는 경우 프로젝트 파일(C#인 경우 csproj)뿐만 아니라 applicationhost.config 파일의 내용까지 함께 바꿔주는 것입니다.




정상적인 경우 그렇다는 것이고, 비정상적인 경우 .NET 4.0 웹 애플리케이션의 프로젝트를 .NET 3.5로 내렸는데도 여전히 IISExpress를 CLR4 설정으로 로드하는 상황이 발생했습니다.

"C:\...\iisexpress.exe" /config:"c:\...\applicationhost.config" /site:"WebTest" /apppool:"Clr4IntegratedAppPool"

실제로 applicationhost.config 파일의 내용을 확인해도 Clr4IntegratedAppPool 임을 확인할 수 있었는데요. applicationhost.config 파일을 좀 더 살펴 보니, 동일한 physicalPath로 2개의 웹 사이트 설정이 포함된 경우였습니다.

<site name="WebTest" id="5">
    <application path="/" applicationPool="Clr4IntegratedAppPool">
        <virtualDirectory path="/" physicalPath="D:\test\WebTest" />
    </application>
    <bindings>
        <binding protocol="http" bindingInformation="*:19448:localhost" />
    </bindings>
</site>

<site name="WebTest(1)" id="6">
    <application path="/" applicationPool="Clr2IntegratedAppPool">
        <virtualDirectory path="/" physicalPath="D:\test\WebTest" />
    </application>
    <bindings>
        <binding protocol="http" bindingInformation="*:9000:localhost" />
    </bindings>
</site>

결국 하단의 "WebTest(1)"에 해당하는 설정을 지워주고 나서야 Visual Studio가 정상적으로 CLR 선택을 할 수 있었습니다.




Visual Studio로 웹 개발 및 테스트를 많이 하다 보면 어쩔 수 없이 applicationhost.config 파일의 site 목록이 많아질 수밖에 없습니다. 그러다 결국 physicalPath가 우연히 겹치는 사태가 발생하고 저런 식의 문제가 발생했을 때 발견이 힘들어지는데요.

그래서, ^^ 임시로 생성한 웹 프로젝트의 경우 physicalPath에 기록된 경로가 이제는 없는 경우가 많을 것이므로 이런 site 설정만 골라서 삭제하는 프로그램을 만들어 봤습니다.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string myDocumentPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            string iisSitesHomePath = Path.Combine(myDocumentPath, "My Web Sites");
            Environment.SetEnvironmentVariable("IIS_SITES_HOME", iisSitesHomePath);

            string file = Path.Combine(myDocumentPath, @"IISExpress\config\applicationhost.config");
            XmlDocument xml = new XmlDocument();
            xml.PreserveWhitespace = true;
            xml.Load(file);

            Dictionary<string, string> toRemove = new Dictionary<string, string>();

            foreach (XmlNode site in xml.SelectNodes("/configuration/system.applicationHost/sites/site"))
            {
                XmlNode virtNode = site.SelectSingleNode("./application/virtualDirectory");
                if (virtNode == null)
                {
                    continue;
                }

                XmlNode pathNode = virtNode.Attributes.GetNamedItem("physicalPath");
                if (pathNode == null)
                {
                    continue;
                }

                string path = Environment.ExpandEnvironmentVariables(pathNode.Value);
                if (Directory.Exists(path) == false)
                {
                    string siteId = site.Attributes.GetNamedItem("id").Value;
                    toRemove.Add(siteId, path);
                }
            }

            XmlNode sitesNode = xml.SelectSingleNode("/configuration/system.applicationHost/sites");
            foreach (var item in toRemove)
            {
                string xPath = string.Format("/configuration/system.applicationHost/sites/site[@id = '{0}']", item.Key);
                XmlNode siteNode = xml.SelectSingleNode(xPath);
                sitesNode.RemoveChild(siteNode);
            }

            string dateTime = DateTime.Now.ToString("yyyyMMddHHmmss");
            string oldPath = string.Format("{0}.{1}.bak", file, dateTime);

            File.Copy(file, oldPath);
            xml.Save(file);
            Console.WriteLine("[Backup] Old Config: " + oldPath);
            Console.WriteLine("Overwritten: " + file);
        }
    }
}

물론, 첨부된 프로젝트는 위의 코드를 포함하고 있습니다.




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







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

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)
13712정성태8/14/20242117개발 환경 구성: 720. Synology NAS - docker 원격 제어를 위한 TCP 바인딩 추가
13711정성태8/13/20242377Linux: 77. C# / Linux - zombie process (defunct process)파일 다운로드1
13710정성태8/8/20242645닷넷: 2294. C# 13 - (6) iterator 또는 비동기 메서드에서 ref와 unsafe 사용을 부분적으로 허용파일 다운로드1
13709정성태8/7/20242473닷넷: 2293. C# - safe/unsafe 문맥에 대한 C# 13의 (하위 호환을 깨는) 변화파일 다운로드1
13708정성태8/7/20242224개발 환경 구성: 719. ffmpeg / YoutubeExplode - mp4 동영상 파일로부터 Audio 파일 추출
13707정성태8/6/20242499닷넷: 2292. C# - 자식 프로세스의 출력이 4,096보다 많은 경우 Process.WaitForExit 호출 시 hang 현상파일 다운로드1
13706정성태8/5/20242742개발 환경 구성: 718. Hyper-V - 리눅스 VM에 새로운 디스크 추가
13705정성태8/4/20242809닷넷: 2291. C# 13 - (5) params 인자 타입으로 컬렉션 허용파일 다운로드1
13704정성태8/2/20242844닷넷: 2290. C# - 간이 dotnet-dump 프로그램 만들기파일 다운로드1
13703정성태8/1/20242998닷넷: 2289. "dotnet-dump ps" 명령어가 닷넷 프로세스를 찾는 방법
13702정성태7/31/20242850닷넷: 2288. Collection 식을 지원하는 사용자 정의 타입을 CollectionBuilder 특성으로 성능 보완파일 다운로드1
13701정성태7/30/20242681닷넷: 2287. C# 13 - (4) Indexer를 이용한 개체 초기화 구문에서 System.Index 연산자 허용파일 다운로드1
13700정성태7/29/20242526디버깅 기술: 200. DLL Export/Import의 Hint 의미
13699정성태7/27/20242657닷넷: 2286. C# 13 - (3) Monitor를 대체할 Lock 타입파일 다운로드1
13698정성태7/27/20242636닷넷: 2285. C# - async 메서드에서의 System.Threading.Lock 잠금 처리파일 다운로드1
13697정성태7/26/20242721닷넷: 2284. C# - async 메서드에서의 lock/Monitor.Enter/Exit 잠금 처리파일 다운로드1
13696정성태7/26/20242629오류 유형: 920. dotnet publish - error NETSDK1047: Assets file '...\obj\project.assets.json' doesn't have a target for '...'
13695정성태7/25/20242316닷넷: 2283. C# - Lock / Wait 상태에서도 STA COM 메서드 호출 처리파일 다운로드1
13694정성태7/25/20242626닷넷: 2282. C# - ASP.NET Core Web App의 Request 용량 상한값 (Kestrel, IIS)
13693정성태7/24/20242342개발 환경 구성: 717. Visual Studio - C# 프로젝트에서 레지스트리에 등록하지 않은 COM 개체 참조 및 사용 방법파일 다운로드1
13692정성태7/24/20242962디버깅 기술: 199. Windbg - 리눅스에서 뜬 닷넷 응용 프로그램 덤프 파일에 포함된 DLL의 Export Directory 탐색
13691정성태7/23/20242564디버깅 기술: 198. Windbg - 스레드의 Win32 Message Queue 정보 조회
13690정성태7/23/20242394오류 유형: 919. Visual C++ 리눅스 프로젝트 - error : ‘u8’ was not declared in this scope
13689정성태7/22/20242910디버깅 기술: 197. Windbg - PE 포맷의 Export Directory 탐색
13688정성태7/21/20242666닷넷: 2281. C# - Lock / Wait 상태에서도 일부 Win32 메시지 처리파일 다운로드1
13687정성태7/19/20242696닷넷: 2280. C# - PostThreadMessage로 보낸 메시지를 Windows Forms에서 수신하는 방법파일 다운로드1
1  [2]  3  4  5  6  7  8  9  10  11  12  13  14  15  ...