Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin Forms crash with async Http call

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 ?

like image 265
Rom Eh Avatar asked Mar 08 '26 15:03

Rom Eh


1 Answers

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);
like image 67
nevermore Avatar answered Mar 10 '26 10:03

nevermore