[英]Is it fine to await for one Task but not for other?
I have a following service method, 我有以下服务方法,
public async Task<IList<ProductsImage>> InsertAsync(BaseProduct product, Dictionary<string, Stream> images)
{
try
{
if (images.Count > 0)
{
await _imageService.SaveAllAsync(images);
}
return await _repository.InsertAsync(product);
}
catch // Delete the files if there is an error from db
{
if (images.Count > 0)
{
var paths = images.Select(i => i.Key).ToList();
_imageService.DeleteAllAsync(paths);
}
throw;
}
}
The problem is that I don't want to await at _imageService.DeleteAllAsync
because it takes some time to finish, so that I can transfer my execution without waiting. 问题是我不想在
_imageService.DeleteAllAsync
等待,因为它需要一些时间才能完成,所以我可以在不等待的情况下传输执行。 Is there any danger of not using await in _imageService.DeleteAllAsync
在
_imageService.DeleteAllAsync
是否存在不使用await的_imageService.DeleteAllAsync
The right way is to send this message to a message queue so it can be processed asynchronously (without await) by an outside process. 正确的方法是将此消息发送到消息队列,以便可以由外部进程异步处理(无需等待)。 This way it will also guarantee that DeleteAllAsync will get called or retried if it fails.
这样,它还可以保证DeleteAllAsync在失败时被调用或重试。
Here is my shameless plug for the article I wrote on this subject. 这是我在这个主题上写的文章的无耻插件 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.