Skip to content

Commit 285397e

Browse files
committed
Updating dynamic config tutorial to use sentinel pattern
1 parent 3f865bc commit 285397e

File tree

1 file changed

+42
-33
lines changed

1 file changed

+42
-33
lines changed

articles/azure-app-configuration/enable-dynamic-configuration-aspnet-core.md

Lines changed: 42 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ ms.custom: mvc
2121
---
2222
# Tutorial: Use dynamic configuration in an ASP.NET Core app
2323

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.
2525

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.
2727

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.
2929

3030
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.
3131

@@ -45,6 +45,16 @@ To do this tutorial, install the [.NET Core SDK](https://dotnet.microsoft.com/do
4545

4646
Before you continue, finish [Create an ASP.NET Core app with App Configuration](./quickstart-aspnet-core-app.md) first.
4747

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+
4858
## Reload data from App Configuration
4959

5060
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](
6777
config.AddAzureAppConfiguration(options =>
6878
{
6979
options.Connect(settings["ConnectionStrings:AppConfig"])
70-
.ConfigureRefresh(refresh =>
71-
{
72-
refresh.Register("TestApp:Settings:BackgroundColor")
73-
.Register("TestApp:Settings:FontColor")
74-
.Register("TestApp:Settings:Message");
75-
});
80+
.ConfigureRefresh(refresh =>
81+
{
82+
refresh.Register("TestApp:Settings:Sentinel", refreshAll: true)
83+
.SetCacheExpiration(new TimeSpan(0, 0, 1));
84+
});
7685
});
7786
})
7887
.UseStartup<Startup>();
@@ -90,19 +99,25 @@ Before you continue, finish [Create an ASP.NET Core app with App Configuration](
9099
config.AddAzureAppConfiguration(options =>
91100
{
92101
options.Connect(settings["ConnectionStrings:AppConfig"])
93-
.ConfigureRefresh(refresh =>
94-
{
95-
refresh.Register("TestApp:Settings:BackgroundColor")
96-
.Register("TestApp:Settings:FontColor")
97-
.Register("TestApp:Settings:Message");
98-
});
102+
.ConfigureRefresh(refresh =>
103+
{
104+
refresh.Register("TestApp:Settings:Sentinel", refreshAll: true)
105+
.SetCacheExpiration(new TimeSpan(0, 0, 1));
106+
});
99107
});
100108
})
101109
.UseStartup<Startup>());
102110
```
103111
---
104112
105-
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.
106121
107122
2. Add a *Settings.cs* file that defines and implements a new `Settings` class.
108123
@@ -199,10 +214,7 @@ Before you continue, finish [Create an ASP.NET Core app with App Configuration](
199214
```
200215
---
201216
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.
206218
207219
## Use the latest configuration data
208220
@@ -279,7 +291,7 @@ Before you continue, finish [Create an ASP.NET Core app with App Configuration](
279291
}
280292
h1 {
281293
color: @ViewData["FontColor"];
282-
font-size: @ViewData["FontSize"];
294+
font-size: @ViewData["FontSize"]px;
283295
}
284296
</style>
285297
<head>
@@ -297,38 +309,35 @@ Before you continue, finish [Create an ASP.NET Core app with App Configuration](
297309
298310
dotnet build
299311
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:
301313
302314
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.
303316
304-
3. Open a browser window, and go to `http://localhost:5000`, which is the default URL for the web app hosted locally.
317+
![Launching quickstart app locally](./media/quickstarts/aspnet-core-app-launch-local-before.png)
305318
306-
![Quickstart app launch local](./media/quickstarts/aspnet-core-app-launch-local-before.png)
319+
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.
307320
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:
311322
312323
| Key | Value |
313324
|---|---|
314325
| TestApp:Settings:BackgroundColor | green |
315326
| TestApp:Settings:FontColor | lightGray |
316327
| TestApp:Settings:Message | Data from Azure App Configuration - now with live updates! |
328+
| TestApp:Settings:Sentinel | 2 |
317329
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.
319331
320-
![Quickstart app refresh local](./media/quickstarts/aspnet-core-app-launch-local-after.png)
321-
322-
> [!NOTE]
323-
> 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.
332+
![Launching updated quickstart app locally](./media/quickstarts/aspnet-core-app-launch-local-after.png)
324333
325334
## Clean up resources
326335
327336
[!INCLUDE [azure-app-configuration-cleanup](../../includes/azure-app-configuration-cleanup.md)]
328337
329338
## Next steps
330339
331-
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.
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.
332341
333342
> [!div class="nextstepaction"]
334343
> [Managed identity integration](./howto-integrate-azure-managed-service-identity.md)

0 commit comments

Comments
 (0)