I have a strongly typed POCO class that is bound to Configuration Settings being retrieved from Azure App Configuration Services.
As an example, the strongly typed class is:
public class AppConfigValues
{
public string? AllowedHosts { get; set; }
}
In Azure App Configuration Services (it could come from other configuration providers like environment variables) the key is: AppName:AllowedHosts.
So what do I need to do to ensure that all the services that needs AllowedHosts are able to access it in the Builder Configuration Services just like it does when it is in the appsettings.json file?
The same can be applied to the Logging:LogLevel keys
I am using .Net 8.
Bind the AppName:AllowedHosts
section in configuration to the AppConfigValues
object:
// Connect to Azure App Configuration
builder.Configuration.AddAzureAppConfiguration(options =>
{
options.Connect(builder.Configuration["AzureAppConfigurationConnectionString"])
.Select("AppName:*")
.Select("Logging:*");
});
// Register the strongly typed configuration
builder.Services.Configure<AppConfigValues>(builder.Configuration.GetSection("AppName"));
You can inject the configuration into your services using the IOptions<AppConfigValues>
interface. For example:
public class SomeService
{
private readonly AppConfigValues _configValues;
public SomeService(IOptions<AppConfigValues> configValues)
{
_configValues = configValues.Value;
}
public void DoSomething()
{
Console.WriteLine($"Allowed Hosts: {_configValues.AllowedHosts}");
}
}
Reference: Create an ASP.NET Core app with Azure App Configuration
1