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
+6-15Lines changed: 6 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -25,14 +25,6 @@ In this tutorial, you learn how to:
25
25
26
26
Finish the quickstart: [Create an ASP.NET Core app with App Configuration](./quickstart-aspnet-core-app.md).
27
27
28
-
## Add a sentinel key
29
-
30
-
A *sentinel key* is a key that you update after you complete the change of all other keys. Your app monitors the sentinel key. When a change is detected, your app refreshes all configuration values. This approach helps to ensure the consistency of configuration in your app and reduces the overall number of requests made to your App Configuration store, compared to monitoring all keys for changes.
31
-
32
-
1. In the Azure portal, open your App Configuration store and select **Configuration Explorer > Create > Key-value**.
33
-
1. For **Key**, enter *TestApp:Settings:Sentinel*. For **Value**, enter 1. Leave **Label** and **Content type** blank.
34
-
1. Select **Apply**.
35
-
36
28
## Reload data from App Configuration
37
29
38
30
1. Open *Program.cs*, and update the `AddAzureAppConfiguration` method you added during the quickstart. You can connect to App Configuration using either Microsoft Entra ID (recommended) or a connection string. The following code snippet demonstrates using Microsoft Entra ID.
@@ -44,17 +36,17 @@ A *sentinel key* is a key that you update after you complete the change of all o
The `Select` methodisusedtoloadallkey-valueswhosekeynamestartswith *TestApp:*andthathave*nolabel*. Youcancallthe `Select` methodmorethanoncetoloadconfigurationswithdifferentprefixesorlabels. IfyoushareoneAppConfigurationstorewithmultipleapps, thisapproachhelpsloadconfigurationonlyrelevanttoyourcurrentappinsteadofloadingeverythingfromyourstore.
56
48
57
-
Inthe `ConfigureRefresh` method, youregisterkeysyouwanttomonitorfor changes in your App Configuration store. The `refreshAll` parameter to the `Register` method indicates that all configurations you specified by the `Select` method will be reloaded if the registered key changes.
49
+
Insidethe `ConfigureRefresh` method, youcallthe `RegisterAll` methodtoinstructtheAppConfigurationprovidertoreloadtheentireconfigurationwheneveritdetectsachangeinanyoftheselectedkey-values (thosestartingwith*TestApp:*andhavingnolabel). Formoreinformationaboutmonitoringconfigurationchanges, see [Bestpracticesforconfigurationrefresh](./howto-best-practices.md#configuration-refresh).
@@ -108,7 +100,7 @@ You've set up your app to use the [options pattern in ASP.NET Core](/aspnet/core
108
100
109
101
## Request-driven configuration refresh
110
102
111
-
Theconfigurationrefreshistriggeredbytheincomingrequeststoyourwebapp. Norefreshwilloccurifyourappisidle. Whenyourappisactive, theAppConfigurationmiddlewaremonitorsthesentinelkey, oranyotherkeysyouregisteredfor refreshing in the `ConfigureRefresh` call. The middleware is triggered upon every incoming request to your app. However, the middleware will only send requests to check the value in App Configuration when the cache expiration time you set has passed.
103
+
Theconfigurationrefreshistriggeredbytheincomingrequeststoyourwebapp. Norefreshwilloccurifyourappisidle. Whenyourappisactive, theAppConfigurationmiddlewaremonitorsanykeysyouregisteredfor refreshing in the `ConfigureRefresh` call. The middleware is triggered upon every incoming request to your app. However, the middleware will only send requests to check the value in App Configuration when the cache expiration time you set has passed.
112
104
113
105
- If a request to App Configuration for change detection fails, your app will continue to use the cached configuration. New attempts to check for changes will be made periodically while there are new incoming requests to your app.
114
106
- The configuration refresh happens asynchronously to the processing of your app's incoming requests. It will not block or slow down the incoming request that triggered the refresh. The request that triggered the refresh may not get the updated configuration values, but later requests will get new configuration values.
@@ -134,14 +126,13 @@ The configuration refresh is triggered by the incoming requests to your web app.
134
126
135
127
1. Signintothe [Azureportal](https://portal.azure.com). Select **All resources**, and select the App Configuration store that you created in the quickstart.
1. Refreshthebrowserpagetoseethenewconfigurationsettings. Youmayneedtorefreshmorethanoncefor the changes to be reflected or change your cache expiration time to less than 5 minutes.
// Register the refresher so that the Worker service can consume it through DI
@@ -129,7 +129,10 @@ You use the [.NET command-line interface (CLI)](/dotnet/core/tools/) to create a
129
129
```
130
130
---
131
131
132
-
In the `ConfigureRefresh` method, a key within your App Configuration store is registered for change monitoring. The `Register` method has an optional boolean parameter `refreshAll` that can be used to indicate whether all configuration values should be refreshed if the registered key changes. In this example, only the key *TestApp:Settings:Message* will be refreshed. All settings registered for refresh have a default cache expiration of 30 seconds before a new refresh is attempted. It can be updated by calling the `AzureAppConfigurationRefreshOptions.SetRefreshInterval` method.
132
+
Inside the `ConfigureRefresh` method, you call the `RegisterAll` method to instruct the App Configuration provider to reload the entire configuration whenever it detects a change in any of the selected key-values (those starting with *TestApp:* and having no label). For more information about monitoring configuration changes, see [Best practices for configuration refresh](./howto-best-practices.md#configuration-refresh).
133
+
134
+
> [!TIP]
135
+
> You can add a call to the `refreshOptions.SetRefreshInterval` method to specify the minimum time between configuration refreshes. In this example, you use the default value of 30 seconds. Adjust to a higher value if you need to reduce the number of requests made to your App Configuration store.
133
136
134
137
1. Open *Worker.cs*. Inject `IConfiguration` and `IConfigurationRefresher` to the `Worker` service and log the configuration data from App Configuration.
// Load the key-value with key "TestApp:Settings:Message" and no label
112
+
options.Select("TestApp:Settings:Message");
113
+
// Reload configuration if any selected key-values have changed.
111
114
options.ConfigureRefresh(refresh=>
112
115
refresh
113
-
.Register("TestApp:Settings:Message")
116
+
.RegisterAll()
114
117
// Important: Reduce poll frequency
115
118
.SetRefreshInterval(TimeSpan.FromDays(1))
116
119
);
@@ -170,7 +173,7 @@ namespace TestConsole
170
173
}
171
174
```
172
175
173
-
The `ProcessPushNotification` method resets the cache expiration to a short random delay. This causes future calls to `RefreshAsync` or `TryRefreshAsync` to re-validate the cached values against App Configuration and update them as necessary. In this example you register to monitor changes to the key: *TestApp:Settings:Message* with a cache expiration of one day. This means no request to App Configuration will be made before a day has passed since the last check. By calling `ProcessPushNotification` your application will send requests to App Configuration in the next few seconds. Your application will load the new configuration values shortly after changes occur in the `App Configuration` store without the need to constantly poll for updates. In case your application misses the change notification for any reason, it will still check for configuration changes once a day.
176
+
The `ProcessPushNotification` method resets the cache expiration to a short random delay. This causes future calls to `RefreshAsync` or `TryRefreshAsync` to re-validate the cached values against App Configuration and update them as necessary. In this example you register to monitor changes to all selected key-values (*TestApp:Settings:Message*) with a cache expiration of one day. This means no request to App Configuration will be made before a day has passed since the last check. By calling `ProcessPushNotification` your application will send requests to App Configuration in the next few seconds. Your application will load the new configuration values shortly after changes occur in the `App Configuration` store without the need to constantly poll for updates. In case your application misses the change notification for any reason, it will still check for configuration changes once a day.
174
177
175
178
The short random delay for cache expiration is helpful if you have many instances of your application or microservices connecting to the same App Configuration store with the push model. Without this delay, all instances of your application could send requests to your App Configuration store simultaneously as soon as they receive a change notification. This can cause the App Configuration Service to throttle your store. Cache expiration delay is set to a random number between 0 and a maximum of 30 seconds by default, but you can change the maximum value through the optional parameter `maxDelay` to the `ProcessPushNotification` method.
// Load the key-value with key "TestApp:Settings:Message" and no label
50
+
.Select("TestApp:Settings:Message")
51
+
// Reload configuration if any selected key-values have changed.
49
52
.ConfigureRefresh(refresh=>
50
53
{
51
-
refresh.Register("TestApp:Settings:Message")
54
+
refresh.RegisterAll()
52
55
.SetRefreshInterval(TimeSpan.FromSeconds(10));
53
56
});
54
57
@@ -70,7 +73,9 @@ if (_refresher != null)
70
73
}
71
74
```
72
75
73
-
In the `ConfigureRefresh` method, a key within your App Configuration store is registered for change monitoring. The `Register` method has an optional boolean parameter `refreshAll` that can be used to indicate whether all configuration values should be refreshed if the registered key changes. In this example, only the key *TestApp:Settings:Message* will be refreshed. The `SetRefreshInterval` method specifies the minimum time that must elapse before a new request is made to App Configuration to check for any configuration changes. In this example, you override the default expiration time of 30 seconds, specifying a time of 10 seconds instead for demonstration purposes.
76
+
Inside the `ConfigureRefresh` method, you call the `RegisterAll` method to instruct the App Configuration provider to reload the entire configuration whenever it detects a change in any of the selected key-values (in this case, just *TestApp:Settings:Message*). For more information about monitoring configuration changes, see [Best practices for configuration refresh](./howto-best-practices.md#configuration-refresh).
77
+
78
+
The `SetRefreshInterval` method specifies the minimum time that must elapse before a new request is made to App Configuration to check for any configuration changes. In this example, you override the default expiration time of 30 seconds, specifying a time of 10 seconds instead for demonstration purposes.
74
79
75
80
Calling the `ConfigureRefresh` method alone won't cause the configuration to refresh automatically. You call the `TryRefreshAsync` method from the interface `IConfigurationRefresher` to trigger a refresh. This design is to avoid requests sent to App Configuration even when your application is idle. You'll want to include the `TryRefreshAsync` call where you consider your application active. For example, it can be when you process an incoming message, an order, or an iteration of a complex task. It can also be in a timer if your application is active all the time. In this example, you call `TryRefreshAsync` every time you press the Enter key. Even if the call `TryRefreshAsync` fails for any reason, your application continues to use the cached configuration. Another attempt is made when the configured cache expiration time has passed and the `TryRefreshAsync` call is triggered by your application activity again. Calling `TryRefreshAsync` is a no-op before the configured cache expiration time elapses, so its performance impact is minimal, even if it's called frequently.
0 commit comments