C# 异步操作正在进行时,无法更改连接

C# 异步操作正在进行时,无法更改连接,c#,sql,azure,ado.net,azure-sql-database,C#,Sql,Azure,Ado.net,Azure Sql Database,我的应用程序很好地运行了一段时间,然后突然我在我访问的每一页上都出现了一个非常奇怪的错误 [InvalidOperationException: Connection cannot be changed while async operation is in progress.] System.Data.SqlClient.SqlCommand.set_Connection(SqlConnection value) +5340612 InvertedSoftware.DataBlock.

我的应用程序很好地运行了一段时间,然后突然我在我访问的每一页上都出现了一个非常奇怪的错误

[InvalidOperationException: Connection cannot be changed while async operation is in progress.]

System.Data.SqlClient.SqlCommand.set_Connection(SqlConnection value) +5340612
   InvertedSoftware.DataBlock.<ExecuteReaderAsync>d__7.MoveNext() +679
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52
   XX.Data.Repositories.<GetAllByMerchantIdAsync>d__1f.MoveNext() in c:\Users\XX.cs:90
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52
   Namespace.Controllers.<Index>d__2.MoveNext() in c:\Users\XX.cs.cs:44
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52
   System.Web.Mvc.Async.TaskAsyncActionDescriptor.EndExecute(IAsyncResult asyncResult) +84
   System.Web.Mvc.Async.<>c__DisplayClass37.<BeginInvokeAsynchronousActionMethod>b__36(IAsyncResult asyncResult) +17
   System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +48
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +32
   System.Web.Mvc.Async.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +50
   System.Web.Mvc.Async.<>c__DisplayClass48.<InvokeActionMethodFilterAsynchronouslyRecursive>b__41() +225
   System.Web.Mvc.Async.<>c__DisplayClass48.<InvokeActionMethodFilterAsynchronouslyRecursive>b__41() +225
   System.Web.Mvc.Async.<>c__DisplayClass33.<BeginInvokeActionMethodWithFilters>b__32(IAsyncResult asyncResult) +10
   System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +48
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +34
   System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +26
   System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +100
   System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +48
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27
   System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +13
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +36
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +53
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +39
   System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +12
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +28
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +53
   System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +29
   System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
   System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +21
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +36
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +53
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +31
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
   System.Web.CallHandlerExecutionStep.OnAsyncHandlerCompletion(IAsyncResult ar) +129

我正在使用这个的
SqlHelper
类有一个bug<代码>ExecuteReader(异步)返回打开的读取器。它立即将用于获取该读卡器的命令放回池中。这意味着现在有一个竞争条件:读取器可以被使用,而底层命令可以被另一个线程变异。这就是为什么要抛出
SqlCommand.set\u Connection


无论如何,池化
SqlCommand
不是一个好主意,因为创建命令非常便宜。池中的同步可能比设置新命令更昂贵。

我猜您是在线程之间共享连接。不要那样做。发布相关代码。@usr,我每页都有错误。当我重新启动应用程序域(网站)时,应用程序现在运行良好。您可以在代码中找到SQLHelper.cs源代码,代码看起来不错(实际上相当不错)。您看到CommandPool(ObjectPool类型)了吗?我认为问题就在那里?试着消除它。无论如何,它都没有帮助,因为创建命令非常便宜。池中的同步可能比设置新命令更昂贵。
  using (var reader = await SqlHelper.ExecuteReaderAsync(_connectionString, CommandType.StoredProcedure, "MySP", parameters))