2

How to call async Task Framework 3.5 please help me. I am calling this method in my 3.5 framework project its shows an error.any alternet way post json data using Framework 3.5 Here's my code:

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

}
9
  • 1
    .net 3.5 don't have async Task. I know if want use it. we can upgrade .net 4.5 Commented May 12, 2016 at 14:05
  • 1
    You should tell us what the error is. Commented May 12, 2016 at 14:06
  • 2
    You cannot do this with framework 3.5. Commented May 12, 2016 at 14:07
  • 3
    There is a Nuget package to add async to older version of .NET - see nuget.org/packages/AsyncBridge.Net35 Commented May 12, 2016 at 14:08
  • any alternate way ? how call to post data json format Commented May 12, 2016 at 14:09

1 Answer 1

2

This is not possible using the 3.5 version of the .Net framework. Your solutions are:

  1. Not use the async/await/Task based methods or
  2. Change the target framework to something more recent like the 4.6 version or
  3. Use WebClient instead of the HttpClient if your use-case is simple and if it's not (ex: manipulating headers, etc) then use the HttpWebRequest object:

Here's an example using the 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);
}
Sign up to request clarification or add additional context in comments.

4 Comments

It's the domain name (http://example.com) since "{supplier_id}/availability?version=2" is not a valid URL and the host was not included in your original answer. If you include the code you used to create the httpClient I might help you with that.
There is no async part in this answer.
@ZbigniewLedwoń of course not. You missed this part "This is not possible using the 3.5 version of the .Net framework"
No, I did not missed it @Nasreddine. What about System.Threading in .NET Framework 3.5?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.