From 5ead4d685cd3aca25c183715ac2f080158a9c455 Mon Sep 17 00:00:00 2001 From: Zhenlan Wang Date: Wed, 21 May 2025 11:25:24 -0700 Subject: [PATCH 1/3] Upgrade the .NET console app sample to .NET 8 --- .../ConsoleApplication.csproj | 11 +- .../DotNetCore/ConsoleApplication/Program.cs | 147 +++++++----------- .../ConsoleApplication/appsettings.json | 3 +- 3 files changed, 69 insertions(+), 92 deletions(-) diff --git a/examples/DotNetCore/ConsoleApplication/ConsoleApplication.csproj b/examples/DotNetCore/ConsoleApplication/ConsoleApplication.csproj index a1de1514..58eb4e18 100644 --- a/examples/DotNetCore/ConsoleApplication/ConsoleApplication.csproj +++ b/examples/DotNetCore/ConsoleApplication/ConsoleApplication.csproj @@ -3,13 +3,16 @@ false Exe - netcoreapp3.1 + net8.0 + enable + enable - - - + + + + diff --git a/examples/DotNetCore/ConsoleApplication/Program.cs b/examples/DotNetCore/ConsoleApplication/Program.cs index 08a6f6e9..710bb1d0 100644 --- a/examples/DotNetCore/ConsoleApplication/Program.cs +++ b/examples/DotNetCore/ConsoleApplication/Program.cs @@ -1,93 +1,66 @@ -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["ConnectionString"])) - { - Console.WriteLine("Connection string not found."); - Console.WriteLine("Please set the 'ConnectionString' 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["ConnectionString"]) - .Select("*") - .ConfigureRefresh(refresh => - { - refresh.Register("AppName") - .Register("Language", refreshAll: true) - .SetCacheExpiration(TimeSpan.FromSeconds(10)); - }); +using Azure.Identity; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Configuration.AzureAppConfiguration; - // Get an instance of the refresher that can be used to refresh data - _refresher = options.GetRefresher(); - }); +// Setup configuration builder +var builder = new ConfigurationBuilder(); - Configuration = builder.Build(); - } +// Load base configuration from appsettings.json and environment variables +builder.AddJsonFile("appsettings.json") + .AddEnvironmentVariables(); - private static async Task Run(CancellationToken token) - { - string display = string.Empty; - StringBuilder sb = new StringBuilder(); +// Build initial configuration +var initialConfig = builder.Build(); - do - { - sb.Clear(); - - // Trigger and wait for an async refresh for registered configuration settings - await _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..."); - - display = sb.ToString(); +// Check for App Configuration endpoint +string? endpoint = initialConfig["AppConfigEndpoint"]; +if (string.IsNullOrEmpty(endpoint)) +{ + Console.WriteLine("App Configuration endpoint not found."); + Console.WriteLine("Please set the 'AppConfigEndpoint' environment variable to a valid Azure App Configuration endpoint URL and re-run this example."); + return 1; +} - Console.Clear(); - Console.Write(display); - - await Task.Delay(1000); - } while (!token.IsCancellationRequested); - } - } +// 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. + refreshOptions.RegisterAll() + .SetRefreshInterval(TimeSpan.FromSeconds(10)); + }); + + // 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); } diff --git a/examples/DotNetCore/ConsoleApplication/appsettings.json b/examples/DotNetCore/ConsoleApplication/appsettings.json index c352c976..1f815369 100644 --- a/examples/DotNetCore/ConsoleApplication/appsettings.json +++ b/examples/DotNetCore/ConsoleApplication/appsettings.json @@ -1,4 +1,5 @@ { "AppName": "Azure App Configuration Console App", - "Language": "English" + "Language": "English", + "AppConfigEndpoint": "" } From 2c21097c388f26108788304affa3f2c563d35170 Mon Sep 17 00:00:00 2001 From: Zhenlan Wang Date: Wed, 21 May 2025 17:18:42 -0700 Subject: [PATCH 2/3] Address feedback --- examples/DotNetCore/ConsoleApplication/Program.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/DotNetCore/ConsoleApplication/Program.cs b/examples/DotNetCore/ConsoleApplication/Program.cs index 710bb1d0..0f7a4e5b 100644 --- a/examples/DotNetCore/ConsoleApplication/Program.cs +++ b/examples/DotNetCore/ConsoleApplication/Program.cs @@ -13,11 +13,11 @@ var initialConfig = builder.Build(); // Check for App Configuration endpoint -string? endpoint = initialConfig["AppConfigEndpoint"]; +string? endpoint = initialConfig["AppConfigurationEndpoint"]; if (string.IsNullOrEmpty(endpoint)) { Console.WriteLine("App Configuration endpoint not found."); - Console.WriteLine("Please set the 'AppConfigEndpoint' environment variable to a valid Azure App Configuration endpoint URL and re-run this example."); + Console.WriteLine("Please set the 'AppConfigurationEndpoint' environment variable to a valid Azure App Configuration endpoint URL and re-run this example."); return 1; } @@ -33,8 +33,8 @@ .ConfigureRefresh(refreshOptions => { // Reload configuration if any selected key-values have changed. - refreshOptions.RegisterAll() - .SetRefreshInterval(TimeSpan.FromSeconds(10)); + // 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 From 0c1422914450da5a3471716c858fc75504f75e86 Mon Sep 17 00:00:00 2001 From: Zhenlan Wang Date: Wed, 21 May 2025 17:23:56 -0700 Subject: [PATCH 3/3] Add copyright header --- examples/DotNetCore/ConsoleApplication/Program.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/examples/DotNetCore/ConsoleApplication/Program.cs b/examples/DotNetCore/ConsoleApplication/Program.cs index 0f7a4e5b..f1914c0f 100644 --- a/examples/DotNetCore/ConsoleApplication/Program.cs +++ b/examples/DotNetCore/ConsoleApplication/Program.cs @@ -1,4 +1,7 @@ -using Azure.Identity; +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT license. +// +using Azure.Identity; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration.AzureAppConfiguration;