【发布时间】:2022-02-18 18:45:36
【问题描述】:
我需要对外部提供者进行一系列 API 调用,每个调用都依赖于前一个调用的成功响应。每个调用方法都是异步的。以下代码是我处理此任务的方式。我担心这些调用可能不会按顺序调用,所以我想知道我是否做对了。
var client = await clientRepository.FirstOrDefaultAsync(c => c.UserId == userId).ConfigureAwait(false);
if (client == null)
throw new Exception("Client Not Found");
// Send data to provider
var applicant = await apiClient.CreateApplicantAsync(client).ConfigureAwait(false);
if (applicant == null || applicant.Id.IsNullOrEmpty())
throw new ApiException("Failed to create applicant");
// Send document a to provider
var documentResponse = await apiClient.UploadDocumentAsync(applicant.Id, dto.DocumentA.Stream, dto.DocumentA.FileName, dto.DocumentA.Document.Type).ConfigureAwait(false);
if (documentResponse == null)
throw new ApiException("Failed to upload document");
// Send document b to provider
documentResponse = await apiClient.UploadDocumentAsync(applicant.Id, dto.DocumentB.Stream, dto.DocumentB.FileName, dto.DocumentB.Document.Type).ConfigureAwait(false);
if (documentResponse == null)
throw new ApiException("Failed to upload document");
// Send applicant c to provider
var imageResponse = await apiClient.UploadApplicantImageAsync(applicant.Id, dto.DocumentC.Stream, dto.DocumentC.FileName).ConfigureAwait(false);
if (imageResponse == null)
throw new ApiException("Failed to upload document");
// Check the applicant
var checkResponse = await apiClient.CheckApplicantAsync(applicant.Id).ConfigureAwait(false);
if (checkResponse == null)
throw new ApiException("Applicant check failed");
// Check is successful when the Result property is null
if (checkResponse.Result.IsNullOrEmpty())
{
result.Success();
}
【问题讨论】:
-
My concern is that these calls may not be called in sequence是什么让你这么说?代码本身没有任何问题 -
为什么不能使用 .Result ?
标签: c# .net asynchronous