3

I am developing a Xamarin.Forms (4.1.0) app, which makes REST calls.

When calling my web service, the application crashes, with no exception.

Only got this message on the output:

07-08 19:09:04.792 F/        (22723): * Assertion at /Users/builder/jenkins/workspace/xamarin-android-d16-1/xamarin-android/external/mono/mono/mini/debugger-agent.c:4387, condition `is_ok (error)' not met, function:get_this_async_id, Could not execute the method because the containing type 'System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1[RestQueryResultT_REF]', is not fully instantiated. assembly:<unknown assembly> type:<unknown type> member:(null)
07-08 19:09:04.793 F/libc    (22723): Fatal signal 6 (SIGABRT), code -6 (SI_TKILL) in tid 22723 (io.puffix), pid 22723 (io.puffix)

Here is the code:

RestQueryResultT queryResult;

using (HttpClient client = new HttpClient())
using (HttpResponseMessage response = await client.GetAsync(serviceUri))
{
    string result = await response.Content.ReadAsStringAsync();
    queryResult = ParseResult(result);
}

return queryResult;

The call is made from an event handler method, fired by a UI action. The GetAsync call made tha app crashes.

Any idea to solve this issue ?

Rom Eh
  • 1,981
  • 1
  • 16
  • 33
  • which line causes the crash? Have you tried catching the exception? – Jason Jul 08 '19 at 17:19
  • The GetAsync line cause the crash. Yes, tried to add catch, and suscribed to errors from the App, with no success. – Rom Eh Jul 08 '19 at 17:21
  • does it only crash on Android? Have you tried switching the HTTP stack? Do you have Internet permissions enabled? Have you tested that your url is accesible from the device/emulator using the browser? – Jason Jul 08 '19 at 17:29
  • I am developping only on Android. The internet permissions are enabled. The URL is browsable from my device. How can I switch the HTTP Stack ? – Rom Eh Jul 08 '19 at 18:19
  • https://learn.microsoft.com/en-us/xamarin/android/app-fundamentals/http-stack?tabs=windows – Jason Jul 08 '19 at 18:21
  • and try a different device/emulator to verify it's not a device issue – Jason Jul 08 '19 at 18:22
  • I checked the HTTP stack, it was correct. I put the GetAsync call outside the using, and it is working no. Should it be this ? However, thank you for your help and your time @Jason. – Rom Eh Jul 08 '19 at 20:13

2 Answers2

15

This seems a issue and already filed in the Github. You can see the discussions in these threads:

  1. await within method with returntype Task makes app crash
  2. xamarin-android/issues
  3. xamarin-ios/issues

The workaround is using GetAwaiter().GetResult() instead of await:

Works:

response = _client.GetAsync(uri).GetAwaiter().GetResult();

Does not work:

await _client.GetAsync(uri);
nevermore
  • 15,432
  • 1
  • 12
  • 30
1

could you try with a smaller response? I had the same problem (in my case, when I tested with 2 or 3 records from the server, it worked fine, but if I used 10 records, it crashed). I had to change the simulator. I am using now my real phone connected to my computer.