App crash during POST with NSUrlSessionHandler

Following code leads app crash at SendAsync method call.
using (var http = new HttpClient(new NSUrlSessionHandler())) { http.Timeout = TimeSpan.FromSeconds(10); var request = new HttpRequestMessage(HttpMethod.Post, "https://api.flexrail.ru/tg/signin"); var authInfo = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{login}:{password}")); request.Headers.Authorization = new AuthenticationHeaderValue("Basic", authInfo); request.Content = new StringContent("SignIn", Encoding.UTF8, "text/plain"); var responce = http.SendAsync(request, HttpCompletionOption.ResponseContentRead).Result; System.Diagnostics.Debug.WriteLine(responce.Content.ReadAsStringAsync().Result); var statusCode = responce.StatusCode; return statusCode == HttpStatusCode.OK; }

HttpClientHandler works fine. Why?

iPhone 5s, iOS 10.2, Xamarin.iOS 10.4.0.49, Xamarin for Visual Studio 4.3.0.490

Crash log attached

Answers

  • JohnMillerJohnMiller USForum Administrator, Xamarin Team Xamurai
    edited December 2016

    @AntonPlotnikov,

    Unfortunately, the crash log is not symbolicated so that makes it tougher to debug with.

    You should refactor this code to use await instead of .Result and you might be able to debug this easier in the IDE.

    // Make sure the containing method is marked `async`
    using (var http = new HttpClient(new NSUrlSessionHandler()))
    {
        http.Timeout = TimeSpan.FromSeconds(10);
        var request = new HttpRequestMessage(HttpMethod.Post, "https://api.flexrail.ru/tg/signin");
        var authInfo = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{login}:{password}"));
        request.Headers.Authorization = new AuthenticationHeaderValue("Basic", authInfo);
        request.Content = new StringContent("SignIn", Encoding.UTF8, "text/plain");
        var responce = await http.SendAsync(request, HttpCompletionOption.ResponseContentRead);
        System.Diagnostics.Debug.WriteLine(await responce.Content.ReadAsStringAsync());
        var statusCode = responce.StatusCode;
        return statusCode == HttpStatusCode.OK;
    }
    
  • AntonPlotnikovAntonPlotnikov RUMember ✭✭
    edited December 2016

    Hello @JohnMiller.

    Refactoring to awaint/async hasn't helped. Crash still exists and a native exception isn't catched by VS debugger. Symbolicated crash log attached.

  • AntonPlotnikovAntonPlotnikov RUMember ✭✭

    Moreover, changing to async leads to app was hang even with HttpClientHandler

  • JohnMillerJohnMiller USForum Administrator, Xamarin Team Xamurai

    @AntonPlotnikov,

    If the app hangs with async then I suspect something is not implemented correctly. Can you share a complete sample including the method and method call?

    I can't reproduce an issue with that code against another API.

Sign In or Register to comment.