❔ Passing parameters to Services
Hi, I've a webapp where, at start, I parse some configuration values from
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 to initialize them and finally inject them into other services?
5 Replies
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 configNice, thank you for the tip!
I've finished working for today, but tomorrow I'll definitely give it a try!
Configuration in ASP.NET Core
Learn how to use the Configuration API to configure AppSettings in an ASP.NET Core app.
this page covers the details. ignore the razor stuff 😛
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.