Skip to content

Commit 08cbf6b

Browse files
authored
Merge pull request #107571 from lisaguthrie/5344170-labels
Labels howto
2 parents 470ccce + 81f86e3 commit 08cbf6b

File tree

7 files changed

+91
-0
lines changed

7 files changed

+91
-0
lines changed

articles/azure-app-configuration/TOC.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@
9292
items:
9393
- name: Enable a feature for a subset of users
9494
href: howto-feature-filters-aspnet-core.md
95+
- name: Use labels for per-environment configuration
96+
href: howto-labels-aspnet-core.md
9597
- name: Import or export configuration data
9698
href: howto-import-export-data.md
9799
- name: Route events to a custom endpoint

articles/azure-app-configuration/faq.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ There's a limit of 10 KB for a single key-value item.
5555

5656
You control who can access App Configuration at a per-store level. Use a separate store for each environment that requires different permissions. This approach provides the best security isolation.
5757

58+
If you do not need security isolation between environments, you can use labels to differentiate between configuration values. [Use labels to enable different configurations for different environments](./howto-labels-aspnet-core.md) provides a complete example.
59+
5860
## What are the recommended ways to use App Configuration?
5961

6062
See [best practices](./howto-best-practices.md).

articles/azure-app-configuration/howto-best-practices.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ configBuilder.AddAzureAppConfiguration(options => {
5252
});
5353
```
5454

55+
[Use labels to enable different configurations for different environments](./howto-labels-aspnet-core.md) provides a complete example.
56+
5557
## App Configuration bootstrap
5658

5759
To access an App Configuration store, you can use its connection string, which is available in the Azure portal. Because connection strings contain credential information, they're considered secrets. These secrets need to be stored in Azure Key Vault, and your code must authenticate to Key Vault to retrieve them.
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
---
2+
title: Use per-environment configuration
3+
titleSuffix: Azure App Configuration
4+
description: Use labels to provide per-environment configuration values
5+
ms.service: azure-app-configuration
6+
author: lisaguthrie
7+
8+
ms.service: azure-app-configuration
9+
ms.topic: conceptual
10+
ms.date: 3/12/2020
11+
ms.author: lcozzens
12+
13+
---
14+
# Use labels to enable different configurations for different environments
15+
16+
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's developers use a different database from the one used in production. The database connection string used by the application must change as the application moves from development to production.
17+
18+
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(s) to load when connecting to App Configuration.
19+
20+
To demonstrate this functionality, we'll modify the web app created in [Quickstart: Create an ASP.NET Core app with Azure App Configuration](./quickstart-aspnet-core-app.md) to use different configuration settings for development vs. production. Please complete the quickstart before proceeding.
21+
22+
## Specify a label when adding a configuration value
23+
24+
In the Azure portal, go into **Configuration Explorer** and locate the *TestApp:Settings:FontColor* key that you created in the quickstart. Select its context menu and then click **Add Value**.
25+
26+
> [!div class="mx-imgBorder"]
27+
> ![Add Value menu item](media/labels-add-value.png)
28+
29+
On the **Add Value** screen, enter a **Value** of **red** and a **Label** of **Development**. Leave **Content type** empty. Select **Apply**.
30+
31+
## Loading configuration values with a specified label
32+
33+
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 label(s) to use when connecting to App Configuration.
34+
35+
In the last section, you created a different configuration value for the *Development* environment. You use the `HostingEnvironment.EnvironmentName` variable to dynamically determine which environment the app is currently running in. To learn more, see [Use multiple environments in ASP.NET Core](/aspnet/core/fundamentals/environments).
36+
37+
Load configuration values with the label corresponding to the current environment by passing the environment name into the `Select` method:
38+
39+
```csharp
40+
public static IHostBuilder CreateHostBuilder(string[] args) =>
41+
Host.CreateDefaultBuilder(args)
42+
.ConfigureWebHostDefaults(webBuilder =>
43+
webBuilder.ConfigureAppConfiguration((hostingContext, config) =>
44+
{
45+
var settings = config.Build();
46+
config.AddAzureAppConfiguration(options =>
47+
options
48+
.Connect(Environment.GetEnvironmentVariable("AppConfigConnectionString"))
49+
// Load configuration values with no label
50+
.Select(KeyFilter.Any, LabelFilter.Null)
51+
// Override with any configuration values specific to current hosting env
52+
.Select(KeyFilter.Any, hostingContext.HostingEnvironment.EnvironmentName)
53+
);
54+
})
55+
.UseStartup<Startup>());
56+
```
57+
58+
> [!IMPORTANT]
59+
> The above code snippet loads the App Configuration connection string from an environment variable called `AppConfigConnectionString`. Be sure that this environment variable is set properly.
60+
61+
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 do not need to define environment-specific values for every key. If a key does not have a value with a label corresponding to the current environment, then the value with no label is used.
62+
63+
## Testing in different environments
64+
65+
To test the different configuration values, open the `launchSettings.json` file under the `Properties` directory. Locate the `config` entry under `profiles`. In the `environmentVariables` section, set the `ASPNETCORE_ENVIRONMENT` variable to `Production`.
66+
67+
With the new values set, build and run your application.
68+
69+
```dotnetcli
70+
dotnet build
71+
dotnet run
72+
```
73+
74+
Use a web browser to navigate to `http://localhost:5000`. You'll notice that the font color is black.
75+
76+
![Web application running with production configuration](media/labels-website-prod.png)
77+
78+
Now update `launchSettings.json` to set the `ASPNETCORE_ENVIRONMENT` variable to `Development`. Run `dotnet run` again. You'll notice that the font color is now red. 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.
79+
80+
![Web application running with development configuration](media/labels-website-dev.png)
81+
82+
## Next steps
83+
84+
> [!div class="nextstepaction"]
85+
> [Configuration in ASP.NET Core](/aspnet/core/fundamentals/configuration/)
101 KB
Loading
34.6 KB
Loading
36.4 KB
Loading

0 commit comments

Comments
 (0)