How to configure services in my cli application:
So I have code like above where I'm creating a container using autofac and then registering some services with methods like
The issue is I want the logger to get its configuration details from the IConfiguraton supplied by WithSystemConfiguration() and I'm not sure whether to use a build callback like
or create a wrapper class like MyCustomLogger that uses ILogger and IConfiguration in it's constructor
7 Replies
Does the config registration actually benefit from being embedded in a di registration, or does it only use static resources and you could just build it outside of the registrations, passing it as a concrete IConfiguration?
Otherwise your callback with resolving the config from scope seems just fine to me on a quick glance
It could be passed in I just wasn’t sure if it was a good idea writing container_builder().withLogging(config).withOtherService(config)
So to give some perspective on my thoughts - Asp net also does not exclusively expose IConfiguration through DI. It starts off the process with actually instatiating a config manager. This config is then exposed and available during the DI registration process. I could imagine this might also simplify your question, initializing config based on truly static values and just exposing it through your registration process, in case you're bending over backwards to access your config otherwise
That being said, I don't see any problem in what you're doing
If you're passing a registration callback to access config on the built scope, that's valid
It's just like injecting IConfiguration into any class
You could also think about supporting options if you're already resolving from the built container
Ahh okay so you’re suggesting creating the config object before the di container so I can just pass properties into the .withLogging function as a parameter for example ?
What do you mean by this ?
$options
appsettings.json:
src/Foo/FooOptions.cs:
src/Foo/FooServiceCollectionExtensions.cs:
Program.cs / Startup.cs:
Bar.cs:
Yeah
Exactly 😄