我在IIS中部署了.NET核心mvc应用程序,当我运行app时,页面显示502.5错误,我在powershell中运行命令" dotnet D:\ deploy \ WebApp \ WebApp.dll" ,以下显示详细错误内容:
注意: .net核心版本2.0.0
Unhandled Exception: System.ArgumentNullException: Value cannot be null. Parameter name: implementationInstance at Microsoft.Extensions.DependencyInjection.ServiceCollectionServiceExtensions .AddSingleton[TService](IServiceCollection services, TService implementationInstance)
我知道错误是怎么发生的,如何实例化?
public class Startup
{ ...
public void ConfigureServices(IServiceCollection services)
{
...
services.AddSingleton(CreateQuery()); // this is error location
...
}
IQuery CreateQuery()
{
IQuery query = null;
var dataBase = Configuration.GetSection("AppSettings")["DataBase"];
var defaultConnection = Configuration.GetSection("ConnectionStrings")["SqlServer"];
switch (dataBase)
{
case "sqlserver":
query = new WebApp.Query.Query(defaultConnection);
break;
}
return query;
}
}
答案 0 :(得分:0)
万一有人来寻找可能的解决方案,我可以通过使用以下IServiceProvider
提供的.AddSingleton
回调获取配置来解决.NET Core 3.1项目中的此问题。方式:
public class Startup
{ ...
public void ConfigureServices(IServiceCollection services)
{
...
// Pass the service provider that AddSingleton provides
services.AddSingleton((serviceProvider) => {
CreateQuery(serviceProvider)
});
...
}
IQuery CreateQuery(IServiceProvider serviceProvider)
{
IQuery query = null;
// Use the service provider to get the configuration
var configuration = serviceProvider.GetRequiredService<IConfiguration>();
var dataBase = configuration.GetSection("AppSettings")["DataBase"];
var defaultConnection = configuration.GetSection("ConnectionStrings")["SqlServer"];
switch (dataBase)
{
case "sqlserver":
query = new WebApp.Query.Query(defaultConnection);
break;
}
return query;
}
}