我从控制器异步调用API。有时API需要更多时间来执行,回调线程会触发对API的另一次调用。结果,总共有三次调用API。我想停止第二次和第三次通话。
如何停止第二次和第三次调用API?
以下是代码:
public async Task<HttpResponseMessage> postMessage(WebMailMessagePostRequest postMessageData) {
UriBuilder uriBuilder = new UriBuilder(_webMailRestClientHelper.domain, _webMailRestClientHelper.useHttps, "address");
postMessageData.idImpersonator = SmartType.smartInt(Security.idImpersonatorSession);
string content = JsonConvert.SerializeObject(postMessageData);
byte[] bytes = Encoding.ASCII.GetBytes(content);
_webMailRestClientHelper.PostOrPutServiceAsync<WebMailMessagePostRequest>(Request, uriBuilder.uri, bytes);
}
答案 0 :(得分:0)
如果您使用'async',为什么不等待API调用:
public async Task<HttpResponseMessage> postMessage(WebMailMessagePostRequest postMessageData)
{
UriBuilder uriBuilder = new UriBuilder(_webMailRestClientHelper.domain, _webMailRestClientHelper.useHttps, "address");
postMessageData.idImpersonator = SmartType.smartInt(Security.idImpersonatorSession);
string content = JsonConvert.SerializeObject(postMessageData);
byte[] bytes = Encoding.ASCII.GetBytes(content);
response = await _webMailRestClientHelper.PostOrPutServiceAsync<WebMailMessagePostRequest>(Request, uriBuilder.uri, bytes);
return response;
}
API也应该是异步的:
public async Task<HttpResponseMessage> postMessage(WebMailMessagePostRequest postMessageData)
{
HttpResponseMessage response = null;
}