Imagine I have the following settings in appsettings.json :
{
"OtherSettings" : {
"id" : 3
},
"Needed" : {
"Database" : {
"ConnectionString" : "abc123"
},
"Interval" : {
"is_active" = true,
"in_seconds" = "15"
}
}
}
For the Database section I've got an class which is DatabaseSettings
with property ConnectionString
.
And for the Interval section I've got the class IntervalSettings
.
If I would like to bind all those settings inside Needed
to an class NeededSettings
, what property do I need and how would I bind the options for the Database class with Dependency Injection ? Currently I am doing this with:
services.Configure<DatabaseSettings>Configuration.GetSection("Needed:Database"));
services.Configure<IntervalSettings>(Configuration.GetSection("Needed:Interval"));
So I am doing this with 2 lines, but I want to do it in 1 line and bind all the settings to the new class NeededSettings
CodePudding user response:
Simply remove these two lines and add:
services.Configure<NeededSettings>(Configuration.GetSection("Needed"));
and use NeededSettings
in all places you used DatabaseSettings
and IntervalSetttings
.
Edit:
public class NeededSettings{
public DatabaseSettings Database {get;set;}
public IntervalSettings Interval {get;set;}
}