PostAsync is not working properly, whenever executed it gives exception in the catch.

Hi guys,
I'm having problem executing "PostAsync" as it is giving unusual error, My whole code works fine except the line with "PostAsync". The weird thing is that , it posts the content to the database through PostAsync successfully but then jumps to "Catch" and throws exception. I have given my code below, what am I doing wrong..?

FYI: I'm using Xamarin.Forms and the database is Sharepoint list

try
{
using (var client = new HttpClient()
{ Timeout = TimeSpan.FromSeconds(5) })
using (var content = new MultipartFormDataContent())
{
var values = new[]
{
new KeyValuePair<string, string>("PinCode", pincode),
new KeyValuePair<string, string>("Email", email),
new KeyValuePair<string, string>("FirstName", firstname),
new KeyValuePair<string, string>("MiddleName", middlename),
new KeyValuePair<string, string>("FamilyName", familyname)

                       };                       

                    foreach (var keyValuePair in values)
                        content.Add(new StringContent(keyValuePair.Value), keyValuePair.Key);

                   using (var result = await client.PostAsync(requestUri, content).ConfigureAwait(false))  //App Jumps to Catch and don't execute further
                       {
                            var input = await result.Content.ReadAsStringAsync().ConfigureAwait(false);

                            if (result.IsSuccessStatusCode)
                            {
                                await DisplayAlert("Response:>", result.StatusCode.ToString(), "OK");
                                submit.IsEnabled = false;
                            }
                            else await DisplayAlert("Response:>", "Unsuccessful", "OK");
                       }
                }                
        }
        catch (TaskCanceledException ex)
        {
            if (ex.CancellationToken.IsCancellationRequested == false)
            {  await DisplayAlert("ex", "Unsuccessful", "OK");   }
            else
            { await DisplayAlert("ex", "true~~~~", "OK");  }

}

Answers

  • JohnHardmanJohnHardman GBUniversity mod

    @MuhammadHamza.5351 - Post the details of the exception.

    Also, be aware that your use of ConfigureAwait(false) is likely to cause problems when you call DisplayAlert (or otherwise interact with the UI thread).

  • @JohnHardman said:
    @MuhammadHamza.5351 - Post the details of the exception.

    Also, be aware that your use of ConfigureAwait(false) is likely to cause problems when you call DisplayAlert (or otherwise interact with the UI thread).

    @JohnHardman The catch throws the first condition which is IsCancelledToken = false
    and shows this exception attached below:
    this whole exception is displayed in one Popup.

    please find the link below of the images for the exception.

    drive.google.com/drive/folders/1wG0qOGtg0U2F-R9aP3PtBq8PQDvoJkfQ?usp=sharing

  • JohnHardmanJohnHardman GBUniversity mod
    edited November 2017

    @MuhammadHamza.5351 - It makes things a lot easier for people reading posts if you paste exception details into your post as text, rather than as a series of images in the cloud. Also, if you get your code formatted correctly (put three back-ticks on the line before the code and another three on the line after the code). A back-tick is the character following the colon here:`

    If you Google "Xamarin.Forms HTTPClient.PostAsync TaskCancelledException" you will find multiple existing posts etc that might explain what you are seeing.

    Are you seeing this on just iOS, just Android, just UWP, all of them, or just some of them? Knowing that may help identify the cause. For example, if it's just on iOS, it could be related to your TLS settings. Alternatively, if it's on all platforms, it may just be that the server didn't respond in the timescale you expected. There are many possibilities.

    (Don't forget to sort out the ConfigureAwait as mentioned previously)

  • JohnHardmanJohnHardman GBUniversity mod

    @MuhammadHamza.5351 - Just seen your post at https://forums.xamarin.com/discussion/107206/readasstringasync-doesnt-return-anyhting-and-so-app-halts-after-100-seconds#latest

    I guess that means it was the timeout being too short, as mentioned as a possibility above.

    When you find a solution to questions that you post, please update the thread to show the problem has been solved.

  • @JohnHardman no it wasn't the timeout issue. The default timeout is 100 seconds, and it use to timeout exactly after 100 seconds,
    the problem was with the Web Service I was using, refreshing and restarting the web Service worked for me

Sign In or Register to comment.