Workaround for a regression in NET7 discovered while converting a project from NET6 to NET7.
Description
I have an ModuleCollection
class which is:
public class ModuleCollection : ConcurrentDictionary<string, ModuleConfig>
{
public ModuleCollection() : base(StringComparer.OrdinalIgnoreCase) { }
}
In NET6:
services.Configure<ModuleCollection>(configuration.GetSection("Modules"));
would load the ModuleCollection
with the ModuleConfig
s from multiple modulesettings.json files that had been registered with the ConfigurationBuilder
. The IOption<ModuleCollection>
injected would have the populated collection as its value.
In NET7, the IOption<ModuleCollection>.Value
is an empty ModuleCollection
.
Workaround
I worked around this by:
services.AddOptions<ModuleCollection>()
.Configure(moduleCollection =>
{
var moduleNames = configuration.GetSection("Modules").GetChildren().Select(x => x.Key).ToList();
foreach (var moduleName in moduleNames)
{
if (moduleName is not null)
{
ModuleConfig moduleConfig = new ModuleConfig();
configuration.Bind($"Modules:{moduleName}", moduleConfig);
moduleCollection.TryAdd(moduleName, moduleConfig);
}
}
});
Expected Behavior
Same as NET6
IOption<ModuleCollection>.Value
contains the collection of ModuleConfig
s.
Actual Behavior
The IOption<ModuleCollection>.Value
is an empty collection.
Issue Tracking
This issue is being tracked in
ServiceCollection.Configure<ConcurrentDictionary<string, T>>(IConfigurationSection) fails in NET7 · Issue #78994 · dotnet/runtime · GitHub[^],
Configuration
NET7, VS2022 17.4.1
History
- 29th November, 2022: Initial version
As Senior Architect, Matthew is responsible for the Architecture, Design, and Coding of the CodeProject software as well as Manager of the Infrastructure that runs the web site.
Matthew works on improving the performance and experience of the Code Project site for users, clients, and administrators.
Matthew has more years of software development, QA and architecture experience under his belt than he likes to admit. He graduated from the University of Waterloo with a B.Sc. in Electrical Engineering. He started out developing micro-processor based hardware and software including compilers and operating systems.
His current focus is on .NET web development including jQuery, Webforms, MVC, AJAX, and patterns and practices for creating better websites.
He is the author of the
Munq IOC, the fastest ASP.NET focused IOC Container.
His non-programming passions include golf, pool, curling, reading and building stuff for the house.