如何在 Service Fabric 中参数化配置文件How to parameterize configuration files in Service Fabric

本文演示如何在 Service Fabric 中参数化配置文件。This article shows you how to parameterize a configuration file in Service Fabric. 如果还不熟悉管理多个环境的应用程序的核心概念,请阅读管理多个环境的应用程序If you're not already familiar with the core concepts of managing applications for multiple environments, read Manage applications for multiple environments.

参数化配置文件的过程Procedure for parameterizing configuration files

在此示例中,在应用程序部署中使用参数来替代配置值。In this example, you override a configuration value using parameters in your application deployment.

  1. 打开你的服务项目中的 <MyService>\PackageRoot\Config\Settings.xml 文件。Open the <MyService>\PackageRoot\Config\Settings.xml file in your service project.

  2. 通过添加以下 XML,设置配置参数名称和值,例如高速缓存大小等于 25:Set a configuration parameter name and value, for example cache size equal to 25, by adding the following XML:

    <Section Name="MyConfigSection">
        <Parameter Name="CacheSize" Value="25" />
    </Section>
    
  3. 保存并关闭该文件。Save and close the file.

  4. 打开 <MyApplication>\ApplicationPackageRoot\ApplicationManifest.xml 文件。Open the <MyApplication>\ApplicationPackageRoot\ApplicationManifest.xml file.

  5. 在 ApplicationManifest.xml 文件的 Parameters 元素中声明参数和默认值。In the ApplicationManifest.xml file, declare a parameter and default value in the Parameters element. 建议参数名称包含服务的名称(例如,“MyService”)。It's recommended that the parameter name contains the name of the service (for example, "MyService").

    <Parameters>
      <Parameter Name="MyService_CacheSize" DefaultValue="80" />
    </Parameters>
    
  6. 在 ApplicationManifest.xml 文件的 ServiceManifestImport 节中,添加 ConfigOverridesConfigOverride 元素,引用配置包、节和参数。In the ServiceManifestImport section of the ApplicationManifest.xml file, add a ConfigOverrides and ConfigOverride element, referencing the configuration package, the section, and the parameter.

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

    备注

    在添加 ConfigOverride 的情况下,Service Fabric 将始终选择应用程序参数或应用程序清单中指定的默认值。In the case where you add a ConfigOverride, Service Fabric always chooses the application parameters or the default value specified in the application manifest.

后续步骤Next steps

有关 Visual Studio 中其他可用应用管理功能的信息,请参阅在 Visual Studio 中管理 Service Fabric 应用程序For information about other app management capabilities that are available in Visual Studio, see Manage your Service Fabric applications in Visual Studio.