我必須從Angular對Web API進行多次調用。問題是這導致線程中出現某種死鎖,並且超時返回「任務被取消」。我嘗試使用在另一篇文章中找到的ConfigureAwait(false),但它沒有解決我的問題。在我的角度控制器,我有:從Angular導致Web API的多個異步調用導致程序掛起
function getClusterLinks(linkCode, returnString) {
var deferred = $q.defer();
$scope.returnString = returnString;
contractorService.gethyperlink(linkCode)
.success(function (data) {
var vchUrl = data[0].vchUrl;
var end = vchUrl.length;
var docID = vchUrl.substring(vchUrl.indexOf("=") + 1, end);
var vchLinkName = data[0].vchLinkName;
var yay = '<a href="" ng-click="getDocumentByID(' + docID + ')">' + vchLinkName + '</a>';
var yCode = "|Y" + linkCode + "~";
$scope.returnString = $scope.returnString.replaceAll(yCode, yay);
deferred.resolve();
})
return deferred.promise;
}
在我的角度服務:
this.gethyperlink = function (id) {
return $http.get('/Home/GetHyperLink?id=' + id)
};
在我的MVC控制器:
public async Task<ActionResult> GetHyperLink(int id)
{
var link = await CommonClient.GetHyperLinkByID(id);
return Json(link, JsonRequestBehavior.AllowGet);
}
在我CommonWebApiClient:
public Task<List<HyperLinkModel>> GetHyperLinkByID(int linkID)
{
try
{
var response = PerformGet<List<HyperLinkModel>>(string.Format("api/GetMyHyperlinks/{0}", linkID.ToString()));
return response;
}
catch (Exception ex)
{
throw ex;
}
}
在我的WebApiClientBase:
protected async Task<T> PerformGet<T>(string requestUri)
{
using (var client = new HttpClient { BaseAddress = _baseAddress })
{
try
{
AppendDefaultHeaders(client);
var response = await client.GetAsync(requestUri).ConfigureAwait(false);
var responseContent = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<T>(responseContent);
}
catch (Exception e)
{
throw e;
}
}
}
以上地方掛線是:
var response = await client.GetAsync(requestUri).ConfigureAwait(false);
任何幫助是非常感謝!
你在哪裏打電話給PerformGet? – MichaelDotKnox
請參閱我上面的編輯。謝謝! –