更新时间:2023-02-13 19:20:53
您的 DatabaseAction
不是异步委托.它似乎是 Action< T>
,但是您真正需要的是 Func< T,Task>
.因此,当您执行 async session =>
时,该代码将变成一个 async void
签名.由于它是 void
,因此您无法等待
.这导致您的 async
代码被调度到线程池线程中,并且该调用立即返回.这具有 await session.CommitTransactionAsync()
提交的连锁效果,而您的委托甚至还没有开始运行.现在,您在 ExecuteInTransaction
中的代码被视为完成";然后退出,并由于 using
块的原因而处理 session
.
Your DatabaseAction
is not an asynchronous delegate. It seems to be Action<T>
but what you really need is Func<T, Task>
. So when you do async session =>
that code gets turned into an async void
signature. Due to it being void
, you cannot await
it. This leads to your async
code being scheduled to a threadpool thread and the call immediately returns. This has the knock on effect that await session.CommitTransactionAsync()
commits while potentially, your delegate hasn't even started running yet. Your code inside ExecuteInTransaction
is now considered "done" and exits, disposing your session
along the way due to using
block.
要解决此问题,您需要更改您的 DatabaseAction
签名,然后 await databaseAction(session);
To fix this, you need to change your DatabaseAction
's signature and then await databaseAction(session);