【发布时间】:2018-06-18 23:06:31
【问题描述】:
我在appsettings.json的配置如下:
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Warning"
}
},
"GatewaySettings": {
"DBName": "StorageDb.sqlite",
"DBSize": "100"
}
}
这是代表配置数据的类
public class GatewaySettings
{
public string DBName { get; set; }
public string DBSize { get; set; }
}
配置服务如下:
services.AddSingleton(Configuration.GetSection("GatewaySettings").Get<GatewaySettings>());
但我收到此错误:
值不能为空。参数名称:implementationInstance'
代码:
public class SqlRepository
{
private readonly GatewaySettings _myConfiguration;
public SqlRepository(GatewaySettings settings)
{
_myConfiguration = settings;
}
}
依赖注入代码:
var settings = new IOTGatewaySettings();
builder.Register(c => new SqlRepository(settings))
背景
我将 ASPNET CORE 应用程序作为 Windows 服务托管,.NET Framework 是 4.6.1
注意:此处出现类似问题,但未提供解决方案。System.ArgumentNullException: Value cannot be null, Parameter name: implementationInstance
【问题讨论】:
-
改用
IOptions<T>参考Configuration in ASP.NET Core
标签: c# asp.net-core dependency-injection appsettings