Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions examples/DotNetCore/ConsoleApplication/ConsoleApplication.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
<PropertyGroup>
<IsPackable>false</IsPackable>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.AzureAppConfiguration" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="3.1.2" />
<PackageReference Include="Azure.Identity" Version="1.14.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.AzureAppConfiguration" Version="8.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="9.0.5" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="9.0.5" />
</ItemGroup>

<ItemGroup>
Expand Down
156 changes: 66 additions & 90 deletions examples/DotNetCore/ConsoleApplication/Program.cs
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);
Comment thread
zhenlan marked this conversation as resolved.
}
3 changes: 2 additions & 1 deletion examples/DotNetCore/ConsoleApplication/appsettings.json
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": ""
}