我需要對外部提供者進行一系列 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();
}
uj5u.com熱心網友回復:
鑒于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();
}
uj5u.com熱心網友回復:
鑒于您的代碼,不可能不按順序呼叫您的 API 請求。由于您直接等待每個呼叫,因此此代碼的行為方式與它是同步的相同。
我不知道您的應用程式的用例,但如果此代碼在某種形式的控制器中執行,您應該包含一個 CancellationToken 以取消這個長時間運行的腳本,以防用戶中止請求。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/430584.html
上一篇:帶有異步OnInitialized的RadzenDataGrid多項選擇
下一篇:復雜的串行和并行承諾解決方案
