简体   繁体   English

WCF异步方法仅在调试时有效

[英]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 ? 您确定MenuItems 在所有情况下都恢复有效吗? What happens on timeout? 超时会发生什么?

Why not check for null just to be safe: 为什么不检查null只是为了安全:

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"); 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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