使用标签为不同的环境启用配置Use labels to enable configurations for different environments

许多应用程序需要针对不同的环境使用不同的配置。Many applications need to use different configurations for different environments. 假设应用程序有一个配置值,该配置值定义了用于其后端数据库的连接字符串。Suppose that an application has a configuration value that defines the connection string to use for its back-end database. 应用程序开发者使用的数据库与生产环境中使用的数据库不同。The application developers use a different database from the one used in production. 当应用程序从开发环境转移到生产环境时,应用程序使用的数据库连接字符串必须进行更改。The database connection string that the application uses must change as the application moves from development to production.

在 Azure 应用配置中,可以使用“标签”为同一个键定义不同的值。In Azure App Configuration, you can use labels to define different values for the same key. 例如,可以定义单个键,对开发和生产环境使用不同的值。For example, you can define a single key with different values for development and production. 可以指定在连接到应用配置时要加载哪个标签。You can specify which label to load when connecting to App Configuration.

为了演示此功能,需修改在快速入门:使用 Azure 应用配置创建 ASP.NET Core 应用中创建的 Web 应用,对开发环境与生产环境使用不同的配置设置。To demonstrate this functionality, you'll modify the web app created in Quickstart: Create an ASP.NET Core app with Azure App Configuration to use different configuration settings for development versus production. 在继续操作之前,请先完成该快速入门。Complete the quickstart before proceeding.

在添加配置值时指定标签Specify a label when adding a configuration value

在 Azure 门户中,转到“配置资源管理器”并找到你在该快速入门中创建的 TestApp:Settings:FontColor 键。In the Azure portal, go to Configuration Explorer and find the TestApp:Settings:FontColor key that you created in the quickstart. 选择其上下文菜单,然后选择“添加值”。Select its context menu and then select Add Value.

“添加值”菜单项Add Value menu item

在“添加值”屏幕上,输入“红色”作为值,输入“开发”作为标签。 On the Add Value screen, enter a Value of red and a Label of Development. 将“内容类型”保留为空。Leave Content type empty. 选择“应用”。Select Apply.

加载具有指定标签的配置值Load configuration values with a specified label

默认情况下,Azure 应用配置仅加载没有标签的配置值。By default, Azure App Configuration only loads configuration values with no label. 如果你为配置值定义了标签,则需要指定在连接到应用配置时要使用的标签。If you've defined labels for your configuration values, you'll want to specify the labels to use when connecting to App Configuration.

在上一部分中,你为开发环境创建了一个不同的配置值。In the previous section, you created a different configuration value for the development environment. 使用 HostingEnvironment.EnvironmentName 变量来动态确定应用当前在哪个环境中运行。You use the HostingEnvironment.EnvironmentName variable to dynamically determine which environment the app currently runs in. 若要了解详细信息,请参阅在 ASP.NET Core 中使用多个环境To learn more, see Use multiple environments in ASP.NET Core.

通过将环境名称传递给 Select 方法,加载其标签与当前环境对应的配置值:Load configuration values with the label corresponding to the current environment by passing the environment name into the Select method:

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            webBuilder.ConfigureAppConfiguration((hostingContext, config) =>
            {
                var settings = config.Build();
                config.AddAzureAppConfiguration(options =>
                    options
                        .Connect(Environment.GetEnvironmentVariable("AppConfigConnectionString"))
                        // Load configuration values with no label
                        .Select(KeyFilter.Any, LabelFilter.Null)
                        // Override with any configuration values specific to current hosting env
                        .Select(KeyFilter.Any, hostingContext.HostingEnvironment.EnvironmentName)
                );
            })
            .UseStartup<Startup>());

重要

前面的代码片段从名为 AppConfigConnectionString 的环境变量加载应用配置连接字符串。The preceding code snippet loads the App Configuration connection string from an environment variable named AppConfigConnectionString. 请确保正确设置了此环境变量。Be sure that this environment variable is set properly.

Select 方法将调用两次。The Select method is called twice. 第一次,它加载没有标签的配置值。The first time, it loads configuration values with no label. 然后,它加载其标签与当前环境对应的配置值。Then, it loads configuration values with the label corresponding to the current environment. 这些特定于环境的值会替代没有标签的任何对应值。These environment-specific values override any corresponding values with no label. 不需要为每个键都定义特定于环境的值。You don't need to define environment-specific values for every key. 如果某个键没有值具有与当前环境对应的标签,它将使用没有标签的值。If a key doesn't have a value with a label corresponding to the current environment, it uses the value with no label.

在不同的环境中测试Test in different environments

打开 Properties 目录下的 launchSettings.json 文件。Open the launchSettings.json file under the Properties directory. profiles 下找到 config 条目。Find the config entry under profiles. environmentVariables 部分中,将 ASPNETCORE_ENVIRONMENT 变量设置为 ProductionIn the environmentVariables section, set the ASPNETCORE_ENVIRONMENT variable to Production.

设置新值后,生成并运行你的应用程序。With the new values set, build and run your application.

dotnet build
dotnet run

在 Web 浏览器中转到 http://localhost:5000Use a web browser to go to http://localhost:5000. 你会注意到,字体颜色为黑色。You'll notice that the font color is black.

以生产配置运行的 Web 应用程序

更新 launchSettings.json,将 ASPNETCORE_ENVIRONMENT 变量设置为 DevelopmentUpdate launchSettings.json to set the ASPNETCORE_ENVIRONMENT variable to Development. 再次运行 dotnet runRun dotnet run again.

你会注意到,字体颜色现在为红色。You'll notice that the font color is now red. 这是因为应用程序现在使用具有 Development 标签的 TestApp:Settings:FontColor 的值。This is because the application now uses the value of TestApp:Settings:FontColor that has the Development label. 所有其他配置值保持与其生产值相同。All other configuration values remain the same as their production values.

以开发配置运行的 Web 应用程序

后续步骤Next steps