You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/azure-app-configuration/enable-dynamic-configuration-aspnet-core.md
+42-33Lines changed: 42 additions & 33 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -21,11 +21,11 @@ ms.custom: mvc
21
21
---
22
22
# Tutorial: Use dynamic configuration in an ASP.NET Core app
23
23
24
-
ASP.NET Core has a pluggable configuration system that can read configuration data from a variety of sources. It can handle changes on the fly without causing an application to restart. ASP.NET Core supports the binding of configuration settings to strongly typed .NET classes. It injects them into your code by using the various `IOptions<T>` patterns. One of these patterns, specifically `IOptionsSnapshot<T>`, automatically reloads the application's configuration when the underlying data changes. You can inject `IOptionsSnapshot<T>` into controllers in your application to access the most recent configuration stored in Azure App Configuration.
24
+
ASP.NET Core has a pluggable configuration system that can read configuration data from a variety of sources. It can handle changes dynamically without causing an application to restart. ASP.NET Core supports the binding of configuration settings to strongly typed .NET classes. It injects them into your code by using the various `IOptions<T>` patterns. One of these patterns, specifically `IOptionsSnapshot<T>`, automatically reloads the application's configuration when the underlying data changes. You can inject `IOptionsSnapshot<T>` into controllers in your application to access the most recent configuration stored in Azure App Configuration.
25
25
26
-
You also can set up the App Configuration ASP.NET Core client library to refresh a set of configuration settings dynamically using a middleware. As long as the web app continues to receive requests, the configuration settings continue to get updated with the configuration store.
26
+
You also can set up the App Configuration ASP.NET Core client library to refresh a set of configuration settings dynamically using a middleware. The configuration settings get updated with the configuration store each time as long as the web app receives requests.
27
27
28
-
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.
28
+
App Configuration automatically caches each setting to avoid too many calls to the configuration store. The refresh operation waits until the cached value of a setting expires to update that setting, even when its value changes in the configuration store. The default cache expiration time is 30 seconds. You can override this expiration time, if necessary.
29
29
30
30
This tutorial shows how you can implement dynamic configuration updates in your code. It builds on the web app introduced in the quickstarts. Before you continue, finish [Create an ASP.NET Core app with App Configuration](./quickstart-aspnet-core-app.md) first.
31
31
@@ -45,6 +45,16 @@ To do this tutorial, install the [.NET Core SDK](https://dotnet.microsoft.com/do
45
45
46
46
Before you continue, finish [Create an ASP.NET Core app with App Configuration](./quickstart-aspnet-core-app.md) first.
47
47
48
+
## Add a sentinel key
49
+
50
+
A *sentinel key* is a special key used to signal when configuration has changed. Your app monitors the sentinel key for changes. When a change is detected, you refresh all configuration values. This approach reduces the overall number of requests made by your app to App Configuration, compared to monitoring all keys for changes.
51
+
52
+
1. In the Azure portal, select **Configuration Explorer > Create > Key-value**.
53
+
54
+
1. For **Key**, enter *TestApp:Settings:Sentinel*. For **Value**, enter 1. Leave **Label** and **Content type** blank.
55
+
56
+
1. Select **Apply**.
57
+
48
58
## Reload data from App Configuration
49
59
50
60
1. Add a reference to the `Microsoft.Azure.AppConfiguration.AspNetCore` NuGet package by running the following command:
@@ -67,12 +77,11 @@ Before you continue, finish [Create an ASP.NET Core app with App Configuration](
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. In order to actually trigger a refresh operation, a refresh middleware needs to be configured for the application to refresh the configuration data when any change occurs.
113
+
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. The `refreshAll` parameter to the `Register` method indicates that all configuration values should be refreshed if the sentinel key changes.
114
+
115
+
Also, the `SetCacheExpiration` method overrides the default cache expiration time of 30 seconds, specifying a time of 1 second instead.
116
+
117
+
> [!WARNING]
118
+
> Use higher cache expiration times in production to avoid unnecessary requests to App Configuration.
119
+
120
+
To actually trigger a refresh operation, configure a refresh middleware for the application to refresh the configuration data when any change occurs.
106
121
107
122
2. Add a *Settings.cs* file that defines and implements a new `Settings` class.
108
123
@@ -199,10 +214,7 @@ Before you continue, finish [Create an ASP.NET Core app with App Configuration](
199
214
```
200
215
---
201
216
202
-
The middleware uses the refresh configuration specified in the `AddAzureAppConfiguration` method in `Program.cs` to trigger a refresh for each request received by the ASP.NET Core web app. For each request, a refresh operation is triggered and the client library checks if the cached value for the registered configuration settings have expired. For the cached values that have expired, the values for the settings are updated with the App Configuration store, and the remaining values remain unchanged.
203
-
204
-
> [!NOTE]
205
-
> 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.
217
+
The middleware uses the refresh configuration specified in the `AddAzureAppConfiguration` method in `Program.cs` to trigger a refresh for each request received by the ASP.NET Core web app. For each request, a refresh operation is triggered and the client library checks if the cached value for the registered configuration setting has expired. If it's expired, it's refreshed.
206
218
207
219
## Use the latest configuration data
208
220
@@ -279,7 +291,7 @@ Before you continue, finish [Create an ASP.NET Core app with App Configuration](
279
291
}
280
292
h1 {
281
293
color: @ViewData["FontColor"];
282
-
font-size: @ViewData["FontSize"];
294
+
font-size: @ViewData["FontSize"]px;
283
295
}
284
296
</style>
285
297
<head>
@@ -297,38 +309,35 @@ Before you continue, finish [Create an ASP.NET Core app with App Configuration](
297
309
298
310
dotnet build
299
311
300
-
2. After the build successfully completes, run the following command to run the web app locally:
312
+
1. After the build successfully completes, run the following command to run the web app locally:
301
313
302
314
dotnet run
315
+
1. Open a browser window, and go to `http://localhost:5000`, which is the default URL for the web app hosted locally.
303
316
304
-
3. Open a browser window, and go to `http://localhost:5000`, which is the default URL for the web app hosted locally.
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.
307
320
308
-
4. 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.
309
-
310
-
5. Select **Configuration Explorer**, and update the values of the following keys:
321
+
1. Select **Configuration Explorer**, and update the values of the following keys:
311
322
312
323
| Key | Value |
313
324
|---|---|
314
325
| TestApp:Settings:BackgroundColor | green |
315
326
| TestApp:Settings:FontColor | lightGray |
316
327
| TestApp:Settings:Message | Data from Azure App Configuration - now with live updates! |
328
+
| TestApp:Settings:Sentinel | 2 |
317
329
318
-
6. Refresh the browser page to see the new configuration settings. More than one refresh of the browser page may be required for the changes to be reflected.
330
+
1. Refresh the browser page to see the new configuration settings. You may need to refresh more than once for the changes to be reflected.
> Since the configuration settings are cached with a default expiration time of 30 seconds, any changes made to the settings in the App Configuration store would only be reflected in the web app when the cache has expired.
In this tutorial, you enabled your ASP.NET Core web app to dynamically refresh configuration settings from App Configuration. To learn how to use an Azuremanaged identity to streamline the access to App Configuration, continue to the next tutorial.
340
+
In this tutorial, you enabled your ASP.NET Core web app to dynamically refresh configuration settings from App Configuration. To learn how to use an Azure-managed identity to streamline the access to App Configuration, continue to the next tutorial.
0 commit comments