【发布时间】:2020-12-29 08:32:01
【问题描述】:
我有一个使用 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"
}
【问题讨论】:
-
读取响应消息,如果不等于“已注册”则再次调用。带有
loop的if语句和在下次调用 api 之前稍等片刻以防止节流,听起来就像你所需要的。 -
"我想知道我应该如何一直在控制器内部等待,直到响应 json 属性值设置为 'Registered'" - 你根本不应该(在控制器中等待几分钟到几小时的事情是一个坏主意)...但是由于它不是您正在寻找的答案,因此我不会将其作为答案发布
-
为什么要等待答案?
-
在上述请求之后,我还有另一个 API 请求要完成。需要高于一个值的下一个 API 请求应“已注册”。这就是为什么我需要等待。整个过程可以使用 Azure power-shell 完成。它也在等待那段时间。
-
我知道这不是好的做法。但要求就是这样。 @Ryan 我可以在这里使用 while 循环吗?
标签: c# asp.net-mvc azure asp.net-core