简体   繁体   中英

WCF Async Method only working when debugging

I have a weird issue, I'm building a Universal App and everything works when I'm debugging but when I run without debugging the emulator tries to open the application and then crashes.

Here is the code I know is failing:

private void MenuPage_Loaded(object sender, RoutedEventArgs e)
{
   SetItemSource();
}

private async void SetItemSource()
{
   MenuItems =  await AppWinService.GetMenuEntriesAsync();     
   ItemSource = new ObservableCollection<AlphaKeyGroup<Menu>>
         ((AlphaKeyGroup<Menu>.CreateGroups(MenuItems,
                                            CultureInfo.CurrentUICulture, 
                                            s => s.MenuName, 
                                            true)));

   ((CollectionViewSource)Resources["MenuGroups"]).Source = ItemSource;

 }

Any suggestions? Thanks in advance.

Are you sure MenuItems is coming back valid for all situations ? What happens on timeout?

Why not check for null just to be safe:

MenuItems =  await AppWinService.GetMenuEntriesAsync();   

if ((MenuItems != null) && (MenuItems.Any())
{
   ItemSource = new ObservableCollection<AlphaKeyGroup<Menu>>(
                                          (AlphaKeyGroup<Menu>.CreateGroups(MenuItems,
                                           CultureInfo.CurrentUICulture, 
                                           s => s.MenuName, 
                                           true)));

  ((CollectionViewSource)Resources["MenuGroups"]).Source = ItemSource;
}
else 
{ 
   MessageBox.Show("Failure to get data"); 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM