Skip to content

Commit 2cc3c58

Browse files
update
1 parent 634acb8 commit 2cc3c58

File tree

1 file changed

+50
-55
lines changed

1 file changed

+50
-55
lines changed

articles/azure-app-configuration/reference-dotnet-provider.md

Lines changed: 50 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -266,56 +266,58 @@ You can add a call to the `SetRefreshInterval` method to specify the minimum tim
266266

267267
To trigger refresh, you need to call the `TryRefreshAsync` method of the `IConfigurationRefresher`. Azure App Configuration provides several patterns for implementation depending on your application architecture.
268268

269-
#### 1. Direct access
269+
#### 1. Dependency injection
270270

271-
For applications not using dependency injection, you can obtain the refresher directly from the options:
271+
For applications using dependency injection (including ASP.NET Core and background services), register the refresher service:
272272

273273
```csharp
274-
IConfigurationRefresher refresher = null;
275-
var builder = new ConfigurationBuilder();
276-
builder.AddAzureAppConfiguration(options =>
274+
builder.Configuration.AddAzureAppConfiguration(options =>
277275
{
278276
options.Connect(new Uri(appConfigEndpoint), new DefaultAzureCredential())
279277
.ConfigureRefresh(refreshOptions =>
280278
{
281-
refreshOptions.RegisterAll();
282-
});
283-
284-
// Store the refresher for later use
285-
refresher = options.GetRefresher();
279+
refreshOptions.RegisterAll()
280+
.SetRefreshInterval(TimeSpan.FromSeconds(60));
281+
})
286282
});
287283

288-
IConfiguration config = builder.Build();
284+
// Register refresher service with the DI container
285+
builder.Services.AddAzureAppConfiguration();
286+
```
289287

290-
// Later in your code, trigger refresh when needed
291-
if (refresher != null)
292-
{
293-
await refresher.TryRefreshAsync()
294-
}
288+
`builder.Services.AddAzureAppConfiguration()` adds the `IConfigurationRefreshProvider` service to the DI container, which gives you access to the refreshers of all Azure App Configuration sources in the application's configuration.
295289

296-
Console.WriteLine(config["TestApp:Settings:Message"]);
297-
```
290+
#### ASP.NET Core applications
298291

299-
#### 2. Dependency injection
292+
For ASP.NET Core applications, you can use the `Microsoft.Azure.AppConfiguration.AspNetCore` package to achieve [request-driven configuration refresh](./enable-dynamic-configuration-aspnet-core#request-driven-configuration-refresh) with a built-in middleware.
300293

301-
For applications using dependency injection (including ASP.NET Core and Worker Services), register the refresher service:
294+
```console
295+
dotnet add package Microsoft.Azure.AppConfiguration.AspNetCore
296+
```
297+
298+
After registering the service, call the `UseAzureAppConfiguration` to add the `AzureAppConfigurationRefreshMiddleware` to your application pipeline to automatically refresh configuration on incoming requests:
302299

303300
```csharp
304-
builder.Configuration.AddAzureAppConfiguration(options =>
305-
{
306-
options.Connect(new Uri(appConfigEndpoint), new DefaultAzureCredential())
307-
.ConfigureRefresh(refreshOptions =>
308-
{
309-
refreshOptions.RegisterAll()
310-
.SetRefreshInterval(TimeSpan.FromSeconds(60));
311-
})
312-
});
301+
...
313302

314-
// Register refresher services with the DI container
303+
// Call the AddAzureAppConfiguration to add refresher service to the DI container
315304
builder.Services.AddAzureAppConfiguration();
305+
306+
var app = builder.Build();
307+
308+
// Call the app.UseAzureAppConfiguration() method as early as appropriate in your request pipeline so another middleware doesn't skip it
309+
app.UseAzureAppConfiguration();
310+
311+
// Continue with other middleware registration
312+
app.UseRouting();
313+
...
316314
```
317315

318-
`builder.Services.AddAzureAppConfiguration()` adds the `IConfigurationRefreshProvider` service to the DI container, which gives you access to the refreshers of all Azure App Configuration sources in the application's configuration.
316+
The `AzureAppConfigurationRefreshMiddleware` automatically checks for configuration changes at the configured refresh interval. This approach is efficient as it only refreshes when both conditions are met: an HTTP request is received and the refresh interval has elapsed.
317+
318+
#### Background services
319+
320+
For background services, you can inject the `IConfigurationRefresherProvider` service and manually refresh each of the registered refreshers.
319321

320322
```csharp
321323
public class Worker : BackgroundService
@@ -341,41 +343,34 @@ public class Worker : BackgroundService
341343
}
342344
```
343345

344-
#### 3. ASP.NET Core
345-
346-
You can use the `Microsoft.Azure.AppConfiguration.AspNetCore` package to use the `AzureAppConfigurationRefreshMiddleware` to achieve the request-driven configuration refresh.
347-
348-
```console
349-
dotnet add package Microsoft.Azure.AppConfiguration.AspNetCore
350-
```
346+
#### 2. Direct access
351347

352-
The middleware calls the `TryRefreshAsync` method on the registered `IConfigurationRefresher` when there are new incoming requests to your application.
348+
For applications not using dependency injection, you can obtain the refresher directly from the options:
353349

354350
```csharp
355-
builder.Configuration.AddAzureAppConfiguration(options =>
351+
IConfigurationRefresher refresher = null;
352+
var builder = new ConfigurationBuilder();
353+
builder.AddAzureAppConfiguration(options =>
356354
{
357355
options.Connect(new Uri(appConfigEndpoint), new DefaultAzureCredential())
358356
.ConfigureRefresh(refreshOptions =>
359357
{
360-
refreshOptions.RegisterAll()
361-
.SetRefreshInterval(TimeSpan.FromSeconds(60));
362-
})
363-
});
364-
365-
// Add Azure App Configuration middleware to the container of services.
366-
builder.Services.AddAzureAppConfiguration();
367-
368-
...
358+
refreshOptions.RegisterAll();
359+
});
369360

370-
var app = builder.Build();
361+
// Store the refresher for later use
362+
refresher = options.GetRefresher();
363+
});
371364

372-
// Call the app.UseAzureAppConfiguration() method as early as appropriate in your request pipeline so another middleware doesn't skip it
373-
app.UseAzureAppConfiguration();
365+
IConfiguration config = builder.Build();
374366

375-
// Continue with other middleware registration
376-
app.UseRouting();
377-
...
367+
// Later in your code, trigger refresh when needed
368+
if (refresher != null)
369+
{
370+
await refresher.TryRefreshAsync()
371+
}
378372

373+
Console.WriteLine(config["TestApp:Settings:Message"]);
379374
```
380375

381376
> [!NOTE]

0 commit comments

Comments
 (0)