如何在 Service Fabric 中指定服务的环境变量How to specify environment variables for services in Service Fabric

本文演示如何在 Service Fabric 中指定服务或容器的环境变量。This article shows you how to specify environment variables for a service or container in Service Fabric.

指定服务的环境变量的过程Procedure for specifying environment variables for services

在此示例中,可以为容器设置环境变量。In this example, you set an environment variable for a container. 本文假设你已有一个应用程序和服务清单。The article assumes you already have an application and service manifest.

  1. 打开 ServiceManifest.xml 文件。Open the ServiceManifest.xml file.

  2. CodePackage 元素中,为每个环境变量添加新的 EnvironmentVariables 元素和 EnvironmentVariable 元素。In the CodePackage element, add a new EnvironmentVariables element and an EnvironmentVariable element for each environment variable.

      <CodePackage Name="MyCode" Version="CodeVersion1">
      ...
        <EnvironmentVariables>
          <EnvironmentVariable Name="MyEnvVariable" Value="DefaultValue"/>
          <EnvironmentVariable Name="HttpGatewayPort" Value="19080"/>
        </EnvironmentVariables>
      </CodePackage>
    

    可在应用程序清单中重写环境变量。Environment variables can be overridden in the application manifest.

  3. 若要替代应用程序清单中的环境变量,请使用 EnvironmentOverrides 元素。To override the environment variables in the application manifest, use the EnvironmentOverrides element.

      <ServiceManifestImport>
        <ServiceManifestRef ServiceManifestName="FrontEndServicePkg" ServiceManifestVersion="1.0.0" />
        <EnvironmentOverrides CodePackageRef="MyCode">
          <EnvironmentVariable Name="MyEnvVariable" Value="OverrideValue"/>
        </EnvironmentOverrides>
      </ServiceManifestImport>
    

后续步骤Next steps

若要详细了解本文中讨论的一些核心概念,请参阅文章管理多个环境的应用程序To learn more about some of the core concepts that are discussed in this article, see the Manage applications for multiple environments articles.

有关 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.