C#Async,Await和ConfigureAwait的关系

跟着阿笨一起玩NET

共 1001字,需浏览 3分钟

 ·

2023-03-03 19:15

以下是ConfigureAwait(false)的示例:

public async Task<int> ExecuteAsync(UpdateCarCommand request, CancellationToken token = default)  {      using (var context = _contextFactory.Create())      {          var entity = context.Cars.FirstOrDefault(a => a.Id == request.Id);          // Mapping logic          return await context.SaveChangesAsync(token).CongifureAwait(false);      }  }


[HttpPut]public IActionResult Put([FromBody]UpdateCommand command) => _responseMediator.ExecuteAsync(command).ConfigureAwait(false).GetAwaiter().GetResult();



 

以下代码为例:


private HttpClient _client = new HttpClient(); async Task<List<string>> GetBothAsync(string url1, string url2){ var result = new List<string>(); var task1 = GetOneAsync(result, url1); var task2 = GetOneAsync(result, url2); await Task.WhenAll(task1, task2); return result;} async Task GetOneAsync(List<string> result, string url){ var data = await _client.GetStringAsync(url); result.Add(data);}



浏览 43
点赞
评论
收藏
分享

手机扫一扫分享

分享
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

分享
举报