30

I have .netcore 3.1 application and I want to update it to .net 5.0
I have the following code

public static IAppSettings ConfigureAppSettings(this IServiceCollection services, IConfiguration configuration)
{
    void ConfigureSection<Interface, Implementation>(string sectionName)
        where Implementation : Interface, new() where Interface : class
    {
        Implementation configSection = new Implementation();
        configuration.GetSection(sectionName).Bind(configSection);
        services.AddSingleton<Interface>(configSection);
    }
}

it was working previously, but after updating to .NET 5 I start seeing this compile-time error

CS1061 'IConfigurationSection' does not contain a definition for 'Bind' and no accessible extension method 'Bind' accepting a first argument of type 'IConfigurationSection' could be found (are you missing a using directive or an assembly reference?)

  1. Is the Bind method been removed?
  2. There is no mention for the solution to this problem in the official documentation for the migration

My question: What is the alternative to the Bind method?

Hakan Fıstık
  • 16,800
  • 14
  • 110
  • 131
  • Are you sure you need one? .NET 5 is .NET *Core* 5. `Bind` is an extension method. You're probably missing a namespace or package – Panagiotis Kanavos Nov 13 '20 at 09:54
  • [Bind is in the same namespace and package it was in 3.1](https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.configuration.configurationbinder?view=dotnet-plat-ext-5.0). Can you post an actual example that reproduces this problem? What packages were used, what `using` statements? – Panagiotis Kanavos Nov 13 '20 at 09:58
  • @PanagiotisKanavos thanks sir, problem solved – Hakan Fıstık Nov 13 '20 at 10:06

2 Answers2

65

I installed this Microsoft.Extensions.Configuration.Binder package and the problem solved.

The strange thing is that when I was using .net-core3.1 I did not need to install it from Nuget but after updating to .net5 I needed to install this package separately.

enter image description here

Hakan Fıstık
  • 16,800
  • 14
  • 110
  • 131
  • 2
    If you created a new project from scratch the proper package references would be included. That's a great way to find out what's wrong - create a new minimal project and check for differences. – Panagiotis Kanavos Nov 13 '20 at 10:08
  • 5
    Reason it works in 3.1, is because Package Microsoft.Extensions.Configuration --version 3.1.11 indirectly takes a dependency on Microsoft.Extensions.Configuration.Binder. Hence following command dotnet add package Microsoft.Extensions.Configuration --version 3.1.11 will automatically install Microsoft.Extensions.Configuration.Binder 3.1.11. – Surender Singh Malik Jan 19 '21 at 12:12
  • @SurenderSinghMalik and this was changed in .NET 5 ? please provide the source, so I will add your comment to the answer, so everyone could benfinit from the information after I will check it. – Hakan Fıstık Jan 19 '21 at 18:00
  • @HakanFıstık yes it has very clearly visible in my experiment. Will share the concrete resource/link if i get anything !! – Surender Singh Malik Jan 21 '21 at 14:57
  • 1
    Worked for me. I moved my class to another project within the solution and Bind lost definition. I didn't think to install Microsoft.Extensions.Configuration.Binder since I didn't see that installed on the original project. +1 – Yogi Nov 08 '21 at 21:47
1

With dotnet 6 worked when I installed Microsoft.Extensions.Options.ConfigurationExtensions

NuGet\Install-Package Microsoft.Extensions.Options.ConfigurationExtensions -Version 6.0.0
George Wurthmann
  • 411
  • 2
  • 8
  • 20
  • `Microsoft.Extensions.Options.ConfigurationExtensions` is taking a dependency on `Microsoft.Extensions.Configuration.Binder` [docs](https://www.nuget.org/packages/Microsoft.Extensions.Options.ConfigurationExtensions/#dependencies-body-tab).This is the reason that this library is also solving the problem, but not everyone needs this library to solve the problem. This is not the minimum solution. – Hakan Fıstık Mar 18 '23 at 12:42