aspnet.config 파일의 설정을 읽는 방법
aspnet.config 파일의 기본 설정 파일 내용은 다음과 같이 <runtime />, <startup /> 설정만 갖는 것이 일반적입니다.
<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
<runtime>
<legacyUnhandledExceptionPolicy enabled="false" />
<legacyImpersonationPolicy enabled="true"/>
<alwaysFlowImpersonationPolicy enabled="false"/>
<SymbolReadingPolicy enabled="1" />
<shadowCopyVerifyByTimestamp enabled="true"/>
</runtime>
<startup useLegacyV2RuntimeActivationPolicy="true" />
</configuration>
그런데 이 값들을 읽어내기 위해 일반적인 web.config 파일을 다루는 방법은 애석하게도 aspnet.config에는 통하지 않습니다. 가령, web.config은 machine.config으로부터 상속되어 내려오는 설정을 다음의 방법으로 읽는 것이 가능하지만,
How to: Access ASP.NET Configuration Settings Programmatically
; https://docs.microsoft.com/en-us/previous-versions/4c2kcht0(v=vs.140)
특이하게도 runtime 섹션에 대해서는 아래의 글에 설명된 것처럼,
ConfigurationManager - reading the “runtime” section
; https://toresenneseth.wordpress.com/2009/04/23/configurationmanager-reading-the-runtime-section/
GetSection 메서드를 이용하면
IgnoreSection 타입의 인스턴스가 반환됩니다.
System.Configuration.Configuration rootWebConfig1 =
System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(null);
object objSection = rootWebConfig1.GetSection("runtime");
// objSection은 System.Configuration.IgnoreSection 타입의 인스턴스
그래서 아무런 값도 알아낼 수 없습니다. 설령 읽어냈다 해도 그것이 machine.config/web.config 내의 설정일 듯 싶고 aspnet.config의 내용이라는 보장이 없습니다.
어쩔 수 없군요. ^^; 이럴 땐 XmlDocument 타입을 이용해 직접 다음과 같이 읽어오는 것이 현재로써는 최선인 듯 합니다.
string path = System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory();
string aspnetPath = System.IO.Path.Combine(path, "aspnet.config");
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(aspnetPath);
bool canAbortThread = false;
XmlNode policyNode = xmlDoc.SelectSingleNode("/configuration/runtime/legacyUnhandledExceptionPolicy");
if (policyNode != null)
{
string enabled = policyNode.Attributes.GetNamedItem("enabled").Value;
canAbortThread = bool.Parse(enabled);
}
[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]