I have a dotnet 8 asp core application and I'm trying to add a HealthCheck for my Azure Database using AspNetCore.HealthChecks.MySql nuget and retreiving connection string from Azure App Configuration but i'm getting this error
Unhandled exception. System.ArgumentNullException: Value cannot be null. (Parameter 'connectionString') 2025-02-13T17:25:58.240518977Z at Guard.ThrowIfNull[T](T argument, Boolean throwOnEmptyString, String paramName) in /home/runner/work/AspNetCore.Diagnostics.HealthChecks/AspNetCore.Diagnostics.HealthChecks/src/CallerArgumentExpressionAttribute.cs:line 49 2025-02-13T17:25:58.240534540Z at Microsoft.Extensions.DependencyInjection.MySqlHealthCheckBuilderExtensions.AddMySql(IHealthChecksBuilder builder, String connectionString, String healthQuery, Action`1 configure, String name, Nullable`1 failureStatus, IEnumerable`1 tags, Nullable`1 timeout) in /home/runner/work/AspNetCore.Diagnostics.HealthChecks/AspNetCore.Diagnostics.HealthChecks/src/HealthChecks.MySql/DependencyInjection/MySqlHealthCheckBuilderExtensions.cs:line 76 2025-02-13T17:25:58.240539703Z at Program.<Main>$(String[] args) in /home/vsts/work/1/s/src/ProjectName/Program.cs:line 90
This is my Program.cs code :
IConfigurationSection configurationConnectionStringsSection = builder.Configuration.GetSection(ConnectionStringsSettings.Section);
builder.Services.Configure<ConnectionStringsSettings>(configurationConnectionStringsSection);
ConnectionStringsSettings? connectionStringsOptions = configurationConnectionStringsSection.Get<ConnectionStringsSettings>()
?? throw new InvalidOperationException($"Invalid settings for {nameof(ConnectionStringsSettings)}");
if (!builder.Environment.IsDevelopment())
{
// Bind Settings from Azure App Configuration
builder.Services.Configure<ConnectionStringsSettings>
(builder.Configuration.GetSection("ConnectionStrings"));
builder.Services.AddAzureAppConfiguration();
builder.Configuration.AddAzureAppConfiguration(options =>
{
options.Connect(new Uri(azureAppConfigurationOptions.Uri), new
DefaultAzureCredential(defaultAzureCredentialOptions));
options.ConfigureKeyVault(kv =>
{
kv.SetCredential(new DefaultAzureCredential(defaultAzureCredentialOptions));
});
});
builder.Services
.AddHealthChecks()
.AddApplicationStatus("Self")
.AddMySql(connectionStringsOptions.MyConnectionString);
}
var app = builder.Build();
When executing this code i get ArgumentNullException because connectionStringsOptions.MyConnectionString does not have value yet, but if i comment healthcheck and try to get connection string after building application i can retrieve it from the cloud. Any tips to avoid this kind of error when starting application please ?
ConnectionStringsSettings? connectionStringsOptions = configurationConnectionStringsSection.Get<ConnectionStringsSettings>()
?? throw new InvalidOperationException($"Invalid settings for {nameof(ConnectionStringsSettings)}");
This code is in the wrong place as you try to read the configuration before it it is binding Azure settings which means it didn't apply. Move this code just before you call health check i.e. as below so put it after adding Azure configuration done.