我正在开发一个 Xamarin.Forms (4.1.0) 应用程序,它进行 REST 调用。
当调用我的网络服务时,应用程序崩溃,无一例外。
仅在输出中收到此消息:
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)
这是代码:
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;
该调用是通过事件处理程序方法进行的,由 UI 操作触发。
GetAsync 调用导致应用程序崩溃。
有解决这个问题的想法吗?
这似乎是一个问题,并且已经在Github
。您可以查看这些主题中的讨论:
- 在返回类型为 Task 的方法内等待会使应用程序崩溃
- xamarin-android/问题
- xamarin-ios/问题
解决方法是使用GetAwaiter().GetResult()
代替await
:
Works:
response = _client.GetAsync(uri).GetAwaiter().GetResult();
不起作用:
await _client.GetAsync(uri);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)