0

I'm calling graph.windows.net to make an api call which is returned as JSON and it works, however, after a random period of time my JSON array returns as NULL and crashes my application, which can be restored after I re-publish through visual studio.

[Authorize]
    public async Task<Dictionary<string,string>> UserApps()
    {

        string tenantId = ClaimsPrincipal.Current.FindFirst(TenantIdClaimType).Value;

        // Get a token for calling the Windows Azure Active Directory Graph
        AuthenticationContext authContext = new AuthenticationContext(String.Format(CultureInfo.InvariantCulture, LoginUrl, tenantId));
        ClientCredential credential = new ClientCredential(AppPrincipalId, AppKey);
        AuthenticationResult assertionCredential = authContext.AcquireToken(GraphUrl, credential);

        string authHeader = assertionCredential.CreateAuthorizationHeader();
        string requestUrl = String.Format(
            CultureInfo.InvariantCulture,
            GraphApplicationsUrl,
            HttpUtility.UrlEncode(tenantId),
            HttpUtility.UrlEncode(User.Identity.Name));

        HttpClient client = new HttpClient();
        HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, requestUrl);
        request.Headers.TryAddWithoutValidation("Authorization", authHeader);
        HttpResponseMessage response = await client.SendAsync(request);
        string responseString = await response.Content.ReadAsStringAsync();
        JObject responseObject = JObject.Parse(responseString);
        JArray jsonArray = (JArray)responseObject["value"];


        Dictionary<string, string> apps = new Dictionary<string, string>();


        if (jsonArray != null && jsonArray.Count > 0)
        {

            foreach (JObject s in jsonArray)
            {
                if (!apps.ContainsKey(s["resourceDisplayName"].ToString()))
                { 
                apps.Add(s["resourceDisplayName"].ToString(), s["resourceId"].ToString()); 

            }

        }
        else
        {
            //Console.WriteLine(jsonArray.ToString());
        }



        return apps;
    }
    `

EDIT: I believe that @Filburt is correct and that it's regarding the request limits to the API. I'll do some more research into why I'm hitting this limit.

Kevin Kulla
  • 450
  • 2
  • 20
  • Side note: Have you considered that you might create too many requests and simply get blocked? – Filburt Nov 15 '16 at 22:19
  • That's what I had originally thought was that I was hitting the request limit, however, I cannot find any documentation regarding what Microsoft's limit is on API Calls. – Kevin Kulla Nov 15 '16 at 22:23
  • A NullReferenceException occurs when you try to consume a member of a null reference. I suggest that you debug the application, locate the line of code in which this occurs, and work it out. – Eyal Perry Nov 15 '16 at 22:25
  • You mean `responseString` is null, or `(JArray)responseObject["value"]` is null? If the latter, what are the contents of the `responseString`? – dbc Nov 15 '16 at 23:29
  • `(JArray)responseObject["value"]` is null, but it doesn't happen every time. It just started happening the other day. I have setup a `Console.WriteLine` for the `responseString` when it fails, but when it works, it is an array of all of the elements that I'm fetching from the API. – Kevin Kulla Nov 16 '16 at 13:48

1 Answers1

0

The issue is my authentication token is expiring. I just needed to re-authenticate and it solved my issue.

Kevin Kulla
  • 450
  • 2
  • 20