Skip to content

[dotnet-sdk-7.0.100-rc.1.22379.5] Unable to bind IList<string> type from appsettings.json with Configuration.Binder  #73221

Closed

Description

@Junjun-zhao

Application Name : Smartstore
OS: Windows 10 RS5
CPU: X64
.NET Build Number: dotnet-sdk-7.0.100-rc.1.22379.5

Devdiv Bug checking at: https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1585569

Github Link: https://github.com/smartstore/Smartstore

Verify Scenarios:
1). Windows rs5 x64 + dotnet-sdk-7.0.100-rc.1.22401.1: Fail
2). Windows rs5 x64 + dotnet-sdk-7.0.100-rc.1.22379.5: Fail
3). Windows10 RS5 x64+dotnet-sdk-7.0.100-preview.7.22377.5: Pass
4). Windows10 RS5 x64+dotnet-sdk-6.0.400-win-x64: Pass

Description:
When we try to bind data from appsettings.json to an object using ConfigurationBinder , we get System.InvalidOperationException.
If we change property type IList to List, issue does not repro, but in our app (SmartStore), it is defined as IList

Minimal repro steps with a sample app:

  1. Create ASP.NET Core Web App (.NET7)
  2. Add this class to project :
public class BundlingOptions
   {
       public IList<string>? Browsers { get; set; }
   }
  1. Add this section to appsettings.json
 "Bundling": {
    "Browsers": [
      "Edge",
      "Chrome",
      "Firefox"
    ]
  }
  1. In program.cs, add this code bind from appsetting.json to opt object
BundlingOptions opt = new BundlingOptions();
  var section = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build().GetSection("Bundling");
  section.Bind(opt); //exception here

Expected Result:
Data in appsettings.json should bind to opt object

Actual Result:

Unhandled exception. System.InvalidOperationException: Cannot create instance of type 'System.Collections.Generic.IList`1[System.String]' because it is either abstract or an interface.
  at Microsoft.Extensions.Configuration.ConfigurationBinder.CreateInstance(Type type, IConfiguration config, BinderOptions options)
  at Microsoft.Extensions.Configuration.ConfigurationBinder.BindInstance(Type type, BindingPoint bindingPoint, IConfiguration config, BinderOptions options)
  at Microsoft.Extensions.Configuration.ConfigurationBinder.BindProperty(PropertyInfo property, Object instance, IConfiguration config, BinderOptions options)
  at Microsoft.Extensions.Configuration.ConfigurationBinder.BindProperties(Object instance, IConfiguration configuration, BinderOptions options)
  at Microsoft.Extensions.Configuration.ConfigurationBinder.BindInstance(Type type, BindingPoint bindingPoint, IConfiguration config, BinderOptions options)
  at WebApplication5.Program.Main(String[] args) in C:\Users\v-altunc\source\repos\WebApplication5\WebApplication5\Program.cs:line 17

DotNet Info:

.NET SDK:
Version:   7.0.100-rc.1.22379.5
Commit:    c674772706

Runtime Environment:
OS Name:     Windows
OS Version:  10.0.19044
OS Platform: Windows
RID:         win10-x64
Base Path:   C:\Program Files\dotnet\sdk\7.0.100-rc.1.22379.5\

Host:
 Version:      7.0.0-rc.1.22378.2
 Architecture: x64
 Commit:       97749c034e

.NET SDKs installed:
 7.0.100-rc.1.22379.5 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
 Microsoft.AspNetCore.App 7.0.0-rc.1.22379.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
 Microsoft.NETCore.App 7.0.0-rc.1.22378.2 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
 Microsoft.WindowsDesktop.App 7.0.0-rc.1.22377.3 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

@dotnet-actwx-bot @dotnet/compat

Activity

  1. ghost added
    untriagedNew issue has not been triaged by the area owner
    on Aug 2, 2022
  2. eerhardt commented on Aug 2, 2022

    @eerhardt
    Member

    @SteveDunn - this looks to be caused by #68133. Do you want to take a look?

  3. added this to the 7.0.0 milestone on Aug 2, 2022
  4. ghost removed
    untriagedNew issue has not been triaged by the area owner
    on Aug 2, 2022
  5. ghost added
    in-prThere is an active PR which will close this issue when it is merged
    on Aug 2, 2022
  6. added a commit that references this issue on Aug 3, 2022
    3b3645e
  7. ghost removed
    in-prThere is an active PR which will close this issue when it is merged
    on Aug 3, 2022
  8. Junjun-zhao commented on Aug 15, 2022

    @Junjun-zhao
    MemberAuthor

    This issue has been verified and fixed on latest build dotnet-sdk-7.0.100-rc.1.22413.1.

  9. ghost locked as resolved and limited conversation to collaborators on Sep 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions