I am getting AggregateException while client.PostAsyc call even I have set long timeout.
Here is my code.
try
{
StoresList objlist = new StoresList();
Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
Windows.Storage.ApplicationDataContainer session = Windows.Storage.ApplicationData.Current.RoamingSettings;
var contents = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("userapi", session.Values["userapi"].ToString()),
new KeyValuePair<string, string>("keyapi", session.Values["keyapi"].ToString()),
new KeyValuePair<string, string>("notification_flag","1"),
new KeyValuePair<string, string>("token", localSettings.Values["deviceid"].ToString()),
});
using (var client = new HttpClient() { Timeout = TimeSpan.FromMinutes(4) } )
{
var result = client.PostAsync(session.Values["URL"] + "/abcd/", contents).Result;
if (result.IsSuccessStatusCode)
{
string data = result.Content.ReadAsStringAsync().Result;
if (data.Contains("activate") || data.Contains("enable"))
{
//Please activate the Mobile Assistant Extension
return null;
}
if (data != null)
{
List<Stores> objStores = JsonConvert.DeserializeObject<LoginResponse>(data).stores;
foreach (var item in objStores)
{
Stores objs = new Stores();
{
objs.id = item.id;
objs.name = item.name;
objlist.Add(objs);
}
}
}
}
}
return objlist;
}
catch (Exception ex)
{
throw ex;
return null;
}
Can anybody suggest how to manage this? Sometime I get AggregateException.
I have referred following link but I still get error. Event I have set timeout.
How can I tell when HttpClient has timed out?
AggregateException is throwing while waiting for PostAsJsonAsync
Whats the difference between HttpClient.Timeout and using the WebRequestHandler timeout properties?