question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

There is already an open DataReader associated with this Command which must be closed first.

See original GitHub issue

Hi, I am using ASP.NET 5.0 and Linq2Db 3.2.1 for my API. I use SQL Server I am following this guide https://linq2db.github.io/articles/get-started/asp-dotnet-core/index.html I also use services.AddLinqToDbContext to register my DataConnection with the ServiceLifetime is Scoped.

And I got this error when I send 2 requests in the same time. “There is already an open DataReader associated with this Command which must be closed first.

I think change the ServiceLifetime to Transient is not the good practices. Do you have any ways else or guide doc to solve this issue? Thank you!

For more detail:

fail: LinqToDB.Data.DataConnection[0]
      Error
      Exception: System.InvalidOperationException
      Message  : There is already an open DataReader associated with this Command which must be closed first.
         at System.Data.SqlClient.SqlCommand.<>c.<ExecuteDbDataReaderAsync>b__126_0(Task`1 result)
         at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
         at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
      --- End of stack trace from previous location ---
         at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)
      --- End of stack trace from previous location ---
         at LinqToDB.Data.DataConnection.ExecuteReaderAsync(CommandBehavior commandBehavior, CancellationToken cancellationToken)

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:13 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
sdanylivcommented, Dec 26, 2020

As I know each request creates scope which equals to creating new DataConnection. Probably problem in you controller, for example missed await, or you have written something like this:

var query1 = ...;
var query2 = ...;

var tasks = new List<Task>{ query1.ToListAsync(), query2.ToListAsync() };

await Task.WhenAll(tasks);
1reaction
MaceWinducommented, Dec 26, 2020

yep, but you need to wait for #2728 to make it work with linq2db

Read more comments on GitHub >

github_iconTop Results From Across the Web

There is already an open DataReader associated with this ...
There is already an open DataReader associated with this Command which must be closed first. Update: stack trace added:
Read more >
Why do I get the error message 'There is already an open ...
Why do I get the error message 'There is already an open DataReader associated with this Connection which must be closed first.' ......
Read more >
There is already an open DataReader associated with this ...
Close the DataReader & then SqlConnection, if there is an exception is not getting closed. best practices to write this code in try...
Read more >
There is already an open DataReader associated with this ...
With SQL-Server: Exception: There is already an open DataReader associated with this Command which must be closed first. Code: using ( ...
Read more >
Script Task: There is already an open DataReader ...
Script Task : There is already an open DataReader associated with this Command which must be closed first. ' A Microsoft platform for...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found