Skip to content

Commit 269adf0

Browse files
authored
Upgrade the .NET console app sample to .NET 8 (#1058)
Upgrade from .NET Core 3.1 to .NET 8 Use the minimal project template style Switch to DefaultAzureCredential for authentication with Azure App Configuration
1 parent 319cb89 commit 269adf0

3 files changed

Lines changed: 75 additions & 95 deletions

File tree

examples/DotNetCore/ConsoleApplication/ConsoleApplication.csproj

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33
<PropertyGroup>
44
<IsPackable>false</IsPackable>
55
<OutputType>Exe</OutputType>
6-
<TargetFramework>netcoreapp3.1</TargetFramework>
6+
<TargetFramework>net8.0</TargetFramework>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
79
</PropertyGroup>
810

911
<ItemGroup>
10-
<PackageReference Include="Microsoft.Extensions.Configuration.AzureAppConfiguration" Version="3.0.0" />
11-
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.2" />
12-
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="3.1.2" />
12+
<PackageReference Include="Azure.Identity" Version="1.14.0" />
13+
<PackageReference Include="Microsoft.Extensions.Configuration.AzureAppConfiguration" Version="8.2.0" />
14+
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="9.0.5" />
15+
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="9.0.5" />
1316
</ItemGroup>
1417

1518
<ItemGroup>
Lines changed: 66 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,69 @@
1-
namespace Microsoft.Extensions.Configuration.AzureAppConfiguration.Examples.ConsoleApplication
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT license.
3+
//
4+
using Azure.Identity;
5+
using Microsoft.Extensions.Configuration;
6+
using Microsoft.Extensions.Configuration.AzureAppConfiguration;
7+
8+
// Setup configuration builder
9+
var builder = new ConfigurationBuilder();
10+
11+
// Load base configuration from appsettings.json and environment variables
12+
builder.AddJsonFile("appsettings.json")
13+
.AddEnvironmentVariables();
14+
15+
// Build initial configuration
16+
var initialConfig = builder.Build();
17+
18+
// Check for App Configuration endpoint
19+
string? endpoint = initialConfig["AppConfigurationEndpoint"];
20+
if (string.IsNullOrEmpty(endpoint))
221
{
3-
using Microsoft.Extensions.Configuration;
4-
using Microsoft.Extensions.Configuration.AzureAppConfiguration;
5-
using System;
6-
using System.Text;
7-
using System.Threading;
8-
using System.Threading.Tasks;
9-
10-
class Program
11-
{
12-
static IConfiguration Configuration { get; set; }
13-
static IConfigurationRefresher _refresher;
14-
15-
static void Main(string[] args)
16-
{
17-
Configure();
18-
19-
var cts = new CancellationTokenSource();
20-
_ = Run(cts.Token);
21-
22-
// Finish on key press
23-
Console.ReadKey();
24-
cts.Cancel();
25-
}
26-
27-
private static void Configure()
28-
{
29-
var builder = new ConfigurationBuilder();
30-
31-
// Load a subset of the application's configuration from a json file and environment variables
32-
builder.AddJsonFile("appsettings.json")
33-
.AddEnvironmentVariables();
34-
35-
IConfiguration configuration = builder.Build();
36-
37-
if (string.IsNullOrEmpty(configuration["ConnectionString"]))
38-
{
39-
Console.WriteLine("Connection string not found.");
40-
Console.WriteLine("Please set the 'ConnectionString' environment variable to a valid Azure App Configuration connection string and re-run this example.");
41-
return;
42-
}
43-
44-
// Augment the configuration builder with Azure App Configuration
45-
// Pull the connection string from an environment variable
46-
builder.AddAzureAppConfiguration(options =>
47-
{
48-
options.Connect(configuration["ConnectionString"])
49-
.Select("*")
50-
.ConfigureRefresh(refresh =>
51-
{
52-
refresh.Register("AppName")
53-
.Register("Language", refreshAll: true)
54-
.SetCacheExpiration(TimeSpan.FromSeconds(10));
55-
});
56-
57-
// Get an instance of the refresher that can be used to refresh data
58-
_refresher = options.GetRefresher();
59-
});
60-
61-
Configuration = builder.Build();
62-
}
63-
64-
private static async Task Run(CancellationToken token)
65-
{
66-
string display = string.Empty;
67-
StringBuilder sb = new StringBuilder();
68-
69-
do
70-
{
71-
sb.Clear();
72-
73-
// Trigger and wait for an async refresh for registered configuration settings
74-
await _refresher.TryRefreshAsync();
75-
76-
sb.AppendLine($"{Configuration["AppName"]} has been configured to run in {Configuration["Language"]}");
77-
sb.AppendLine();
78-
79-
sb.AppendLine(string.Equals(Configuration["Language"], "spanish", StringComparison.OrdinalIgnoreCase) ? "Buenos Dias." : "Good morning");
80-
sb.AppendLine();
81-
82-
sb.AppendLine("Press any key to exit...");
83-
84-
display = sb.ToString();
22+
Console.WriteLine("App Configuration endpoint not found.");
23+
Console.WriteLine("Please set the 'AppConfigurationEndpoint' environment variable to a valid Azure App Configuration endpoint URL and re-run this example.");
24+
return 1;
25+
}
8526

86-
Console.Clear();
87-
Console.Write(display);
88-
89-
await Task.Delay(1000);
90-
} while (!token.IsCancellationRequested);
91-
}
92-
}
27+
// Connect to Azure App Configuration
28+
IConfigurationRefresher refresher = null!;
29+
builder.AddAzureAppConfiguration(options =>
30+
{
31+
// Use DefaultAzureCredential for Microsoft Entra ID authentication
32+
options.Connect(new Uri(endpoint), new DefaultAzureCredential())
33+
// Load all keys that start with "Settings:" and have no label.
34+
.Select("Settings:*")
35+
.TrimKeyPrefix("Settings:")
36+
.ConfigureRefresh(refreshOptions =>
37+
{
38+
// Reload configuration if any selected key-values have changed.
39+
// Use the default refresh interval of 30 seconds. It can be overridden via refreshOptions.SetRefreshInterval.
40+
refreshOptions.RegisterAll();
41+
});
42+
43+
// Get an instance of the refresher that can be used to refresh data
44+
refresher = options.GetRefresher();
45+
});
46+
47+
// Build the final configuration
48+
IConfiguration configuration = builder.Build();
49+
50+
// Application main loop
51+
while (true)
52+
{
53+
// Trigger and wait for an async refresh for configuration settings
54+
await refresher.TryRefreshAsync();
55+
56+
Console.Clear();
57+
58+
Console.WriteLine($"{configuration["AppName"]} has been configured to run in {configuration["Language"]}");
59+
Console.WriteLine();
60+
61+
Console.WriteLine(string.Equals(configuration["Language"], "Spanish", StringComparison.OrdinalIgnoreCase)
62+
? "Buenos Dias."
63+
: "Good morning");
64+
Console.WriteLine();
65+
66+
Console.WriteLine("Press CTRL+C to exit...");
67+
68+
await Task.Delay(10000);
9369
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
22
"AppName": "Azure App Configuration Console App",
3-
"Language": "English"
3+
"Language": "English",
4+
"AppConfigEndpoint": ""
45
}

0 commit comments

Comments
 (0)