Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error CS1503 - Cannot convert from Microsoft.Extensions.Configuration.IConfigurationSection to System.Action<>

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
}
like image 655
born2Learn Avatar asked Apr 21 '20 20:04

born2Learn


1 Answers

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

like image 90
Benrobot Avatar answered Sep 17 '22 22:09

Benrobot