❔ Passing parameters to Services

Hi, I've a webapp where, at start, I parse some configuration values from
appssettings.json
appssettings.json
Then, from these configuration values, I'd like to build some object and passing them to services. Please note these services could have other services injected into them. Should I have classes representing configuration objects, make them services, use
GetRequestService
GetRequestService
to initialize them and finally inject them into other services?
5 Replies
Pobiega
Pobiega2y ago
This sounds a lot like what Microsoft.Extensions.Configuration already does with its IOption<T> interface essentially, you specify a "section" of your configuration and that part of the config gets deserialized into an object that is then stored in your DI container in your services that then need access to the configuration values, you simply add a IOptions<MyConfigObject> options to your service constructor and voila, you have access to a strongly typed instance of the config
alkasel#159
alkasel#159OP2y ago
Nice, thank you for the tip! I've finished working for today, but tomorrow I'll definitely give it a try!
Pobiega
Pobiega2y ago
Configuration in ASP.NET Core
Learn how to use the Configuration API to configure AppSettings in an ASP.NET Core app.
Pobiega
Pobiega2y ago
this page covers the details. ignore the razor stuff 😛
services.Configure<PositionOptions>(
config.GetSection(PositionOptions.Position));
services.Configure<ColorOptions>(
config.GetSection(ColorOptions.Color));
services.Configure<PositionOptions>(
config.GetSection(PositionOptions.Position));
services.Configure<ColorOptions>(
config.GetSection(ColorOptions.Color));
Accord
Accord2y ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server