问题描述:
我在我创建了一个Web API以下操作:
I have the following operation in a Web API I created:
// GET api/
[HttpGet]
[Route("pharmacies/{pharmacyId}/page/{page}/{filter?}")]
public CartTotalsDTO GetProductsWithHistory(Guid pharmacyId, int page, string filter = null ,[FromUri] bool refresh = false)
{
return delegateHelper.GetProductsWithHistory(CustomerContext.Current.GetContactById(pharmacyId), refresh);
}
此WebService的呼叫通过jQuery的Ajax调用这种方式完成的:
The call to this webservice is done through a Jquery Ajax call this way:
$.ajax({
url: "/api/products/pharmacies/<%# Farmacia.PrimaryKeyId.Value.ToString() %>/page/" + vm.currentPage() + "/" + filter,
type: "GET",
dataType: "json",
success: function (result) {
vm.items([]);
var data = result.Products;
vm.totalUnits(result.TotalUnits);
}
});
我已经看到了实现previous操作这样一些开发商:
I've seen some developers that implement the previous operation this way:
// GET api/
[HttpGet]
[Route("pharmacies/{pharmacyId}/page/{page}/{filter?}")]
public async Task
{
return await Task.Factory.StartNew(() => delegateHelper.GetProductsWithHistory(CustomerContext.Current.GetContactById(pharmacyId), refresh));
}
不得不说,虽然,GetProductsWithHistory()是一个相当长的操作。鉴于我的问题和情况,将如何使的WebAPI操作异步有什么好处?
Gotta say, though, that GetProductsWithHistory() is a quite long operation. Given my problem and context, how will making the webAPI operation asynchronous benefit me?