ฉันมีการดำเนินการต่อไปนี้ใน Web API ที่ฉันสร้างขึ้น:
// GET api/<controller>
[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);
}การเรียกใช้บริการเว็บนี้ทำผ่าน Jquery Ajax เรียกวิธีนี้:
$.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);
      }          
  });ฉันเคยเห็นนักพัฒนาบางคนที่ใช้การดำเนินการก่อนหน้านี้ด้วยวิธีนี้:
// GET api/<controller>
[HttpGet]
[Route("pharmacies/{pharmacyId}/page/{page}/{filter?}")]
public async Task<CartTotalsDTO> GetProductsWithHistory(Guid pharmacyId, int page, string filter = null ,[FromUri] bool refresh = false)
{
    return await Task.Factory.StartNew(() => delegateHelper.GetProductsWithHistory(CustomerContext.Current.GetContactById(pharmacyId), refresh));
}ต้องบอกว่า GetProductsWithHistory () เป็นการดำเนินการที่ค่อนข้างยาว จากปัญหาและบริบทของฉันการทำให้การทำงานแบบอะซิงโครนัส webAPI เป็นประโยชน์ต่อฉันอย่างไร
GetProductsWithHistoryAsync() Task<CartTotalsDTO>การเขียนตัวควบคุม async ของคุณอาจมีประโยชน์หากคุณต้องการย้ายการเรียกที่ทำให้เป็น async ด้วย จากนั้นคุณจะเริ่มได้รับประโยชน์จากส่วน async เมื่อคุณโยกย้ายส่วนที่เหลือ
                
async Task<T>ไฟล์. โปรดจำไว้ว่า AJAX ถูกนำมาใช้ก่อนที่ TPL จะมีอยู่จริง :)