c# - 如何继续等待 API 请求响应值完成 ASP .Net Core MVC
问题描述
我有使用 Azure 服务管理 API 的带有多个外部 api 请求的 asp .net core mvc 应用程序。https://docs.microsoft.com/en-us/rest/api/resources/providers/register#code-try-0从这个 api 请求注册任何命名空间。但它有更多的时间来完成。最初,它的响应 json 属性值为“未注册”,在上述 POST 请求发送其值设置为“注册”之后。它需要 2 到 3 分钟才能完成。最后将响应值设置为“已注册”。
HttpClient client = new HttpClient();
HttpRequestMessage request2 = new HttpRequestMessage(HttpMethod.Post, String.Format(Constants.MicrosoftManagedProviderRegisterApi, subscription));
request2.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
// Ensure a successful response
HttpResponseMessage response2 = await client.SendAsync(request2);
response2.EnsureSuccessStatusCode();
这就是我发送 API 请求的方式。我想知道我应该如何在控制器内部等待,直到响应 json 属性值设置为“已注册”。有什么简单的方法可以实现这一点吗?
请参阅下面的响应类型。
{
"id": "/subscriptions/***/providers/Microsoft.ManagedServices",
"namespace": "Microsoft.ManagedServices",
"authorization": "",
"resourceTypes": [],
"registrationState": "Unregistered",
"registrationPolicy": "RegistrationRequired"
}
{
"id": "/subscriptions/***/providers/Microsoft.ManagedServices",
"namespace": "Microsoft.ManagedServices",
"authorization": "",
"resourceTypes": [],
"registrationState": "Registering",
"registrationPolicy": "RegistrationRequired"
}
{
"id": "/subscriptions/***/providers/Microsoft.ManagedServices",
"namespace": "Microsoft.ManagedServices",
"authorization": "",
"resourceTypes": [],
"registrationState": "Registered",
"registrationPolicy": "RegistrationRequired"
}
解决方案
HttpClient
有一个名为的属性Timeout
,默认情况下设置为100
秒,使用这个:
client.Timeout = TimeSpan.FromMinutes(4);
推荐阅读
- html - 在 CSS 中使用变换样式模糊文本
- python - 使用自定义有效负载在 Python 中使用 IP:PORT 进行 Ping
- ibm-doors - 如何在 DOORS 中使用 DXL 获取条形 RTF 格式并获取实际字符串值?
- python - 带有 Seaborn 的 LinePlot:查看限制最小误差
- excel - VBA将行添加到csv文件
- javascript - 将 JSON 流式传输到 Redis
- vhdl - VHDL - 想要创建一个简单的分隔线
- javascript - 从接口声明中访问静态属性
- javascript - 使用 let 时的 Javascript 问题
- python - 显示来自 2 个单独的 JSON 文件的 JSON 数据 Discord PY