I'm creating an Instagram API client on ASP MVC using HttpClient, I'm trying to make a get request but it fails without throwing exception or responding and doesn't respond to my timeout. Here is my code:
 public class InstagramService
 {
    private HttpClient Client = new HttpClient {
       BaseAddress = new Uri("https://api.instagram.com/v1/"),
       Timeout = TimeSpan.FromMilliseconds(500)
    };
    public async Task<InstagramUser> GetInstagramUser(long? userId = null)
    {
       InstagramUser User = null;
       string Parameter = (userId == null) ? "self" : userId.ToString();
       try {
          var response = await Client.GetAsync("users/" + Parameter + "/" + GetAccessToken());
          if (response.IsSuccessStatusCode)
          {
              User = await response.Content.ReadAsAsync<InstagramUser>();
          }
      }catch(Exception e)
      {
          Console.WriteLine(e.Message);
          Console.WriteLine(e.InnerException.Message);
      }
      return User;
    }
    private string GetAccessToken()
    {
        return "?access_token=" + DB.config_det_sys.Single(i => i.codigo == "ACCESS_TOKEN_INSTAGRAM" && i.estado == true).Valor;
    }
 }
EDIT
Here I add how I call my service on the Home Controller, I will still test changing the controller to async Task
public class HomeController : Controller
{
    private InstagramService IGService = new InstagramService();
    public ActionResult About()
    {
       var apiCall = IGService.GetInstagramUser();
       var model = apiCall.Result;
       return View(model);
    }
}
I tested on Postman trying to make the API call and it indeed worked, so where I'm failing to catch errors?
Your problem is here:
var model = apiCall.Result;
As I describe on my blog, you shouldn't block on asynchronous code. It can cause a deadlock.
Instead of Result, use await:
var model = await apiCall;
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With