C# ASMX 프로젝트에서 빌드 시 Web.Debug.config
와 Web.Release.config
가 자동으로 Web.config
에 적용되도록 설정하는 것은 Web.config Transformation 기능을 사용하는 것입니다. ASP.NET 웹 프로젝트(ASMX 포함)에서는 기본적으로 이 기능이 제공됩니다.
Web.config Transformation의 동작 방식
Web.config
변환은 **빌드 구성(Debug, Release 등)**과 게시 프로필에 따라 Web.config
파일을 변경하는 데 사용됩니다.
- 개발 환경 (Debug 모드에서 F5 실행): 기본적으로 Visual Studio에서 F5를 눌러 애플리케이션을 실행할 때는
Web.Debug.config
변환이 자동으로 적용되지 않습니다. 이는 개발자가Web.config
자체를 직접 편집하여 로컬 개발 환경에 맞는 설정을 사용하도록 하는 것이 일반적이기 때문입니다.Web.Debug.config
는 주로 디버그 목적으로 게시할 때 적용됩니다. - 빌드 및 게시 시:
Debug
또는Release
빌드 구성으로 프로젝트를 “게시(Publish)”할 때, 해당 빌드 구성에 맞는.config
파일(예:Web.Debug.config
또는Web.Release.config
)에 정의된 변환 규칙이 원본Web.config
에 적용됩니다. 결과적으로 변환된Web.config
파일이 배포 패키지 또는 게시된 폴더에 생성됩니다.
Web.Debug.config 및 Web.Release.config 사용 방법
이 파일들 내부에는 XML-Document-Transform 네임스페이스(xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"
)에 정의된 특수 속성(xdt:Transform
, xdt:Locator
)을 사용하여 Web.config
의 특정 부분을 변경하도록 지시합니다.
예시:
Web.config (기본 설정):
XML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?xml version="1.0" encoding="utf-8"?> <configuration> <appSettings> <add key="Environment" value="Development" /> </appSettings> <connectionStrings> <add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=MyDb_Dev;Integrated Security=True" providerName="System.Data.SqlClient" /> </connectionStrings> <system.web> <compilation debug="true" targetFramework="4.7.2" /> </system.web> </configuration> |
Web.Debug.config (디버그 빌드 시 변경될 내용):
XML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?xml version="1.0"?> <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"> <appSettings> <add key="Environment" value="DebugLocal" xdt:Transform="SetAttributes" xdt:Locator="Match(key)" /> </appSettings> <connectionStrings> <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=MyDb_LocalDebug;Integrated Security=True" xdt:Transform="SetAttributes" xdt:Locator="Match(name)" /> </connectionStrings> <system.web> <compilation xdt:Transform="RemoveAttributes(debug)" /> </system.web> </configuration> |
Web.Release.config (릴리즈 빌드 시 변경될 내용):
XML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?xml version="1.0"?> <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"> <appSettings> <add key="Environment" value="Production" xdt:Transform="SetAttributes" xdt:Locator="Match(key)" /> </appSettings> <connectionStrings> <add name="DefaultConnection" connectionString="Data Source=ProdServer;Initial Catalog=MyDb_Prod;Integrated Security=True" xdt:Transform="SetAttributes" xdt:Locator="Match(name)" /> </connectionStrings> <system.web> <compilation xdt:Transform="RemoveAttributes(debug)" /> </system.web> </configuration> |
주요 xdt:Transform
및 xdt:Locator
특성:
xdt:Transform="SetAttributes"
: 해당 요소의 속성을 변경합니다.xdt:Transform="Insert"
: 요소를 삽입합니다.xdt:Transform="Remove"
: 요소를 제거합니다.xdt:Transform="Replace"
: 요소를 완전히 교체합니다.xdt:Locator="Match(key)"
또는xdt:Locator="Match(name)"
:key
또는name
속성 값이 일치하는 요소를 찾습니다.xdt:Locator="XPath(/configuration/system.web/compilation)"
: XPath를 사용하여 특정 요소를 찾습니다.
빌드 시 자동 선택 (Web.config Transformation의 기본 동작)
Visual Studio에서 Debug
또는 Release
구성을 선택하고 프로젝트를 **게시(Publish)**할 때, MSBuild는 자동으로 해당 구성에 맞는 Web.<Configuration>.config
파일을 찾아 Web.config
에 변환을 적용합니다.
주의사항:
- 디버깅 시
Web.config
변환: 위에서 언급했듯이, Visual Studio에서 F5를 눌러 디버깅할 때는Web.config
변환이 자동으로 적용되지 않습니다. 이는 개발 편의를 위한 것이며,Web.config
파일 자체가 로컬 개발 환경 설정을 포함하도록 하는 것이 일반적입니다. 변환은 주로 게시(배포)를 위해 사용됩니다. - 변환 미리 보기: Visual Studio에서는
Web.Debug.config
또는Web.Release.config
파일을 마우스 오른쪽 버튼으로 클릭하고 “Preview Transform” (변환 미리 보기)을 선택하여 변환 후의Web.config
파일이 어떻게 변경될지 미리 확인할 수 있습니다. - 소스 제어:
Web.config
와Web.Debug.config
,Web.Release.config
파일은 모두 소스 제어(Git 등)에 포함되어야 합니다.Web.config
가 빌드 또는 게시 시에 변환되어 생성되는 파일이므로, 실제로 배포될Web.config
는 소스 제어에서 제외하는 것이 좋습니다.
이러한 Web.config Transformation은 C# ASMX 프로젝트뿐만 아니라 ASP.NET Web Forms, MVC 등 모든 ASP.NET 웹 프로젝트에서 동일하게 적용됩니다.