-
Notifications
You must be signed in to change notification settings - Fork 25.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New topic: Getting the DbContext outside of a Controller #4782
Comments
@Rick-Anderson that really is a DI question, not an EF question 😸 By default https://docs.microsoft.com/en-us/aspnet/core/migration/1x-to-2x/#move-database-initialization-code But not sure where else this could be useful besides initialization. E.g. the thread in reddit talks about a service that scheddules recurrent tasks rather tha servicing external requests. I imagine we would need general guidance on how to do that correctly. BTW, @davidfowl pushed a change late in 2.0 which removed the need to create the DI scope if you needed to use a scoped service (e.g. a |
@scottaddie can you review this? |
We have a real use case: We're using
in our Asp.NET Core Application. None of the 4 entry points (public EventHubProcessor(), OpenAsync(), CloseAsync(), ProcessEventsAsync()) are integrated in the DI system, so we have to be "creative" even when only using an ILogger. Up to Asp.NET Core 2.0, we injected an IServiceProvider into Configure() of Startup, saved that into a static property of Startup and used that in the Eventprocessor Contructor with This broke with asp.NET Core 2.1 because of aspnet/Hosting#1106, now the IServiceProvider injected into Configure() is scoped and most of the time disposed at the time the EventprocessorHost is created. Now we have to be even more creative and safe this IServiceProvider in program.cs:
and fetch the ILogger in Eventprocessor Constructor with:
None of these options are a clean solution and prone to fail in future asp.net core Versions. It would be very helpful if there would be easier ways to use the DI system in code paths which are not derived from IController or which are integrated with the DI system like Hangfire |
Yes this is tricky as it hard to integrate when multiple systems want to be the owner of the container, and that's what going on here. Ideally you would register something in the DI container that activated the EventHubProcessor from the DI container. It looks like there's a factory though but I'm not 100% sure if it'll work well here. |
per @davidfowl
There's enough confusion here that I think we should have a first class topic for it on the EF docs:
https://www.reddit.com/r/dotnet/comments/7cednh/how_to_grab_a_dbcontext_from_outside_a_controller/
We should probably also have something on accessing scoped services outside of an http request.
Can one of @ajcvickers or @divega provide the sample for Getting the DbContext outside of a Controller?
@rynowak @danroth27 who's the contact for Accessing scoped services outside of an HTTP request?
The text was updated successfully, but these errors were encountered: