对外部提供程序进行一系列异步API调用



我需要对外部提供程序进行一系列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();
}

如果等待每个异步Task,代码将按顺序运行但是,根据代码,您似乎可以同时执行上传任务(参见下面的示例(。

展示如何同时执行上传的示例:

var client = await clientRepository.FirstOrDefaultAsync(c => c.UserId == userId);
if (client == null)
throw new ApiException("Failed to read client");
// Send data to provider
var applicant = await apiClient.CreateApplicantAsync(client);
if (applicant == null || applicant.Id.IsNullOrEmpty())
throw new ApiException("Failed to create applicant");
// Send files a to provider
var docUploadA = apiClient.UploadDocumentAsync(applicant.Id, dto.DocumentA.Stream, dto.DocumentA.FileName, dto.DocumentA.Document.Type);
var docUploadB = apiClient.UploadDocumentAsync(applicant.Id, dto.DocumentB.Stream, dto.DocumentB.FileName, dto.DocumentB.Document.Type);
var imageUpload = apiClient.UploadApplicantImageAsync(applicant.Id, dto.DocumentC.Stream, dto.DocumentC.FileName);
await Task.WhenAll(docUploadA, docUploadB, imageUpload);
if (docUploadA.Result == null || docUploadB.Result == null || imageUpload.Result == null)
throw new ApiException("Failed to upload document");
// Check the applicant
var checkResponse = await apiClient.CheckApplicantAsync(applicant.Id);
if (checkResponse == null)
throw new ApiException("Applicant check failed");
// Check is successful when the Result property is null
if (checkResponse.Result.IsNullOrEmpty())
{
result.Success();
}

考虑到您的代码,不可能不按顺序调用API请求。由于您直接等待每个调用,因此此代码的行为方式与同步代码相同。

我不知道你的应用程序的用例,但如果这个代码是在某种形式的控制器中执行的,你应该包括一个CancellationToken来取消这个长时间运行的脚本,以防用户中止请求。

最新更新