✅ Using a scoped service within an IHostedService
I have a IHostedService implementation that sends a Mediatr request every hour. The handler uses a DbContext to carry out its work.
When sending the request, I get the following exception:
I have tried to create an async scope, but I still get the exception.
I have listed the project in GitHub. Here is a link to where the functionality lies https://github.com/Scharps/AuctionData/blob/Scharps/issue15/AuctionData.ApplicationCore/Features/ProcessAuctionData.cs
Thank you for reading.
5 Replies
you can't just create the scope, you must also resolve all services you need from the scoped service provider
So I would have to provide the DbContext with the request?
I think resolving the mediatr from the scoped provider will do it?
you def don't need to include the dbcontext in the request
yep
mediatR will in turn request its handler from the same service provider, and thus the context from there too
That did it!
Final code:
Thanks @Pobiega and @jcotton42