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

时间:2016-05-12 14:01:36

标签: c# .net-3.5

如何调用异步任务框架3.5请帮帮我。我在我的3.5框架项目中调用此方法,它显示了一个错误。使用Framework 3.5发布json数据的任何alternet方式这是我的代码:

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();
    }

}

1 个答案:

答案 0 :(得分:1)

使用3.5版本的.Net框架是不可能的。您的解决方案是:

  1. 不使用基于async / await / Task的方法或
  2. 将目标框架更改为更新的内容,例如4.6版本或
  3. 如果您的用例很简单,如果不是(例如:操纵标题等),请使用WebClient代替HttpClient,然后使用HttpWebRequest对象:
  4. 以下是使用WebClient类的示例。它可能无法正常运行,具体取决于您设置HttpClient

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