forked from Azure/AppConfiguration-DotnetProvider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
101 lines (81 loc) · 3.56 KB
/
Program.cs
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
//
namespace Microsoft.Extensions.Configuration.AzureAppConfiguration.Examples.ConsoleApplication
{
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.AzureAppConfiguration;
using System;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
class Program
{
static IConfiguration Configuration { get; set; }
static IConfigurationRefresher _refresher;
static void Main(string[] args)
{
Configure();
var cts = new CancellationTokenSource();
_ = Run(cts.Token);
// Finish on key press
Console.ReadKey();
cts.Cancel();
}
private static void Configure()
{
var builder = new ConfigurationBuilder();
// Load a subset of the application's configuration from a json file and environment variables
builder.AddJsonFile("appsettings.json")
.AddEnvironmentVariables();
IConfiguration configuration = builder.Build();
if (string.IsNullOrEmpty(configuration["connection_string"]))
{
Console.WriteLine("Connection string not found.");
Console.WriteLine("Please set the 'connection_string' environment variable to a valid Azure App Configuration connection string and re-run this example.");
return;
}
// Augment the configuration builder with Azure App Configuration
// Pull the connection string from an environment variable
builder.AddAzureAppConfiguration(options =>
{
options.Connect(configuration["connection_string"])
.Select("AppName")
.Select("Settings:BackgroundColor")
.ConfigureClientOptions(clientOptions => clientOptions.Retry.MaxRetries = 5)
.ConfigureRefresh(refresh =>
{
refresh.Register("AppName")
.Register("Language", refreshAll: true)
.SetCacheExpiration(TimeSpan.FromSeconds(10));
});
// Get an instance of the refresher that can be used to refresh data
_refresher = options.GetRefresher();
});
Configuration = builder.Build();
}
private static async Task Run(CancellationToken token)
{
string display = string.Empty;
StringBuilder sb = new StringBuilder();
while (!token.IsCancellationRequested)
{
// Trigger an async refresh for registered configuration settings without wait
_ = _refresher.TryRefreshAsync();
sb.AppendLine($"{Configuration["AppName"]} has been configured to run in {Configuration["Language"]}");
sb.AppendLine();
sb.AppendLine(string.Equals(Configuration["Language"], "spanish", StringComparison.OrdinalIgnoreCase) ? "Buenos Dias." : "Good morning");
sb.AppendLine();
sb.AppendLine("Press any key to exit...");
await Task.Delay(1000);
if (!sb.ToString().Equals(display))
{
display = sb.ToString();
Console.Clear();
Console.Write(display);
}
sb.Clear();
}
}
}
}