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.