Reputation: 6871
I recently added some code to my application to consume an webAPI. This is done with an HttpClient. On my development machine everything is working fine. But as soon as I publish to an Azure WebApp (directly or via a buildprocess) I'm getting a runtime error once the application has been deployed.
This is the exception I'm getting;
The exception points to line 246. Which is equivalent to this method;
public IList<SubscriberDefinition> GetAllSubscribers()
{
IList<SubscriberDefinition> allSubscribers = Caching.Get<IList<SubscriberDefinition>>(CacheKeys.ListAllSubscribers);
if (allSubscribers != null && allSubscribers.Count > 0)
return allSubscribers;
try
{
SetBearerToken(HttpClient);
HttpResponseMessage response = HttpClient.GetAsync("api/getallsubscribers").Result;
if (response.IsSuccessStatusCode)
{
allSubscribers = response.Content.ReadAsAsync<IList<SubscriberDefinition>>().Result;
Caching.Add(CacheKeys.ListAllSubscribers, allSubscribers, ECacheDuration.TwentyFourHours);
return allSubscribers;
}
}
catch
{
return new List<SubscriberDefinition>();
}
return new List<SubscriberDefinition>();
}
I've been pulling my hair out for two days now about this issue. But I can't figure out what it is.. The setup of this piece of code is similar to another API I'm consuming in the application. And there it is working fine. So it must be the references I'm using in this particular class. Hopefully somebody can help me out here, or give me a push in the right direction.
Upvotes: 1
Views: 485
Reputation: 6871
After a long search I found out that the problem was caused by the way I was using async. Many thanks to @JohnRasch for pushing me in the right direction!
I suspect that in one of the many referenced NuGet and MyGet packages an update took place causing this error to occur. I updated the way the response was handled and converted to the type I wanted. And that solved the issue. Though I still don't completely understand what the root cause for this problem was, I atleast know how to fix it properly.
For this specific service / API-call I was able to get it working with the following code;
public IList<SubscriberDefinition> GetAllSubscribers()
{
IList<SubscriberDefinition> allSubscribers = Caching.Get<IList<SubscriberDefinition>>(CacheKeys.ListAllSubscribers);
if (allSubscribers != null && allSubscribers.Count > 0)
return allSubscribers;
try
{
SetBearerToken(HttpClient);
HttpResponseMessage response = Task.Run(async () => await HttpClient.GetAsync("api/getallsubscribers")).Result;
if (response.IsSuccessStatusCode)
{
var jsonstring = response.Content.ReadAsStringAsync();
jsonstring.Wait();
allSubscribers = JsonConvert.DeserializeObject<IList<SubscriberDefinition>>(jsonstring.Result);
Caching.Add(CacheKeys.ListAllSubscribers, allSubscribers, ECacheDuration.TwentyFourHours);
return allSubscribers;
}
}
catch
{
return new List<SubscriberDefinition>();
}
return new List<SubscriberDefinition>();
}
In another service we also had this error and there we could fix it by going async all-the-way.
Upvotes: 1