【问题标题】:Xamarin & Android - crash on exiting from methodXamarin 和 Android - 退出方法时崩溃
【发布时间】:2018-12-16 19:17:05
【问题描述】:

我不知道我还能说什么。 我有这个方法:

public async Task<HttpResponseMessage> SendAsyncRequest(string uri, string content, HttpMethod method, bool tryReauthorizeOn401 = true)
{
    HttpRequestMessage rm = new HttpRequestMessage(method, uri);
    if (!string.IsNullOrWhiteSpace(content))
        rm.Content = new StringContent(content, Encoding.UTF8, "application/json");

    HttpResponseMessage response = await client.SendAsync(rm);
    if (response.StatusCode == HttpStatusCode.Unauthorized && tryReauthorizeOn401)
    {
        var res = await AuthenticateUser();
        if(res.user == null)
            return response;
        return await SendAsyncRequest(uri, content, method, false);
    }

    return response;
}

没什么特别的。 client.SendAsync(rm) 被执行,response.StatusCode 是好的。 退出此方法时应用程序会崩溃。

输出只显示了这个断言:

12-16 20:09:22.025 F/        ( 1683): * Assertion at /Users/builder/jenkins/workspace/xamarin-android-d15-9/xamarin-android/external/mono/mono/mini/debugger-agent.c:4957, condition `is_ok (error)' not met, function:set_set_notification_for_wait_completion_flag, Could not execute the method because the containing type is not fully instantiated. assembly:<unknown assembly> type:<unknown type> member:(null)
12-16 20:09:22.025 F/libc    ( 1683): Fatal signal 6 (SIGABRT), code -6 in tid 1683 (omerang.Android)

仅此而已。 客户端是 HttpClient。 我在我的 Android 项目中有设置:HttpClient Implementation 设置为 Android。

有谁知道可能出了什么问题?

编辑

SendAsyncRequest 是这样使用的:

public async Task<(HttpResponseMessage response, IEnumerable<T> items)> GetListFromRequest<T>(string uri)
{
    HttpResponseMessage response = await SendAsyncRequest(uri, null, HttpMethod.Get);
    if (!response.IsSuccessStatusCode)
        return (response, null);

    string content = await response.Content.ReadAsStringAsync();
    var items = JsonConvert.DeserializeObject<IEnumerable<T>>(content);
    return (response, new List<T>(items));
}

【问题讨论】:

  • ...containing type is not fully instantiated... 你是如何使用 SendAsyncRequest 方法的?即,如果没有使用模式,我会假设您使用的是 Result 等,但这只是一个猜测。
  • 看看我编辑的问题。
  • @AdamJachocki 不断向上移动调用堆栈,以确保您没有进行任何可能导致死锁的 .Result 阻塞调用。
  • 我没有。更重要的是,一切都在 WinForms 上运行(这个项目是 Winforms 和 Xamarin 的通用项目)。但在这里它只是崩溃,没有任何例外。调试器刚刚结束它的工作,VS 停止调试。
  • @AdamJachocki 好的,什么叫GetListFromRequest,然后叫什么等等?显示可用于帮助重现和诊断问题的minimal reproducible example。也可能是火灾后忘记问题,但没有提供足够的详细信息来进行明确的评估。

标签: android xamarin crash httpclient


【解决方案1】:

基于您提供的示例项目代码

protected override async void OnStart()
{
    Controller c = new Controller();
    TodoItem item = await c.GetTodoItem(1);
    TodoItem item2 = await c.GetTodoItem(2);
}

您正在调用async void fire 并在启动时忘记。

您将无法捕获任何抛出的异常,这将解释为什么应用程序崩溃而没有警告。

参考Async/Await - Best Practices in Asynchronous Programming

async void 只能与事件处理程序一起使用,所以我建议添加一个事件和处理程序。

根据提供的示例,它可能如下所示

public partial class App : Application {
    public App() {
        InitializeComponent();
        MainPage = new MainPage();
    }

    private even EventHander starting = delegate { };
    private async void onStarting(object sender, EventArgs args) {
        starting -= onStarting;
        try {
            var controller = new Controller();
            TodoItem item = await controller.GetTodoItem(1);
            TodoItem item2 = await controller.GetTodoItem(2);
        } catch(Exception ex) {
            //...handler error here
        }
    }

    protected override void OnStart() {
        starting += onStarting;
        starting(this, EventArgs.Empty);
    }

    //...omitted for brevity
}

这样,您现在至少应该能够捕获抛出的异常以确定失败的原因。

【讨论】:

  • 我认为在 OnStart 事件中调用 async void 是可以的。但我会按照你说的捕获异常,然后看看会发生什么。
  • @AdamJachocki 我已经看到文档示例显示了这一点,但 OnStart 不是 事件处理程序,因此不建议在该方法上使用 async void .
  • 没有任何帮助:(我完全按照您的说明做了,但结果是一样的。当然不会抛出异常。
  • @AdamJachocki 让我们看看是否可以缩小异常范围。如果你在 catch 中设置一个断点,它会命中吗?
  • 没有。就是这样。什么都没有扔。断点没有在捕获范围内命中。调试刚刚结束,我可以在模拟器上看到一条消息,说我的应用程序停止工作。
【解决方案2】:

尝试将您的 compileSdkVersion 更新到更高版本并检查。 也尝试关注

>     Go to: File > Invalidate Caches/Restart and select Invalidate and Restart

【讨论】:

  • 我正在使用 Visual Studio 2017。忘了告诉它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-01
相关资源
最近更新 更多