Search code examples
c#instagram-apiinstasharp

Hung returning Follows data from Relationships endpoint


I'm trying to return a list of followed users from the Instagram API. I'm on a sandbox account using the InstaSharp wrapper for .NET.

The action method is being called after user is authenticated.

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

Solution

  • Try making the method async all the way through instead of making the blocking call .Result which runs the risk of causing a deadlock

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

    depending on how info.Follows was implemented.

    Looking at the Github repo, the API internally makes a call to a method defined like this

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

    Which looks like your smoking gun as calling .Result higher up the call stack on this task would result in your experienced deadlock.

    Reference Async/Await - Best Practices in Asynchronous Programming