挂起返回跟踪关系端点的数据

时间:2017-11-05 01:06:56

标签: c# instagram-api instasharp

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

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

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

1 个答案:

答案 0 :(得分: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 repo,API在内部调用一个像这样定义的方法

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

看起来像你的冒烟枪,因为在此任务上调用堆栈.Result更高的调用堆栈会导致你经历过僵局。

参考Async/Await - Best Practices in Asynchronous Programming