错误 CS1503 - 无法从 Microsoft.Extensions.Configuration.IConfigurationSection 转换为 System.Action<>
Error CS1503 - Cannot convert from Microsoft.Extensions.Configuration.IConfigurationSection to System.Action<>
我收到一个错误:
错误 CS1503 参数 2:无法从 'Microsoft.Extensions.Configuration.IConfigurationSection' 转换为 'System.Action<>
当我将 Configure 与 Bind() 一起使用时,它正在运行。
var bandConfig = new BandSettings();
Configuration.GetSection("BandSettings").Bind(bandConfig );
但是使用下面的代码,我得到了上面的错误。
我尝试了博客和其他论坛中建议的许多解决方案,
例如 -
但还是一样的错误。我错过了什么吗??
我准备好了以下东西:
appsettings.json
"BandSettings":{
"UserID": "aTestUserID",
"Password" : "aTestPassword"
}
BandSettings.cs
public class BandWidthSettings
{
public string UserID { get; set; }
public string ApiToken { get; set; }
}
TestHelper.cs
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
public static IConfiguration GetTestConfiguration()
=> new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
public IConfiguration Configuration { get; }
public TestHelper(IConfiguration configuration)
{
Configuration = configuration;
}
public void ConfigureServices(IServiceCollection services)
{
var config = new BandSettings();
var bandSettingsSection = Configuration.GetSection("BandSettings");
services.Configure<BandSettings>(bandSettingsSection); //Error is getting on this line - bandSettingsSection
}
public IConfiguration Configuration { get; }
public TestHelper(IConfiguration configuration)
{
Configuration = configuration;
}
public void ConfigureServices(IServiceCollection services)
{
//Configure section binding
services.Configure<BandSettings>(Configuration.GetSection(BandSettings.SECTION));
}
为设置创建 POCO
public class MySetting
{
public string Setting{ get; set; }
}
在启动时它完美地工作,因为它应该是:
services.Configure<MySetting>(Configuration.GetSection("MySetting"));
但是在我的依赖注入中添加问题评论中提到的绑定。
private static void ConfigureServices(IServiceCollection services)
{
var mySection = configuration.GetSection("MySetting");
services.Configure<MySetting>(c => mySection .Bind(c));
}
添加NuGet包Microsoft.Extensions.Options.ConfigurationExtensions获取扩展方法where
services.Configure<BandSettings>(Configuration.GetSection("BandSettings"));
会起作用。
我收到一个错误:
错误 CS1503 参数 2:无法从 'Microsoft.Extensions.Configuration.IConfigurationSection' 转换为 'System.Action<>
当我将 Configure 与 Bind() 一起使用时,它正在运行。
var bandConfig = new BandSettings();
Configuration.GetSection("BandSettings").Bind(bandConfig );
但是使用下面的代码,我得到了上面的错误。
我尝试了博客和其他论坛中建议的许多解决方案,
例如 -
但还是一样的错误。我错过了什么吗??
我准备好了以下东西: appsettings.json
"BandSettings":{
"UserID": "aTestUserID",
"Password" : "aTestPassword"
}
BandSettings.cs
public class BandWidthSettings
{
public string UserID { get; set; }
public string ApiToken { get; set; }
}
TestHelper.cs
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
public static IConfiguration GetTestConfiguration()
=> new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
public IConfiguration Configuration { get; }
public TestHelper(IConfiguration configuration)
{
Configuration = configuration;
}
public void ConfigureServices(IServiceCollection services)
{
var config = new BandSettings();
var bandSettingsSection = Configuration.GetSection("BandSettings");
services.Configure<BandSettings>(bandSettingsSection); //Error is getting on this line - bandSettingsSection
}
public IConfiguration Configuration { get; }
public TestHelper(IConfiguration configuration)
{
Configuration = configuration;
}
public void ConfigureServices(IServiceCollection services)
{
//Configure section binding
services.Configure<BandSettings>(Configuration.GetSection(BandSettings.SECTION));
}
为设置创建 POCO
public class MySetting
{
public string Setting{ get; set; }
}
在启动时它完美地工作,因为它应该是:
services.Configure<MySetting>(Configuration.GetSection("MySetting"));
但是在我的依赖注入中添加问题评论中提到的绑定。
private static void ConfigureServices(IServiceCollection services)
{
var mySection = configuration.GetSection("MySetting");
services.Configure<MySetting>(c => mySection .Bind(c));
}
添加NuGet包Microsoft.Extensions.Options.ConfigurationExtensions获取扩展方法where
services.Configure<BandSettings>(Configuration.GetSection("BandSettings"));
会起作用。