❔ Passing parameters to Services

Aalkasel11/21/2022
Hi, I've a webapp where, at start, I parse some configuration values from
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
to initialize them and finally inject them into other services?
PPobiega11/21/2022
This sounds a lot like what Microsoft.Extensions.Configuration already does with its IOption<T> interface
PPobiega11/21/2022
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
PPobiega11/21/2022
in your services that then need access to the configuration values, you simply add a IOptions<MyConfigObject> options to your service constructor
PPobiega11/21/2022
and voila, you have access to a strongly typed instance of the config
Aalkasel11/21/2022
Nice, thank you for the tip!
Aalkasel11/21/2022
I've finished working for today, but tomorrow I'll definitely give it a try!
PPobiega11/21/2022
this page covers the details. ignore the razor stuff 😛
PPobiega11/21/2022
            services.Configure<PositionOptions>(
                config.GetSection(PositionOptions.Position));
            services.Configure<ColorOptions>(
                config.GetSection(ColorOptions.Color));
AAccord11/22/2022
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.