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..f1914c0f 100644 --- a/examples/DotNetCore/ConsoleApplication/Program.cs +++ b/examples/DotNetCore/ConsoleApplication/Program.cs @@ -1,93 +1,69 @@ -namespace Microsoft.Extensions.Configuration.AzureAppConfiguration.Examples.ConsoleApplication +// 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)) { - 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)); - }); - - // 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(); - - 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(); + 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; +} - 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. + // 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); } 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": "" }