0

I have seen a lot of examples doing this and am wondering if they are advised.

Scenario

Lets assume I have a database that has some details about how to register a service with DI, for sake of simplicity lets assume my service requires a URL to work along with other data.

So in ConfigureServices() I wish to access the DBContext to fill this data. There are many examples suggesting you can do it by calling services.BuildServiceProvider(); and then you can access the DBContext. Although this works its not suggested to do it this way.

How can I access my DBContext within the ConfigureServices method in a way that I can read settings to configure additional services?

Timothy G.
  • 6,335
  • 7
  • 30
  • 46
AliK
  • 962
  • 2
  • 10
  • 31
  • 1
    Why is this any different from injecting the context/DAL in the service itself and use it to initialize any of its prop(s)/field(s) in the constructor? – Ergis Sep 10 '21 at 12:21
  • 1
    Lets say you have EmailSettings object which is configured from the database and then you can inject this into any services that requires EmailSettings again just an example but this is very simple if settings are in appsettings.json as you can just call services.configure() and bind to settings from json but from db does not seem as simple. – AliK Sep 10 '21 at 13:07
  • You can check using custom configuration provider https://stackoverflow.com/a/73838748/6453193 – Mary Sep 24 '22 at 16:27

0 Answers0