C#C
C#3y ago
Anton

❔ API DI advice

I want to implement a configurable injected class via an extension method, using the options system for the configuration. By looking through some code, I gathered that by doing .Configure<Options> you can chain configuration actions, which will execute when the container is being built. When the service that requires these options is instantiated, it would get the options snapshot from the service provider as a service.

However, what do I do if I still want to use this system, chaining the repeated actions, but I also want to register more services from these actions? How do I reference the service collection? Do I keep a reference to it in the options class? That's not it, right?

Basically, I need to register a few dependencies while the service is being configured from those actions.

Also, I've seen a library use ServiceDescriptor added explicitly, but the code there is really funky, I don't understand how it works (it being encumbered with abstractions does not help).
Was this page helpful?