我想将以下对象与ServiceCollection中的appsettings.json数据绑定,但是我无法更改类或接口的设计:
public class TransferOptions :ITransferOptions
{
public IConnectionInfo Source {get;set;}
public IConnectionInfo Destination {get;set;}
}
public class ConnectionInfo : IConnectionInfo
{
public string UserName{get;set;}
public string Password{get;set;}
}
public interface ITransferOptions
{
IConnectionInfo Source {get;set;}
IConnectionInfo Destination {get;set;}
}
public interface IConnectionInfo
{
string UserName{get;set;}
string Password{get;set;}
}
这是我在appsettings.json中的数据
{
"TransferOptions":{
"Source ":{
"UserName":"USERNAME",
"Password":"PASSWORD"
},
"Destination":{
"UserName":"USERNAME",
"Password":"PASSWORD"
}
}
}
这是我对服务提供商的配置:
var provider=new ServiceCollection()
.Configure<TransferOptions>(options => _configuration.GetSection("TransferOptions").Bind(options))
.BuildServiceProvider();
这是我得到错误的部分
无法创建“IConnectionInfo”类型的实例,因为它要么是抽象的,要么是接口:
var transferOptions =_serviceProvider.GetService<IOptions<TransferOptions>>()