52

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
}
born2Learn
  • 1,253
  • 5
  • 14
  • 25

3 Answers3

159

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

Benrobot
  • 2,630
  • 1
  • 21
  • 24
5

Create a POCO for the setting

public class MySetting
{
  public string Setting{ get; set; }
}

On Startup it works perfectly as it should be:

services.Configure<MySetting>(Configuration.GetSection("MySetting"));

However on my the dependecy injection add the bind as mentioned in the question comments.

private static void ConfigureServices(IServiceCollection services)
{
  var mySection = configuration.GetSection("MySetting");
  services.Configure<MySetting>(c => mySection .Bind(c));
}
Gabriel Molter
  • 333
  • 2
  • 10
0
 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));
        }
born2Learn
  • 1,253
  • 5
  • 14
  • 25
  • 1
    It's needed add Microsoft.Extensions.Options.ConfigurationExtensions from NuGet. You can make it easier writing down OptionsConfigurationServiceCollectionExtensions in your code and wait for VS suggest you. – Jener Garcia Menezes Aug 25 '23 at 15:45