Share via


Service Fabric에서 구성 파일을 매개 변수화하는 방법

이 문서에서는 Service Fabric에서 구성 파일을 매개 변수화하는 방법을 보여 줍니다. 여러 환경에 대한 애플리케이션 관리의 핵심 개념에 익숙하지 않은 경우 여러 환경에 대한 애플리케이션 관리를 읽어보세요.

구성 파일 매개 변수화 절차

이 예제에서는 애플리케이션 배포에서 매개 변수를 사용하여 구성 값을 재정의합니다.

  1. 서비스 프로젝트에서 <MyService>\PackageRoot\Config\Settings.xml 파일을 엽니다.

  2. 다음 XML을 추가하여 구성 매개 변수 이름 및 값(예: 캐시 크기가 25임)을 설정하세요.

     <Section Name="MyConfigSection">
       <Parameter Name="CacheSize" Value="25" />
     </Section>
    
  3. 파일을 저장 후 닫습니다.

  4. <MyApplication>\ApplicationPackageRoot\ApplicationManifest.xml 파일을 엽니다.

  5. ApplicationManifest.xml 파일에서 Parameters 요소에 매개 변수와 기본값을 선언하세요. 매개 변수 이름에 서비스 이름(예: “내 서비스”)이 포함되어 있는 것이 좋습니다.

     <Parameters>
       <Parameter Name="MyService_CacheSize" DefaultValue="80" />
     </Parameters>
    
  6. ApplicationManifest.xml 파일의 ServiceManifestImport 섹션에서 구성 패키지, 섹션 및 매개 변수를 참조하는 ConfigOverridesConfigOverride 요소를 추가하세요.

     <ConfigOverrides>
       <ConfigOverride Name="Config">
           <Settings>
             <Section Name="MyConfigSection">
                 <Parameter Name="CacheSize" Value="[MyService_CacheSize]" />
             </Section>
           </Settings>
       </ConfigOverride>
     </ConfigOverrides>
    

참고 항목

ConfigOverride를 추가하는 경우, Service Fabric은 항상 애플리케이션 매니페스트에 지정된 애플리케이션 매개 변수 또는 기본값을 선택합니다.

코드에서 매개 변수화된 구성에 액세스

프로그래밍 방식으로 settings.xml 파일의 구성에 액세스할 수 있습니다. 예를 들어 다음 구성 XML 파일을 살펴보겠습니다.

<Settings
	xmlns:xsd="http://www.w3.org/2001/XMLSchema"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://schemas.microsoft.com/2011/01/fabric">
	<!-- Add your custom configuration sections and parameters here -->
	<Section Name="MyConfigSection">
		<Parameter Name="MyParameter" Value="Value1" />
	</Section>
</Settings>     

다음 코드를 사용하여 매개 변수에 액세스합니다.

CodePackageActivationContext context = FabricRuntime.GetActivationContext();
var configSettings = context.GetConfigurationPackageObject("Config").Settings;
var data = configSettings.Sections["MyConfigSection"];
foreach (var parameter in data.Parameters)
{
  ServiceEventSource.Current.ServiceMessage(this.Context, "Working-{0} - {1}", parameter.Name, parameter.Value);
}

여기서 Parameter.Name은 MyParameter이고 Parameter.Value는 Value1입니다.

다음 단계

Visual Studio에서 사용할 수 있는 다른 앱 관리 기능에 대한 정보는 Visual Studio에서 Service Fabric 애플리케이션 관리를 참조하세요.