Skip to content

Commit c22c261

Browse files
committed
Updates, including new tutorial
1 parent 7067b0f commit c22c261

File tree

5 files changed

+171
-18
lines changed

5 files changed

+171
-18
lines changed
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
---
2+
title: Tutorial for using Azure App Configuration dynamic configuration in a .NET Framework app | Microsoft Docs
3+
description: In this tutorial, you learn how to dynamically update the configuration data for .NET Framework apps
4+
services: azure-app-configuration
5+
documentationcenter: ''
6+
author: lisaguthrie
7+
manager: maiye
8+
editor: ''
9+
10+
ms.assetid:
11+
ms.service: azure-app-configuration
12+
ms.workload: tbd
13+
ms.devlang: csharp
14+
ms.topic: tutorial
15+
ms.date: 10/21/2019
16+
ms.author: lcozzens
17+
18+
#Customer intent: I want to dynamically update my .NET Framework app to use the latest configuration data in App Configuration.
19+
---
20+
# Tutorial: Use dynamic configuration in a .NET Framework app
21+
22+
The App Configuration .NET client library supports updating a set of configuration settings on demand without causing an application to restart. This can be implemented by first getting an instance of `IConfigurationRefresher` from the options for the configuration provider and then calling `Refresh` on that instance anywhere in your code.
23+
24+
In order to keep the settings updated and avoid too many calls to the configuration store, a cache is used for each setting. Until the cached value of a setting has expired, the refresh operation does not update the value, even when the value has changed in the configuration store. The default expiration time for each request is 30 seconds, but it can be overridden if required.
25+
26+
This tutorial shows how you can implement dynamic configuration updates in your code. It builds on the app introduced in the quickstarts. Before you continue, finish [Create a .NET Framework app with App Configuration](./quickstart-dotnet-app.md) first.
27+
28+
In this tutorial, you learn how to:
29+
30+
> [!div class="checklist"]
31+
> * Set up your application to update its configuration with an app configuration store on demand.
32+
> * Inject the latest configuration in your application's controllers.
33+
34+
## Prerequisites
35+
36+
- Azure subscription - [create one for free](https://azure.microsoft.com/free/)
37+
- [Visual Studio 2019](https://visualstudio.microsoft.com/vs)
38+
- [.NET Framework 4.7.1 or later](https://dotnet.microsoft.com/download)
39+
40+
## Create an app configuration store
41+
42+
[!INCLUDE [azure-app-configuration-create](../../includes/azure-app-configuration-create.md)]
43+
44+
6. Select **Configuration Explorer** > **+ Create** to add the following key-value pairs:
45+
46+
| Key | Value |
47+
|---|---|
48+
| TestApp:Settings:Message | Data from Azure App Configuration |
49+
50+
Leave **Label** and **Content Type** empty for now.
51+
52+
## Create a .NET console app
53+
54+
1. Start Visual Studio, and select **File** > **New** > **Project**.
55+
56+
1. In **Create a new project**, filter on the **Console** project type and click on **Console App (.NET Framework)**. Click **Next**.
57+
58+
1. In **Configure your new project**, enter a project name. Under **Framework**, select **.NET Framework 4.7.1** or higher. Click **Create**.
59+
60+
## Reload data from App Configuration
61+
1. Right-click your project, and select **Manage NuGet Packages**. On the **Browse** tab, search and add the `Microsoft.Extensions.Configuration.AzureAppConfiguration` NuGet package to your project. If you can't find it, select the **Include prerelease** check box.
62+
63+
1. Open *Program.cs*, and add a reference to the .NET Core App Configuration provider.
64+
65+
```csharp
66+
using Microsoft.Extensions.Configuration;
67+
using Microsoft.Extensions.Configuration.AzureAppConfiguration;
68+
```
69+
70+
1. Add two variables to store configuration-related objects.
71+
72+
```csharp
73+
private static IConfiguration _configuration = null;
74+
private static IConfigurationRefresher _refresher = null;
75+
```
76+
77+
1. Update the `Main` method to connect to App Configuration with the specified refresh options.
78+
79+
```csharp
80+
static void Main(string[] args)
81+
{
82+
var builder = new ConfigurationBuilder();
83+
builder.AddAzureAppConfiguration(options =>
84+
{
85+
options.Connect(Environment.GetEnvironmentVariable("ConnectionString"))
86+
.ConfigureRefresh(refresh =>
87+
{
88+
refresh.Register("TestApp:Settings:Message")
89+
.SetCacheExpiration(TimeSpan.FromSeconds(10));
90+
});
91+
92+
_refresher = options.GetRefresher();
93+
});
94+
95+
_configuration = builder.Build();
96+
PrintMessage().Wait();
97+
}
98+
```
99+
The `ConfigureRefresh` method is used to specify the settings used to update the configuration data with the app configuration store when a refresh operation is triggered. An instance of `IConfigurationRefresher` can be retrieved by calling `GetRefresher` method on the options provided to `AddAzureAppConfiguration` method, and the `Refresh` method on this instance can be used to trigger a refresh operation anywhere in your code.
100+
101+
> [!NOTE]
102+
> The default cache expiration time for a configuration setting is 30 seconds, but can be overridden by calling the `SetCacheExpiration` method on the options initializer passed as an argument to the `ConfigureRefresh` method.
103+
104+
1. Add a method called `PrintMessage()` that triggers a manual refresh of configuration data from App Configuration.
105+
106+
```csharp
107+
private static async Task PrintMessage()
108+
{
109+
Console.WriteLine(_configuration["TestApp:Settings:Message"] ?? "Hello world!");
110+
111+
// Wait for the user to press Enter
112+
Console.ReadLine();
113+
114+
await _refresher.Refresh();
115+
Console.WriteLine(_configuration["TestApp:Settings:Message"] ?? "Hello world!");
116+
}
117+
```
118+
119+
## Build and run the app locally
120+
121+
1. Set an environment variable named **ConnectionString**, and set it to the access key to your app configuration store. If you use the Windows command prompt, run the following command and restart the command prompt to allow the change to take effect:
122+
123+
setx ConnectionString "connection-string-of-your-app-configuration-store"
124+
125+
If you use Windows PowerShell, run the following command:
126+
127+
$Env:ConnectionString = "connection-string-of-your-app-configuration-store"
128+
129+
1. Restart Visual Studio to allow the change to take effect.
130+
131+
1. Press Ctrl + F5 to build and run the console app.
132+
133+
![App launch local](./media/dotnet-app-run.png)
134+
135+
1. Sign in to the [Azure portal](https://portal.azure.com). Select **All resources**, and select the app configuration store instance that you created in the quickstart.
136+
137+
1. Select **Configuration Explorer**, and update the values of the following keys:
138+
139+
| Key | Value |
140+
|---|---|
141+
| TestApp:Settings:Message | Data from Azure App Configuration - Updated |
142+
143+
1. Back in the running application, press the Enter key to trigger a refresh and print the updated value in the Command Prompt or PowerShell window.
144+
145+
![App refresh local](./media/dotnet-app-run-refresh.png)
146+
147+
> [!NOTE]
148+
> Since the cache expiration time was set to 10 seconds using the `SetCacheExpiration` method while specifying the configuration for the refresh operation, the value for the configuration setting will only be updated if at least 10 seconds have elapsed since the last refresh for that setting.
149+
150+
## Clean up resources
151+
152+
[!INCLUDE [azure-app-configuration-cleanup](../../includes/azure-app-configuration-cleanup.md)]
153+
154+
## Next steps
155+
156+
In this tutorial, you added an Azure managed service identity to streamline access to App Configuration and improve credential management for your app. To learn how to add an Azure-managed service identity that streamlines access to App Configuration, continue to the next tutorial.
157+
158+
> [!div class="nextstepaction"]
159+
> [Managed identity integration](./howto-integrate-azure-managed-service-identity.md)
29.1 KB
Loading
19.4 KB
Loading
22.9 KB
Loading

articles/azure-app-configuration/quickstart-feature-flag-dotnet.md

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ ms.devlang: csharp
1313
ms.topic: quickstart
1414
ms.tgt_pltfrm: .NET
1515
ms.workload: tbd
16-
ms.date: 10/09/2019
16+
ms.date: 10/21/2019
1717
ms.author: lcozzens
1818

1919
#Customer intent: As a .NET Framework developer, I want to use feature flags to control feature availability quickly and confidently.
@@ -34,14 +34,6 @@ The .NET Feature Management libraries extend the framework with comprehensive fe
3434

3535
[!INCLUDE [azure-app-configuration-create](../../includes/azure-app-configuration-create.md)]
3636

37-
6. Select **Configuration Explorer** > **+ Create** to add the following key-value pairs:
38-
39-
| Key | Value |
40-
|---|---|
41-
| TestApp:Settings:Message | Data from Azure App Configuration |
42-
43-
Leave **Label** and **Content Type** empty for now.
44-
4537
## Create a .NET console app
4638

4739
1. Start Visual Studio, and select **File** > **New** > **Project**.
@@ -55,8 +47,9 @@ The .NET Feature Management libraries extend the framework with comprehensive fe
5547
1. Right-click your project, and select **Manage NuGet Packages**. On the **Browse** tab, search and add the following NuGet packages to your project. If you can't find them, select the **Include prerelease** check box.
5648

5749
```
58-
Microsoft.Azure.AppConfiguration.AspNetCore
59-
Microsoft.FeatureManagement.AspNetCore
50+
Microsoft.Extensions.DependencyInjection
51+
Microsoft.Extensions.Configuration.AzureAppConfiguration
52+
Microsoft.FeatureManagement
6053
```
6154
6255
1. Open *Program.cs* and add the following statements:
@@ -68,25 +61,25 @@ The .NET Feature Management libraries extend the framework with comprehensive fe
6861
using Microsoft.FeatureManagement;
6962
```
7063
71-
1. Update the `Main` method to display a message if the `Beta` feature flag is enabled.
64+
1. Update the `Main` method to connect to App Configuration, specifying the `UseFeatureFlags` option so that feature flags are retrieved. Then display a message if the `Beta` feature flag is enabled.
7265
7366
```csharp
7467
static void Main(string[] args)
7568
{
76-
IConfigurationRoot configuration = new Microsoft.Extensions.Configuration.ConfigurationBuilder()
69+
IConfigurationRoot configuration = new ConfigurationBuilder()
7770
.AddAzureAppConfiguration(options =>
7871
{
7972
options.Connect(Environment.GetEnvironmentVariable("ConnectionString"))
8073
.UseFeatureFlags();
81-
}).Build();
74+
}).Build();
8275
8376
IServiceCollection services = new ServiceCollection();
8477
services.AddSingleton<IConfiguration>(configuration).AddFeatureManagement();
8578
IFeatureManager featureManager = services.BuildServiceProvider().GetRequiredService<IFeatureManager>();
8679
8780
if (featureManager.IsEnabled("Beta"))
8881
{
89-
Console.WriteLine("Warning: You are using a beta version of this application.");
82+
Console.WriteLine("Welcome to the beta");
9083
}
9184
9285
Console.WriteLine("Hello World!");
@@ -107,13 +100,14 @@ The .NET Feature Management libraries extend the framework with comprehensive fe
107100
108101
1. Press Ctrl + F5 to build and run the console app.
109102
103+
![App with feature flag enabled](./media/quickstarts/dotnet-app-feature-flag.png)
104+
110105
## Clean up resources
111106
112107
[!INCLUDE [azure-app-configuration-cleanup](../../includes/azure-app-configuration-cleanup.md)]
113108
114109
## Next steps
115110
116-
In this quickstart, you created a feature flag in App Configuration and used it with a .NET Framework console app. To learn more about how to use App Configuration, continue to the next tutorial that demonstrates authentication.
111+
In this quickstart, you created a feature flag in App Configuration and used it with a .NET Framework console app. To learn how to dynamically update feature flags without restarting the application, continue to the next tutorial.
117112
118-
> [!div class="nextstepaction"]
119-
> [Managed identity integration](./howto-integrate-azure-managed-service-identity.md)
113+
[Enable dynamic configuration](./enable-dynamic-configuration-dotnet.md)

0 commit comments

Comments
 (0)