简体   繁体   English

如何在不等待 ASP.NET API 中的响应的情况下处理多个 3rd 方 API

[英]How to process multiple 3rd party APIs without waiting for their response in ASP.NET API

Let's assume I have add order API & I need to call 3 APIs if the order is saved successfully.假设我有添加订单 API,如果订单保存成功,我需要调用 3 个 API。 I want to return the response as soon as the order processing is finished without waiting for the 3 APIs to finish.我想在订单处理完成后立即返回响应,而不用等待 3 个 API 完成。

For example:例如:

var result = ProcessOrder(parameters);
if (result.IsSuccess)
{
   await SendSMSAsync(smsDetails);   //All 3 APIs includes calling the API, saving status to database & log
   await SendEmailAsync(emailDetails);
   await ThirdPartyAsync(moreDetails);
}
return result;   //I want to return this without waiting for the above 3 APIs to finish

Also, I would like to call again any failed API automatically.另外,我想再次自动呼叫任何失败的 API。 What's the best approach to achieve this?实现这一目标的最佳方法是什么?

Instead of await, create a task and call Task.Run.不要等待,而是创建一个任务并调用 Task.Run。 This will run without waiting for the task to return.这将在不等待任务返回的情况下运行。

I achieved this by using .ConfigureAwait(false) .我通过使用.ConfigureAwait(false)实现了这一点。 For example:例如:

if (result.IsSuccess)
{
   SendSMSAsync(smsDetails).ConfigureAwait(false);
   SendEmailAsync(emailDetails).ConfigureAwait(false);
   ThirdPartyAsync(moreDetails).ConfigureAwait(false);
}

More information can be found here更多信息可以在这里找到

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET 核心 Web API - 如何使用第 3 方 API 基于条件 - ASP.NET Core Web API - How to Consume 3rd party API Based on Condition ASP.NET Core Web API - 如何使用 HttpClient 使用 3rd 方 API - ASP.NET Core Web API - How to Consume 3rd party API using HttpClient 如何在第三方ASP.NET Web API客户端中使用Oauth生成的令牌? - How to use an Oauth Generated Token in a 3rd party asp.net web API client? 如何在asp.net core web api中实现JWT Refresh Tokens(没有第三方)? - How to implement JWT Refresh Tokens in asp.net core web api (no 3rd party)? 我如何验证第三方用户访问我的Web API asp.net? - how do i authenticate 3rd party user to access my web API asp.net? 从ASP.Net MVC控制器中的第三方API返回JSON字符串 - Returning JSON string from 3rd party API in ASP.Net MVC controller 在ASP.NET MVC中管理对第三方应用程序的共享API调用的最佳场所 - Best Place to manage shared API calls to 3rd party application inside my asp.net mvc asp.net将第3方dll部署到bin文件夹 - asp.net deploy 3rd party dlls to bin folder 调用第 3 方 API asp .net 核心 - Calling 3rd party API asp .net Core 当无法访问Application_Start时如何向第三方asp.net应用程序添加路由 - How to add routes to a 3rd party asp.net application, when no access to Application_Start is available
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM