Description
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:
- Create ASP.NET Core Web App (.NET7)
- Add this class to project :
public class BundlingOptions
{
public IList<string>? Browsers { get; set; }
}
- Add this section to appsettings.json
"Bundling": {
"Browsers": [
"Edge",
"Chrome",
"Firefox"
]
}
- 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