admin管理员组文章数量:1415111
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 ?
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 ?
Share Improve this question edited Feb 13 at 19:23 Terai asked Feb 13 at 18:20 TeraiTerai 3211 gold badge4 silver badges14 bronze badges1 Answer
Reset to default 1ConnectionStringsSettings? 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.
本文标签: cArgumentNullException in MySql HealthCheck before building AspNet Core applicationStack Overflow
版权声明:本文标题:c# - ArgumentNullException in MySql HealthCheck before building Asp.Net Core application - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745196267a2647149.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论