Skip to content

Commit 4d0a75f

Browse files
committed
edit pass: howto-labels-aspnet-core
1 parent d325b53 commit 4d0a75f

File tree

1 file changed

+48
-44
lines changed

1 file changed

+48
-44
lines changed

articles/azure-app-configuration/howto-labels-aspnet-core.md

Lines changed: 48 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -9,73 +9,77 @@ ms.date: 3/12/2020
99
ms.author: lcozzens
1010

1111
---
12-
# Use labels to enable different configurations for different environments
12+
# Use labels to enable configurations for different environments
1313

14-
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.
14+
Many applications need to use different configurations for different environments. For example, 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 used by the application must change as the application moves from development to production.
1515

16-
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.
16+
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.
1717

18-
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.
18+
To demonstrate this functionality, you'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 versus production. Complete the Quickstart before proceeding.
1919

2020
## Specify a label when adding a configuration value
2121

22-
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**.
22+
1. In the Azure portal, go to **Configuration Explorer** and locate the *TestApp:Settings:FontColor* key that you created in the Quickstart. Select its context menu and then click **Add Value**.
2323

24-
> [!div class="mx-imgBorder"]
25-
> ![Add Value menu item](media/labels-add-value.png)
24+
> [!div class="mx-imgBorder"]
25+
> ![Add Value menu item](media/labels-add-value.png)
2626
27-
On the **Add Value** screen, enter a **Value** of **red** and a **Label** of **Development**. Leave **Content type** empty. Select **Apply**.
27+
1. On the **Add Value** screen, enter a **Value** of **red** and a **Label** of **Development**. Leave **Content type** empty. Select **Apply**.
2828

29-
## Loading configuration values with a specified label
29+
## Load configuration values with a specified label
3030

31-
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.
31+
By default, 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.
3232

33-
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).
33+
In the previous section, you created a different configuration value for the *Development* environment. You use the `HostingEnvironment.EnvironmentName` variable to dynamically determine which environment the app currently runs in. To learn more, see [Use multiple environments in ASP.NET Core](/aspnet/core/fundamentals/environments).
3434

35-
Load configuration values with the label corresponding to the current environment by passing the environment name into the `Select` method:
35+
- Load configuration values with the label corresponding to the current environment by passing the environment name into the `Select` method:
3636

37-
```csharp
38-
public static IHostBuilder CreateHostBuilder(string[] args) =>
39-
Host.CreateDefaultBuilder(args)
40-
.ConfigureWebHostDefaults(webBuilder =>
41-
webBuilder.ConfigureAppConfiguration((hostingContext, config) =>
42-
{
43-
var settings = config.Build();
44-
config.AddAzureAppConfiguration(options =>
45-
options
46-
.Connect(Environment.GetEnvironmentVariable("AppConfigConnectionString"))
47-
// Load configuration values with no label
48-
.Select(KeyFilter.Any, LabelFilter.Null)
49-
// Override with any configuration values specific to current hosting env
50-
.Select(KeyFilter.Any, hostingContext.HostingEnvironment.EnvironmentName)
51-
);
52-
})
53-
.UseStartup<Startup>());
54-
```
37+
```csharp
38+
public static IHostBuilder CreateHostBuilder(string[] args) =>
39+
Host.CreateDefaultBuilder(args)
40+
.ConfigureWebHostDefaults(webBuilder =>
41+
webBuilder.ConfigureAppConfiguration((hostingContext, config) =>
42+
{
43+
var settings = config.Build();
44+
config.AddAzureAppConfiguration(options =>
45+
options
46+
.Connect(Environment.GetEnvironmentVariable("AppConfigConnectionString"))
47+
// Load configuration values with no label
48+
.Select(KeyFilter.Any, LabelFilter.Null)
49+
// Override with any configuration values specific to current hosting env
50+
.Select(KeyFilter.Any, hostingContext.HostingEnvironment.EnvironmentName)
51+
);
52+
})
53+
.UseStartup<Startup>());
54+
```
5555

56-
> [!IMPORTANT]
57-
> 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.
56+
> [!IMPORTANT]
57+
> The preceding code snippet loads the App Configuration connection string from an environment variable called `AppConfigConnectionString`. Be sure that this environment variable is set properly.
5858

59-
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.
59+
This calls the `Select` method 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.
6060

61-
## Testing in different environments
61+
## Test in different environments
6262

63-
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`.
63+
Follow these steps to test the different configuration values:
6464

65-
With the new values set, build and run your application.
65+
1. 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`.
6666

67-
```dotnetcli
68-
dotnet build
69-
dotnet run
70-
```
67+
1. With the new values set, build and run your application.
7168

72-
Use a web browser to navigate to `http://localhost:5000`. You'll notice that the font color is black.
69+
```dotnetcli
70+
dotnet build
71+
dotnet run
72+
```
7373

74-
![Web application running with production configuration](media/labels-website-prod.png)
74+
1. Use a web browser to go to `http://localhost:5000`. You'll notice that the font color is black.
7575
76-
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.
76+
![Web application running with production configuration](media/labels-website-prod.png)
7777

78-
![Web application running with development configuration](media/labels-website-dev.png)
78+
1. Update `launchSettings.json` to set the `ASPNETCORE_ENVIRONMENT` variable to `Development`. Run `dotnet run` again.
79+
80+
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.
81+
82+
![Web application running with development configuration](media/labels-website-dev.png)
7983

8084
## Next steps
8185

0 commit comments

Comments
 (0)