I am getting an error:
Error CS1503 Argument 2: cannot convert from 'Microsoft.Extensions.Configuration.IConfigurationSection' to 'System.Action<>
When I am using Configure with Bind(), it is working.
var bandConfig = new BandSettings();
Configuration.GetSection("BandSettings").Bind(bandConfig );
But with the below code, I am getting the above error.
I tried many solutions suggested in blogs and other forums, For example - Getting value from appsettings.json in .net core
but still the same error. Am I missing anything??
I have below things in place: 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
}
Add NuGet package Microsoft.Extensions.Options.ConfigurationExtensions to get the extension method where
services.Configure<BandSettings>(Configuration.GetSection("BandSettings"));
will work.
Reference: https://github.com/dotnet/AspNetCore.Docs/issues/18833
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With