-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathProgram.cs
More file actions
69 lines (56 loc) · 2.38 KB
/
Program.cs
File metadata and controls
69 lines (56 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
//
using Azure.Identity;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.AzureAppConfiguration;
// Setup configuration builder
var builder = new ConfigurationBuilder();
// Load base configuration from appsettings.json and environment variables
builder.AddJsonFile("appsettings.json")
.AddEnvironmentVariables();
// Build initial configuration
var initialConfig = builder.Build();
// Check for App Configuration endpoint
string? endpoint = initialConfig["AppConfigurationEndpoint"];
if (string.IsNullOrEmpty(endpoint))
{
Console.WriteLine("App Configuration endpoint not found.");
Console.WriteLine("Please set the 'AppConfigurationEndpoint' environment variable to a valid Azure App Configuration endpoint URL and re-run this example.");
return 1;
}
// Connect to Azure App Configuration
IConfigurationRefresher refresher = null!;
builder.AddAzureAppConfiguration(options =>
{
// Use DefaultAzureCredential for Microsoft Entra ID authentication
options.Connect(new Uri(endpoint), new DefaultAzureCredential())
// Load all keys that start with "Settings:" and have no label.
.Select("Settings:*")
.TrimKeyPrefix("Settings:")
.ConfigureRefresh(refreshOptions =>
{
// Reload configuration if any selected key-values have changed.
// Use the default refresh interval of 30 seconds. It can be overridden via refreshOptions.SetRefreshInterval.
refreshOptions.RegisterAll();
});
// Get an instance of the refresher that can be used to refresh data
refresher = options.GetRefresher();
});
// Build the final configuration
IConfiguration configuration = builder.Build();
// Application main loop
while (true)
{
// Trigger and wait for an async refresh for configuration settings
await refresher.TryRefreshAsync();
Console.Clear();
Console.WriteLine($"{configuration["AppName"]} has been configured to run in {configuration["Language"]}");
Console.WriteLine();
Console.WriteLine(string.Equals(configuration["Language"], "Spanish", StringComparison.OrdinalIgnoreCase)
? "Buenos Dias."
: "Good morning");
Console.WriteLine();
Console.WriteLine("Press CTRL+C to exit...");
await Task.Delay(10000);
}