1

我正在尝试从 Instagram API 返回关注用户的列表。我在使用InstaSharp .NET 包装器的沙盒帐户。

用户通过身份验证后,将调用操作方法。

public ActionResult Following()
{
    var oAuthResponse = Session["InstaSharp.AuthInfo"] as OAuthResponse;

    if (oAuthResponse == null)
    {
        return RedirectToAction("Login");
    }

    var info = new InstaSharp.Endpoints.Relationships(config_, oAuthResponse);

    var following = info.Follows("10").Result;

    return View(following.Data);
}
4

1 回答 1

1

尝试使方法一直异步,而不是进行.Result可能导致死锁的阻塞调用

public async Task<ActionResult> Following() {
    var oAuthResponse = Session["InstaSharp.AuthInfo"] as OAuthResponse;

    if (oAuthResponse == null) {
        return RedirectToAction("Login");
    }

    var info = new InstaSharp.Endpoints.Relationships(config_, oAuthResponse);

    var following = await info.Follows("10");

    return View(following.Data);
}

取决于如何info.Follows实施。

查看Github 存储库,API 在内部调用了这样定义的方法

public static async Task<T> ExecuteAsync<T>(this HttpClient client, HttpRequestMessage request)

这看起来像您的确凿证据,因为.Result在此任务上调用更高的调用堆栈会导致您经历死锁。

参考Async/Await - 异步编程的最佳实践

于 2017-11-05T01:13:37.340 回答