我的存储库中有以下方法:
public async void UpdateName(int id, string name)
{
var connection = dataContext.GetDatabase().Connection;
var initialState = connection.State;
try
{
if (initialState == ConnectionState.Closed)
await connection.OpenAsync();
await connection.ExecuteAsync("myproc", new { id, name}, commandType: CommandType.StoredProcedure);
}
catch
{
throw;
}
finally
{
if (initialState == ConnectionState.Closed)
await connection.CloseAsync();
}
}
调用
Connection is closed
时抛出异常ExecuteAsync
。在调用之前调试代码并检查连接状态时,状态为Open
.
此外,我正在查看
ExecuteAsync
实现,我看到该方法也尝试打开连接。
所以我的问题是:
更新
这是堆栈跟踪:
在 System.Data.SqlClient.SqlCommand.EndExecuteNonQuery(IAsyncResult >asyncResult) 在 >System.Threading.Tasks.TaskFactory
2 endFunction, Action1.FromAsyncCoreLogic(IAsyncResult >iar, Func
1 promise, Boolean >requiresSynchronization) --- 从上一个位置开始的堆栈跟踪结束 --- 在 Dapper.SqlMapper.d__39.MoveNext() 在 >C:\myapp\myrepository.cs:line 91 中的 MyApp.MyRepository.d__5.MoveNext() 在 System.Threading.Tasks.Task.<>c.b__128_1(Object >state) 在 System.Threading.QueueUserWorkItemCallback.Execute() 在 System.Threading.ThreadPoolWorkQueue.Dispatch() 在 System.Threading.PortableThreadPool.WorkerThread.WorkerThreadStart()1 endAction, Task