Skip to content

Commit faa5ca4

Browse files
Merge pull request #249863 from amerjusupovic/ajusupovic/dotnet-refresh-using-di
Add example for .NET core configuration refresh using DI
2 parents 7004dc3 + 4a70ecb commit faa5ca4

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,60 @@ In the `ConfigureRefresh` method, a key within your App Configuration store is r
130130

131131
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 phantom 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.
132132

133+
### Configuration refresh using dependency injection
134+
135+
In the previous code, you're manually saving an instance of `IConfigurationRefresher` to invoke `TryRefreshAsync`. Alternatively, if you're using dependency injection to resolve your services, you can reference the following steps.
136+
137+
1. Register the required App Configuration services by invoking `AddAzureAppConfiguration` on your `IServiceCollection`.
138+
139+
#### [.NET 6.0+](#tab/core6x)
140+
Add the following code to *Program.cs*.
141+
142+
```csharp
143+
// Existing code in Program.cs
144+
// ... ...
145+
146+
// Add Azure App Configuration services to IServiceCollection
147+
builder.Services.AddAzureAppConfiguration();
148+
```
149+
150+
#### [.NET Core 3.x](#tab/core3x)
151+
Open *Startup.cs*, and update the `ConfigureServices` method.
152+
153+
```csharp
154+
public void ConfigureServices(IServiceCollection services)
155+
{
156+
// Add Azure App Configuration services to IServiceCollection
157+
services.AddAzureAppConfiguration();
158+
159+
// Existing code
160+
// ... ...
161+
}
162+
```
163+
---
164+
165+
1. Refresh your configuration by resolving an instance of `IConfigurationRefresherProvider` from your service collection and invoking `TryRefreshAsync` on each of its refreshers.
166+
167+
```csharp
168+
class SampleConfigRefresher
169+
{
170+
private readonly IEnumerable<IConfigurationRefresher> _refreshers = null;
171+
172+
public SampleConfigRefresher(IConfigurationRefresherProvider refresherProvider)
173+
{
174+
_refreshers = refresherProvider.Refreshers;
175+
}
176+
177+
public async Task RefreshConfiguration()
178+
{
179+
foreach (var refresher in _refreshers)
180+
{
181+
_ = refresher.TryRefreshAsync();
182+
}
183+
}
184+
}
185+
```
186+
133187
## Build and run the app locally
134188

135189
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:

0 commit comments

Comments
 (0)