2012-04-09 235 views
0

我幾乎完成了我的第一個WP7應用程序,並且希望將其發佈到市場。然而,發佈的應用程序的規定之一是它在使用過程中不會意外崩潰。處理需要Web服務的應用程序 - 處理EndpointNotFoundExceptions

我的應用程序幾乎完全依賴於WCF Azure服務 - 所以我必須隨時連接到Internet以使我的函數能夠工作(與託管數據庫通信) - 包括登錄,添加/刪除/編輯/搜索客戶端等等。

未連接到互聯網時,或者在使用過程中連接斷開時,調用Web服務將導致應用程序退出。我該如何處理?我認爲連接到服務的失敗將會被捕獲,並且我可以處理這個異常,但是這種方式不起作用。

 LoginCommand = new RelayCommand(() => 
     { 
      ApplicationBarHelper.UpdateBindingOnFocussedControl(); 
      MyTrainerReference.MyTrainerServiceClient service = new MyTrainerReference.MyTrainerServiceClient(); 

      // get list of clients from web service 
      service.LoginCompleted += new EventHandler<LoginCompletedEventArgs>(service_LoginCompleted); 

      try 
      { 
       service.LoginAsync(Email, Password); 
      } 
      **catch (Exception ex) 
      { 
       throw new Exception(ex.Message); 
      }** 
      service.CloseAsync(); 
     }); 

編輯:

我的主要問題是如何處理在WP7的EndpointNotFoundException沒有應用程序崩潰。

謝謝,

傑拉德。

回答

0

你的代碼看起來應該像

LoginCommand = new RelayCommand(Login); 
... 

public void Login() 
{ 
    var svc = new MyTrainerReference.MyTrainerServiceClient(); 
    try 
    { 
     svc.LoginCompleted += LoginCompleted; 
     svc.LoginAsync(); 
    } 
    catch (Exception e) 
    { 
     svc.CloseAsync(); 
     ShowError(e); 
    } 
} 

private void LoginCompleted(object sender, LoginCompletedEventArgs e) 
{ 
    ((MyTrainerReference.MyTrainerServiceClient)sender).LoginCompleted -= LoginCompleted; 
    ((MyTrainerReference.MyTrainerServiceClient)sender).CloseAsync(); 

    if (e.Error == null && !e.Cancelled) 
    { 
     // TODO process e.Result 
    } 
    else if (!e.Cancelled) 
    { 
     ShowError(e.Error); 
    } 
} 

private void ShowError(Exception e) 
{ 
    // TODO show error 
    MessageBox.Show(e.Message, "An error occured", MessageBoxButton.OK); 
} 

代碼調用LoginAsync,然後立即CloseAsync,我認爲這將導致問題......

+0

非常感謝您的提示。我將重新提出問題,詢問如何處理EndpointNotFoundExceptions。 – renegade442 2012-04-09 21:58:00

+0

如果你使用我的代碼,這個異常也將被處理和顯示......我沒有看到你的問題...... – 2012-04-09 22:04:34

+0

其實,我沒有繼續拋出Reference.cs錯誤 - 現在工作。再次感謝! – renegade442 2012-04-09 22:07:14