System.ArgumentNullException:值不能为null,参数名称:ImplementationInstance [英] System.ArgumentNullException: Value cannot be null, Parameter name: implementationInstance
问题描述
我在IIS中部署了.NET核心mvc应用程序,当我运行应用程序时,页面显示502.5错误,我在powershell"dotnet D:\ deploy \ WebApp \ WebApp.dll"中运行命令,以下显示详细的错误内容:
I deployed .NET core mvc application in IIS, when I run app, the page show 502.5 error, I run command in powershell "dotnet D:\deploy\WebApp\WebApp.dll" ,this follow show detail error content:
注意: .net核心版本2.0.0
NOTE:.net core version 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)
我知道错误是如何发生的,如何实例化?
I know how the error occurred, how to instantiate?
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;
}
}
推荐答案
如果有人来寻找可能的解决方案,我可以通过使用
In case someone comes along looking for a possible solution, I was able to resolve this issue in a .NET Core 3.1 project by getting the configuration using the IServiceProvider
callback provided by .AddSingleton
in the following way:
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;
}
}
这篇关于System.ArgumentNullException:值不能为null,参数名称:ImplementationInstance的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!