-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
"An attempt was made to use the context while it is being configured. A DbContext instance cannot be used inside OnConfiguring since it is still being configured at this point" and "An unhandled exception was thrown by the application. System.InvalidOperationException: The connection was not closed. The connection's current state is connecting." #6488
Comments
@ravipunjwani This is most likely happening because the same context instance is being used concurrently by multiple threads. DbContext is not thread-safe. If the context instance is not being used concurrently but this is still happening, then please post some code showing how you are obtaining and using instances of the context. |
@ajcvickers I'm using the same context in several classes passing it through constructors. But, each of my methods in all these classes (that use the dbcontexts) are async. So ideally each call in the hierarchy would not get called unless the other calls get completed. Otherwise if they are still called simultaneously, definitely they would belong to separate http requests. The load-test I referred in the description were initiated using separate http-requests (and separate httpclients) for each call, from a single unit-test function. |
Below is the code from ServiceHelper class and startup.cs, and you can see I called the erring function serviceHelper.OneService.GetAsync in this place.
|
Got this solved by myself. I was incorrectly using my serviceHelper object (can be seen in Configure function parameters injected as service). Removed this parameter and replaced the line:
This gets the ServiceHelper instance from the current request context, instead of the one passed in Configure function as injected service. |
@ajcvickers Hey I'm getting the same error message. Mine has something to do with concurrency issues although I'm not sure why... My app is stateless and basically is an api. I set up my context with |
@snaveevans Have you verified that you are getting a new instance each time? |
Hi @snaveevans ,
The above line of code might fix your problem, as it did for me the same way. Though I added my service via services.AddScoped compared to yours services.AddTransient. |
@ajcvickers not sure how to check if it is creating a new instance or not, how can I do that? (for future reference) It's now fixed though. |
@snaveevans Typical ways are using the debugger or tagging each instance with a unique value and logging it. |
@snaveevans The same SO issue that I referred to earlier, shows exactly how to achieve this and see differences visually. Switching transient and scoped would be same for you because you were using constructor injection, which would work great for the Configure method in Startup.cs. The middleware is placed in the request pipeline and would need a reference to the service instance from current request context (here comes the decision of transient and scoped - transient would give you new instance even in current request and scoped would give you a unique instance per http request lifecycle). I also got confused earlier but figured out sooner than later as the production app started creating a lot of issues. |
I am having the same issue as @snaveevans described. My app is also a webapi, and console application calls this api endpoint everytime a file is dropped into a hotfolder. The web api doesnt throw any errors if I call the endpoint in a sequential manner. As soon as start calling it in async method, the api end point throws this error: Any help is really appreciated |
@nshathish The context is not thread safe. From what you say it seems likely that their are multiple calls accessing the same context instance, possibly due to how your async code is written. |
@nshathish In your WebApi project the DbContext injection has to be Transient or PerThread. |
Steps to reproduce
Nothing different in my database configuration. I have configured my database as:
The issue
As soon as I start the application, the log files show some errors referring to the database access.
The exception details are given as below:
Further technical details
EF Core version: (1.0.0)
Operating system: Windows Server 2012 R2
Visual Studio version: VS 2015 update 3
This error comes up first few times the database is accessed in the code, but then this doesn't show up. However, another similar error often comes up when I make several requests on server simultaneously, like load test on several-threads. That error details is given below:
Exception Trace:
The text was updated successfully, but these errors were encountered: