|
| 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 | +  |
| 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 | +  |
| 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) |
0 commit comments