2016-04-11 4 views
1

Ich bekomme AggregateException während client.PostAsyc aufrufen, auch ich habe lange Timeout festgelegt.Wie verwalten AggregateException wirft während PostAsync-Aufruf?

Hier ist mein 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; 
} 

Kann jemand vorschlagen, wie man das bewerkstelligt? Irgendwann bekomme ich AggregateException.

Ich habe folgenden Link verwiesen, aber ich bekomme immer noch Fehler. Event Ich habe Timeout eingestellt.

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?

Antwort

1

async auf Ihre Methodendefinition hinzufügen und await statt .Result verwenden.

Mit .Result blockiert den Thread und Sie verlieren alle Vorteile der Async-Methoden.

Mit await werden nicht nur die Daten, sondern auch die Ausnahmen "ausgepackt" (so erhalten Sie eine tatsächliche Ausnahme statt AggregateException).

var result = await client.PostAsync(session.Values["URL"] + "/abcd/", contents); 

... 

string data = await result.Content.ReadAsStringAsync(); 

Es ist ein bisschen lang, aber hier ist ein gutes Video über die Verwendung von Asynchron/erwarten: https://channel9.msdn.com/Events/aspConf/aspConf/Async-in-ASP-NET

Es ist die Uhr wert.

0

AggregateException ist eine Auflistung aller beim Ausführen einer asynchronen Aufgabe aufgetretenen Ausnahmen.

Wenn Sie nur wollen, zu überprüfen, was die Ausnahme aufgetreten war, ohne jede Behandlung zu tun, können Sie so etwas wie:

catch (AggregateException ae) 
{ 
    // This will just throw the first exception, which might not explain everything/show the entire trace. 
    throw ae.InnerException 
} 

oder

catch (AggregateException ae) 
{ 
    // This will flatten the entire exception stack to a string. 
    throw new Exception(ae.ToString()) 
} 
Verwandte Themen