Skip to content

Commit 392d62a

Browse files
authored
Merge pull request #289870 from maud-lv/entra-dynamic-dotnet-background
Add Entra ID tab
2 parents 1bca15a + 01f8b23 commit 392d62a

File tree

1 file changed

+59
-20
lines changed

1 file changed

+59
-20
lines changed

articles/azure-app-configuration/enable-dynamic-configuration-dotnet-background-service.md

Lines changed: 59 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ ms.service: azure-app-configuration
99
ms.devlang: csharp
1010
ms.custom: devx-track-csharp, devx-track-dotnet
1111
ms.topic: tutorial
12-
ms.date: 02/20/2024
12+
ms.date: 11/12/2024
1313
ms.author: zhiyuanliang
1414
#Customer intent: I want to dynamically update my .NET background service to use the latest configuration data in App Configuration.
1515
---
@@ -70,8 +70,39 @@ You use the [.NET command-line interface (CLI)](/dotnet/core/tools/) to create a
7070
using Microsoft.Extensions.Configuration.AzureAppConfiguration;
7171
```
7272
73-
1. Connect to App Configuration.
73+
1. Connect to App Configuration using Microsoft Entra ID (recommended), or a connection string.
7474
75+
### [Microsoft Entra ID (recommended)](#tab/entra-id)
76+
77+
You use the `DefaultAzureCredential` to authenticate to your App Configuration store. Follow the [instructions](./concept-enable-rbac.md#authentication-with-token-credentials) to assign your credential the **App Configuration Data Reader role**. Be sure to allow sufficient time for the permission to propagate before running your application.
78+
79+
```csharp
80+
// Existing code in Program.cs
81+
// ... ...
82+
83+
var builder = Host.CreateApplicationBuilder(args);
84+
85+
builder.Configuration.AddAzureAppConfiguration(options =>
86+
{
87+
string endpoint = Environment.GetEnvironmentVariable("Endpoint");
88+
options.Connect(new Uri(endpoint), new DefaultAzureCredential());
89+
// Load all keys that start with `TestApp:`.
90+
.Select("TestApp:*")
91+
// Configure to reload the key 'TestApp:Settings:Message' if it is modified.
92+
.ConfigureRefresh(refreshOptions =>
93+
{
94+
refreshOptions.Register("TestApp:Settings:Message");
95+
});
96+
97+
// Register the refresher so that the Worker service can consume it through DI
98+
builder.Services.AddSingleton(options.GetRefresher());
99+
});
100+
101+
// The rest of existing code in Program.cs
102+
// ... ...
103+
```
104+
105+
### [Connection string](#tab/connection-string)
75106
```csharp
76107
// Existing code in Program.cs
77108
// ... ...
@@ -96,6 +127,7 @@ You use the [.NET command-line interface (CLI)](/dotnet/core/tools/) to create a
96127
// The rest of existing code in Program.cs
97128
// ... ...
98129
```
130+
---
99131
100132
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.SetCacheExpiration` method.
101133
@@ -137,42 +169,49 @@ You use the [.NET command-line interface (CLI)](/dotnet/core/tools/) to create a
137169
138170
## Build and run the app locally
139171
140-
1. Set an environment variable named **ConnectionString**, and set it to the access key to your App Configuration store. At the command line, run the following command.
172+
1. Set an environment variable.
141173
142-
### [Windows command prompt](#tab/windowscommandprompt)
174+
### [Microsoft Entra ID (recommended)](#tab/entra-id)
175+
Set the environment variable named **Endpoint** to the endpoint of your App Configuration store found under the *Overview* of your store in the Azure portal.
143176
144-
To build and run the app locally using the Windows command prompt, run the following command.
177+
If you use the Windows command prompt, run the following command and restart the command prompt to allow the change to take effect:
145178
146-
```console
147-
setx ConnectionString "connection-string-of-your-app-configuration-store"
179+
```cmd
180+
setx Endpoint "endpoint-of-your-app-configuration-store"
148181
```
149182
150-
Restart the command prompt to allow the change to take effect. Print the value of the environment variable to validate that it's set properly.
183+
If you use PowerShell, run the following command:
151184
152-
### [PowerShell](#tab/powershell)
185+
```powershell
186+
$Env:Endpoint = "endpoint-of-your-app-configuration-store"
187+
```
153188
154-
If you use Windows PowerShell, run the following command.
189+
If you use macOS or Linux, run the following command:
155190
156-
```azurepowershell
157-
$Env:ConnectionString = "connection-string-of-your-app-configuration-store"
191+
```bash
192+
export Endpoint='endpoint-of-your-app-configuration-store'
158193
```
159194
160-
### [macOS](#tab/unix)
195+
### [Connection string](#tab/connection-string)
196+
Set the environment variable named **ConnectionString** to the read-only connection string of your App Configuration store found under *Access keys* of your store in the Azure portal.
161197
162-
If you use macOS, run the following command.
198+
If you use the Windows command prompt, run the following command and restart the command prompt to allow the change to take effect:
163199
164-
```console
165-
export ConnectionString='connection-string-of-your-app-configuration-store'
200+
```cmd
201+
setx ConnectionString "connection-string-of-your-app-configuration-store"
166202
```
167203
168-
### [Linux](#tab/linux)
204+
If you use PowerShell, run the following command:
169205
170-
If you use Linux, run the following command.
206+
```powershell
207+
$Env:ConnectionString = "connection-string-of-your-app-configuration-store"
208+
```
209+
210+
If you use macOS or Linux, run the following command:
171211
172-
```console
212+
```bash
173213
export ConnectionString='connection-string-of-your-app-configuration-store'
174214
```
175-
176215
---
177216
178217
1. Run the following command to build the app.

0 commit comments

Comments
 (0)