-
Notifications
You must be signed in to change notification settings - Fork 79
Upgrade the .NET console app sample to .NET 8 #1058
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| { | ||
| "AppName": "Azure App Configuration Console App", | ||
| "Language": "English" | ||
| "Language": "English", | ||
| "AppConfigEndpoint": "" | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.