英文:
Unhandled exception. System.ArgumentNullException: Value cannot be null. (Parameter 'connectionString') after Upgrading .NET 5 to .NET 7
问题
I have upgraded my API to be working with .NET 7 and Ubuntu 23.04, everything went fine, the API and database are connected on my local machine and I can send request, but when I publish and deploy the app to my server, it seems that it doesn't read the connectionString
in appsetting.json
.
Here is my appsettings.json
{
"AppSettings": {
"Secret": "*************",
"EmailFrom": "********",
"SmtpHost": "v******",
"SmtpPort": 587,
"SmtpUser": "a*****",
"SmtpPass": "HJu******"
},
"ConnectionStrings": {
"WebApiDatabase": "Data Source=WebApiDatabase.db",
"HangfireConnection": "Server=**.**.***.***;Port=5432;Database=Min;User Id=postgres;Password=min***;" //server
//"HangfireConnection": "Host=localhost;Port=5432;Database=Min;User Id=postgres;Password=*****;" // local
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information",
"Hangfire": "Information"
}
},
"AllowedHosts": "*",
"postgresqlconnection": {
"connectionString": "Server=**.**.***.***;Port=5432;Database=Min;User Id=postgres;Password=min***;" //server
//"connectionString": "Host=localhost;Port=5432;Database=Min;User Id=postgres;Password=*****;" // local
},
"mongoconnection": {
"connectionString": "mongodb://localhost:27017/?readPreference=primary&appname=MongoDB%20Compass%20Community&ssl=false",
"database": "backlog",
"collection": "rawData",
"collectionpMedian": "resultPmedian",
"collectionClustering": "resultClustering"
},
"urlSettings": {
"staticPageUrl": "http://**.**.***.***",
"backendUrl": "http://**.**.***.***"
},
"variousSettings": {
"defaultMessage": "A default message if nothing is found in the database."
}
}
My startup.cs
services.AddHangfire(configuration => configuration
.SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
.UseSimpleAssemblyNameTypeSerializer()
.UseRecommendedSerializerSettings()
.UsePostgreSqlStorage(Configuration.GetConnectionString("HangfireConnection")));
My DataContext.cs
private readonly IConfiguration Configuration;
public DataContext(IConfiguration configuration)
{
Configuration = configuration;
}
protected override void OnConfiguring(DbContextOptionsBuilder options)
{
// connect to postgress database
var connectionString = Configuration["postgresqlconnection:connectionString"];
options.UseNpgsql(connectionString);
}
The error
root@ubuntu-4gb:/# /usr/bin/dotnet /var/www/MinAPI/MinWebApi.dll
Unhandled exception. System.ArgumentNullException: Value cannot be null. (Parameter 'connectionString')
at Hangfire.PostgreSql.PostgreSqlStorage..ctor(String connectionString, Action`1 connectionSetup, PostgreSqlStorageOptions options)
at Hangfire.PostgreSql.PostgreSqlBootstrapperConfigurationExtensions.UsePostgreSqlStorage(IGlobalConfiguration configuration, String connectionString, Action`1 connectionSetup, PostgreSqlStorageOptions options)
at Hangfire.PostgreSql.PostgreSqlBootstrapperConfigurationExtensions.UsePostgreSqlStorage(IGlobalConfiguration configuration, String connectionString)
at WebApi.Startup.<ConfigureServices>b__4_2(IGlobalConfiguration configuration) in D:\C Sharp\Startup.cs:line 46
at Hangfire.HangfireServiceCollectionExtensions.<>c__DisplayClass0_0.<AddHangfire>b__0(IServiceProvider provider, IGlobalConfiguration config)
at Hangfire.HangfireServiceCollectionExtensions.<>c__DisplayClass1_0.<AddHangfire>b__14(IServiceProvider serviceProvider)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite factoryCallSite, RuntimeResolverContext context)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitRootCache(ServiceCallSite callSite, RuntimeResolverContext context)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope)
at Microsoft.Extensions.DependencyInjection.ServiceProvider.CreateServiceAccessor(Type serviceType)
at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetService[T](IServiceProvider provider)
at Hangfire.HangfireServiceCollectionExtensions.ThrowIfNotConfigured(IServiceProvider serviceProvider)
at Hangfire.HangfireServiceCollectionExtensions.CreateBackgroundJobServerHostedService(IServiceProvider provider, JobStorage storage, IEnumerable`1 additionalProcesses, BackgroundJobServerOptions options)
at Hangfire.HangfireServiceCollectionExtensions.<>c__DisplayClass9_0.<AddHangfireServerInner>b__0(IServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite factoryCallSite, RuntimeResolverContext context)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitDisposeCache(ServiceCallSite transientCallSite, RuntimeResolverContext context)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitIEnumerable(IEnumerableCallSite enumerableCallSite, RuntimeResolverContext context)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitNoCache(ServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.DynamicServiceProviderEngine.<>c__DisplayClass2_0.<RealizeService>b__0(ServiceProviderEngineScope scope)
at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
at Microsoft.Extensions.Hosting.Internal.Host.StartAsync(CancellationToken cancellationToken)
at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token)
at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token)
at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.Run(IHost host)
at WebApi.Program.Main(String[] args) in D:\C Sharp\Program.cs:line 12
I have tried to hardcode the string inside the functions but the same error happens, tried to
英文:
I have upgraded my API to be working with .NET 7 and Ubuntu 23.04, everything went fine, the API and database are connected on my local machine and I can send request, but when I publish and deploy the app to my server, it seems that it doesn't read the connectionString
in appsetting.json
.
Here is my appsettings.json
{
"AppSettings": {
"Secret": "*************",
"EmailFrom": "********",
"SmtpHost": "v******",
"SmtpPort": 587,
"SmtpUser": "a*****",
"SmtpPass": "HJu******"
},
"ConnectionStrings": {
"WebApiDatabase": "Data Source=WebApiDatabase.db",
"HangfireConnection": "Server=**.**.***.***;Port=5432;Database=Min;User Id=postgres;Password=min***;" //server
//"HangfireConnection": "Host=localhost;Port=5432;Database=Min;User Id=postgres;Password=*****;" // local
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information",
"Hangfire": "Information"
}
},
"AllowedHosts": "*",
"postgresqlconnection": {
"connectionString": "Server=**.**.***.***;Port=5432;Database=Min;User Id=postgres;Password=min***;" //server
//"connectionString": "Host=localhost;Port=5432;Database=Min;User Id=postgres;Password=*****;" // local
},
"mongoconnection": {
"connectionString": "mongodb://localhost:27017/?readPreference=primary&appname=MongoDB%20Compass%20Community&ssl=false",
"database": "backlog",
"collection": "rawData",
"collectionpMedian": "resultPmedian",
"collectionClustering": "resultClustering"
},
"urlSettings": {
"staticPageUrl": "http://**.**.***.***",
"backendUrl": "http://**.**.***.***"
},
"variousSettings": {
"defaultMessage": "A default message if nothing is found in the database."
}
}
My startup.cs
services.AddHangfire(configuration => configuration
.SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
.UseSimpleAssemblyNameTypeSerializer()
.UseRecommendedSerializerSettings()
.UsePostgreSqlStorage(Configuration.GetConnectionString("HangfireConnection")));
My DataContext.cs
private readonly IConfiguration Configuration;
public DataContext(IConfiguration configuration)
{
Configuration = configuration;
}
protected override void OnConfiguring(DbContextOptionsBuilder options)
{
// connect to postgress database
var connectionString = Configuration["postgresqlconnection:connectionString"];
options.UseNpgsql(connectionString);
}
The error
root@ubuntu-4gb:/# /usr/bin/dotnet /var/www/MinAPI/MinWebApi.dll
Unhandled exception. System.ArgumentNullException: Value cannot be null. (Parameter 'connectionString')
at Hangfire.PostgreSql.PostgreSqlStorage..ctor(String connectionString, Action`1 connectionSetup, PostgreSqlStorageOptions options)
at Hangfire.PostgreSql.PostgreSqlBootstrapperConfigurationExtensions.UsePostgreSqlStorage(IGlobalConfiguration configuration, String connectionString, Action`1 connectionSetup, PostgreSqlStorageOptions options)
at Hangfire.PostgreSql.PostgreSqlBootstrapperConfigurationExtensions.UsePostgreSqlStorage(IGlobalConfiguration configuration, String connectionString)
at WebApi.Startup.<ConfigureServices>b__4_2(IGlobalConfiguration configuration) in D:\C Sharp\Startup.cs:line 46
at Hangfire.HangfireServiceCollectionExtensions.<>c__DisplayClass0_0.<AddHangfire>b__0(IServiceProvider provider, IGlobalConfiguration config)
at Hangfire.HangfireServiceCollectionExtensions.<>c__DisplayClass1_0.<AddHangfire>b__14(IServiceProvider serviceProvider)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite factoryCallSite, RuntimeResolverContext context)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitRootCache(ServiceCallSite callSite, RuntimeResolverContext context)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope)
at Microsoft.Extensions.DependencyInjection.ServiceProvider.CreateServiceAccessor(Type serviceType)
at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetService[T](IServiceProvider provider)
at Hangfire.HangfireServiceCollectionExtensions.ThrowIfNotConfigured(IServiceProvider serviceProvider)
at Hangfire.HangfireServiceCollectionExtensions.CreateBackgroundJobServerHostedService(IServiceProvider provider, JobStorage storage, IEnumerable`1 additionalProcesses, BackgroundJobServerOptions options)
at Hangfire.HangfireServiceCollectionExtensions.<>c__DisplayClass9_0.<AddHangfireServerInner>b__0(IServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite factoryCallSite, RuntimeResolverContext context)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitDisposeCache(ServiceCallSite transientCallSite, RuntimeResolverContext context)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitIEnumerable(IEnumerableCallSite enumerableCallSite, RuntimeResolverContext context)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitNoCache(ServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.DynamicServiceProviderEngine.<>c__DisplayClass2_0.<RealizeService>b__0(ServiceProviderEngineScope scope)
at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
at Microsoft.Extensions.Hosting.Internal.Host.StartAsync(CancellationToken cancellationToken)
at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token)
at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token)
at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.Run(IHost host)
at WebApi.Program.Main(String[] args) in D:\C Sharp\Program.cs:line 12
I have tried to hardcode the string inside the functions but the same error happens, tried to change variable name the same, tried to update the appsettings.Development.json
to be the same as appsetting.json
, it worked but when I restart the app service the error appears again.
Please guide me.
答案1
得分: 1
请验证您的Appsettings文件是否实际上已经发布?
另外,只有在IDE(如Visual Studio等)本地运行时才会读取Appsettings.Development.Json。否则,它将尝试读取Appsettings.Json或Appsettings.Production.Json。
英文:
Can you please verify that your Appsettings file is actually being published?
Also Appsettings.Development.Json is only read when running locally in an IDE(Visual Studios, ETC). Otherwise it will try and read Appsettings.Json or Appsettings.Production.Json
答案2
得分: 0
错误每次我在服务器上更新API时都会出现,但我发现只要编辑服务器上的appsettings.json
,问题就会解决,但只有在那个时候才有效,而且我每次都需要这样做,不过可以忽略,因为API运行得非常正常。
英文:
The error kept appearing every time I update the API on the server, however I found that editing the appsettings.json
within the server solves the problem for that time only, and I need to do that every time, however, It can be ignored as the API works perfeclty fine.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论