简体   繁体   中英

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 ?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM