UOW - A second operation started on this context before a previous asynchronous operation completed

一笑奈何 提交于 2019-11-29 09:21:46
Philip Stuyck

The problem is this code :

_unitOfWork = _UnitOfWorkFactory.createUnitOfWorkAsync();

IEnumerable<Relatie> relaties = await getRelatieAsync(_unitOfWork, relatieId).ConfigureAwait(true);

_relatieTypeTypes = await getRelatieTypeTypesAsync(_unitOfWork, relatieId).ConfigureAwait(true);
_relatie = relaties.FirstOrDefault();

 _unitOfWork.Dispose();

the UnitOfWork is an instance variable, when the scenario starts a second time it is overwritten with a new instance. The already executing scenario then uses that new UnitOfWork instead of its own because it is overwritten. Not so easy to spot, but a simple race condition. I found it by replacing all instance variables by local variables and then the problem disappeared.

This is probably not the answer, but a general look over your code.

Main purpose of async/await is, to keep the current Thread available. This helps not blocking the UI Thread and keeping your App responsive.

You are already making sure that your deep loading happens on a ThreadPool thread, because you are starting it entirely utilizing Task.Run(). You can probably get around most of your problems by using the default synchronous methods of EntityFramework in your loading mechanism.

In a first look, your code looks fine as of the async calls. Maybe your deep loading is triggered multiple times?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!