-
-
Notifications
You must be signed in to change notification settings - Fork 63
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
Injecting scoped dependencies #126
Comments
Hello. |
The when is definitely different. In order to use scoped objects, the injector must also be resolved by a scope created at request time. The code below is basically what I'm using in my app's controller method that handles GraphQL requests. (note: _scopeFactory is an instance of IServiceScopeFactory)
If you try to use an injector that is not scoped, created by the default provider, to inject a scoped service, you will get an exception similar to:
|
You are right :) |
Thanks for the PR - merged. Closing this issue. |
Certain service objects, like database connections, need to have a lifetime of ServiceLifetime.Scoped so that sharing is limited to a single request. In order to allow these types of objects to be dependency injected, a new, scoped, dependency injector needs created for each request. Currently, the injector used to process GraphQL requests is specified when RequestHandler is constructed via the RequestHandlerBuilder.WithDependencyInjector method. This requires the RequestHandler to be re-constructed with each request in order to inject scoped objects. In order to avoid this overhead, I would like to propose adding an optional dependencyInjector parameter to the RequestHandler.ProcessRequest method that overrides the injector specified when the RequestHandler was constructed.
I have a PR ready that implements this and will link it to this issue for review.
The text was updated successfully, but these errors were encountered: