如何使用.Net framework v3.5 调用异步任务

How to call async Task using the .Net framework v3.5

如何调用async Task Framework 3.5 请帮助我。我在我的 3.5 框架项目中调用此方法它显示了一个 error.any alternet 方式 post json 使用 Framework 3.5 的数据 这是我的代码:

public async void APIPushStatusDriverPostion(string direction,string carColor,string supplier_company,string SupplierAccountId,string Karho_Ref,string booking_id,string vehicale_type, string vehicle_id, string vehicle_plate, double latitude, double longitude, string carModel, string status, string driver_id, string driver_phone, string driver_first_name, string driver_last_name)
{
    APIKarhoBookingProperties objbooking = new APIKarhoBookingProperties();
    objbooking.vehicle.vehicle_type   = vehicale_type;
    objbooking.vehicle.vehicle_id     =    vehicle_id;
    objbooking.vehicle.vehicle_plate  = vehicle_plate;
    objbooking.vehicle.latitude       =   latitude;
    objbooking.vehicle.longitude      =    longitude;
    objbooking.vehicle.eta_minutes    = "null";
    objbooking.vehicle.make           = "null";
    objbooking.vehicle.model = carModel;
    objbooking.vehicle.color = carColor;
    objbooking.vehicle.status = status;
    objbooking.vehicle.driver_id = driver_id;
    objbooking.vehicle.driver_phone = driver_phone;
    objbooking.vehicle.driver_phone = driver_first_name;
    objbooking.vehicle.driver_last_name = driver_last_name;          

    var json = JsonConvert.SerializeObject(objbooking);
    using (var response = await httpClient.PostAsync("{supplier_id}/availability?version=2", json))
    {
        string responseData = await response.Content.ReadAsStringAsync();
    }

}

使用 3.5 版的 .Net 框架无法做到这一点。您的解决方案是:

  1. 不使用基于 async/await/Task 的方法或
  2. 将目标框架更改为更新的版本,例如 4.6 版本或
  3. 使用WebClient instead of the HttpClient if your use-case is simple and if it's not (ex: manipulating headers, etc) then use the HttpWebRequest对象:

这是一个使用 WebClient class. It might not work correctly depending on how you're setting up your HttpClient:

的例子
using (WebClient client = new WebClient())
{
    string responseData = client.UploadString(host + "{supplier_id}/availability?version=2", json);
}