✅ asp.net background service with database
Im trying to make a background task which has a nats connection listening for incoming messages.
When a message is received i want to add it to the database but with DI i cannot inject it.
How can i work arround this?
10 Replies
Youll have to be less ambigious in your wording here, for example:
with DI i cannot inject itWhat is "it" in this sentence?
sry, with
it
i mean the dbcontext
Why can't you inject a db context?
it says the singleton IHostedService cannot consume scoped service dbcontext
Inject IServiceFactory create your own scope and resolve it in the hosted service
Sorry, IServiceScopeFactory
you could also use DbContextFactory
DbContext Lifetime, Configuration, and Initialization - EF Core
Patterns for creating and managing DbContext instances with or without dependency injection
what do you mean by "it says" here
try and be much more specific with your wording, where is that error coming from and what is the full problem
it (the service) works now thx for the help and recommendations ❤️