-
-
Notifications
You must be signed in to change notification settings - Fork 64
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
Injector.Resolve gets called outside of request context? #49
Comments
Remark: we're using normal ASP.NET WebAPI 2, not ASP.NET Core. |
Found the root of the problem. There seems to be only one dependency injector and it is kept on the engine (see https://github.com/graphql-dotnet/conventions/blob/master/src/GraphQL.Conventions/Adapters/Engine/GraphQLExecutor.cs#L87) although the method This means, that in a multi-threading environment thread 1 can override the dependency injector set by thread 2, which is exactly what happens when we try to process multiple requests at the same time. I consider this as a bug, but I already have a fix which I will send as a PR in a moment. I only need to add a unit-test to cover this situation. |
Ah sorry dude. Just saw this now. Thanks for looking into it - nice catch! Will look at your PR here later today and get that merged. Thanks! |
Thank you. I'm leaving on a holiday after today, so if there are any issues with the PR, I can fix them today, otherwise it might take some time. In the meantime, we're creating an engine for each request 😮, not performant, but we have to prepare for a demo 😄 |
Give each executor its own IDependencyInjector #49
👍 Haha, yeah. I've merged it now - looks good! Version 1.2.9 is indexing on NuGet as we speak :-) Thanks for fixing this! |
There seems to be an issue that sometimes
Injector.Resolve
gets called outside the WebAPI request context (at least this is how it looks like).I've created a very simple WebApplication in an attempt to reproducing this issue.
In our application, we're using Autofac for resolving our repositories. Therefore, we created a
IDependencyInjector
for your library that lets us resolve types from Autofac:The complete source of this test web application can be found here
Now, sometimes, during a request, an
ObjectDisposedException
is thrown by Autofac because it seems like_scope
is already disposed, which is theAutofacWebRequest
scope as we register it asInstancePerRequest()
like this:builder.RegisterType<Injector>().As<IDependencyInjector>().InstancePerRequest();
.This can only be the case after the WebAPI request is completed. This makes me think that we're getting into the
Resolve
method too late. This only happens on rare occasions but we see it happening every day in our production code with only 1 active user.I have written a small Console application that sends out graphql queries in a loop from 10 simultaneous threads and I always get the exception somewhere between request 200 and 1500.
I've also compared the complete stacktrace of a successful Resolve with the one from a failing Resolve and they both look the same (apart from the method inside Autofac that throws the exception).
Stacktrace of failing Resolve can be found here, the successful Resolve can be found here.
The console application for firing requests looks like this:
We could probably register everything as
InstancePerDependency
to decouple it from any lifetimescope, but I consider this as a workaround rather than a fix. I really want to track down this one, but I'm out of inspiration. Do you have any idea what can go wrong?The text was updated successfully, but these errors were encountered: