Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Migrating from .NET Core 3.1 to .NET 5 produce compile-time error

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 .net5 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. Obviously, the Bind method has been removed and the API is no more compatible
  2. There is no mention for the solution of this problem in the offical documentation for the migration

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

like image 486
Hakan Fıstık Avatar asked Nov 13 '20 08:11

Hakan Fıstık


People also ask

What is the difference between .NET Core 3.1 and .NET 5?

NET 5 is a free, cross-platform, open-source developer platform for building many different types of applications. . NET 5 is the next major release after . Net Core 3.1 which was released in June 2016. The word 'Core' is dropped from the name to emphasize that .


1 Answers

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

like image 64
Hakan Fıstık Avatar answered Sep 20 '22 19:09

Hakan Fıstık