c# - web.api 中的同步和异步方法
问题描述
有人可以解释一下这段代码是否是潜在的死锁。将同步数据库调用与异步数据库调用混合,如下面的代码。或者,如果首先执行异步调用,那就是死锁的风险。
[Route("{id}/someobjects")]
[ResponseType(typeof(IEnumerable<SomobjectDto>))]
public async Task<IHttpActionResult> GetSomeobjects(string id)
{
var syncMethodResult = SyncDBCallMethod(); //In this method there is a databas call..
var asyncMethodresult = await AsyncDBMCallMethod(1L); //In this method there is a Async databas call..
解决方案
在这种情况下,异步AsyncDBMCallMethod
调用将在同步SyncDBCallMethod
完成之前开始。
一旦你没有在 async/await 函数中混合阻塞调用Result
,.Wait
并且正确地等待async 函数,你应该能够同时进行 async 和 sync 调用。
推荐阅读
- postgresql - 将 oracle Reference 触发器转换为 postgreSQL 触发器
- video - 当从 withUIImagePickerController 或 PHPickerViewController 使用的照片库中选择视频时,与 AirDrop 共享的 HDR 视频的 mediaURL 为零
- rust - 如何在第一个 None 值上停止无限迭代器?
- javascript - 在 Firefox 和 Safari 上使用 React 本地化 Fullcalendar
- xpath - 无法通过在 XPATH 中提供变量名称来定位元素
- python - 如何查询 GeoJSON 对象中的特定对象
- python-3.x - S3 文件只能以只读模式打开
- debian - debuild - 是否可以从一个来源创建多个具有不同名称的 debian 包添加特定选项
- javascript - 在 React 中将回调指定为另一个回调的依赖项是否重要?
- android-studio - 如何在 android studio 中使用 org.joda.time.LocalDate 更改我的日期格式?