2010-04-16 297 views
0

我開發了Silverlight客戶端,使異步Web服務調用到asmx Web服務。問題是,我想處理異常,以便能夠在客戶端應用程序中告知Web服務中是否存在異常(因此將記錄在Web服務的本地)或者是否存在通信問題(即Web服務的端點是錯誤的)。處理異步ASMX Web服務異常

在我的項目中測試兩種類型的異常,我得到了相同的一般異常:

System.ServiceModel.CommunicationException: The remote server returned an error: NotFound. 

這個例外是驚人的無用時異常的web服務發生,因爲它顯然已經找到。

是否存在與安全性有關的通用錯誤(不被允許查看真正的錯誤)?這不可能是因爲我在開發PC上運行時沒有調試字符串。

無論哪種方式,我的問題是,在商業silverlight應用程序中處理異步錯誤的最佳方法是什麼?

任何鏈接或想法是最受歡迎的! :)

非常感謝!

Andy。

+0

我覺得這原來是一個重複 - http://stackoverflow.com/questions/1910915/can-silverlight-wcf-client-read-exceptions-from-an-asmx-web-service - 它確實有一個接受的答案,雖然... – Bermo 2010-04-16 11:12:11

回答

0

是的,通用錯誤處理安全性。這個想法是,如果攻擊者在頁面中發現錯誤等等。這個人不知道錯誤的原因是什麼。

您是否打開了serviceDebug標記中的遠程調試?

http://www.mostlydevelopers.com/mostlydevelopers/blog/post/2009/01/14/Debugging-Tips-ndash3b-The-remote-server-returned-an-error-NotFound.aspx

這將返回一個不太常規錯誤。

+0

啊,認爲這樣,感謝您的鏈接,但這似乎是與WCF服務,而不是普通的'asmx([WebMethod]屬性)。不確定它會起作用! – Andy 2010-04-16 10:50:21

0

我想你可能真的會在這裏得到404錯誤。如果服務中存在例外情況,但includeDetailsInException被設置爲false,那麼您將得到一個FaultException,但只有Exception.Message

我認爲你需要去查看服務運行的機器上,看看你的客戶端收到異常時是否有任何錯誤或警告。特別是,如果服務在.NET 2.0或更高版本上運行,則當發生未處理的異常時,ASP.NET運行狀況監視的默認配置會將警告消息記錄到應用程序事件日誌中。

0

我試圖爲此找到「正確的」解決方案。它不是返回原始的例外是個好主意,所以這是什麼,我想出了一個簡化版本:

public class AjaxResponse 
    {    
     public string Message { get; set; } 
     public string FullError { get; set; } 
     public AjaxResponse() { } 
     public AjaxResponse(Exception e) 
     { 
      Message = e.Message; 
      FullError = e.ToString(); 
     } 
    } 
    public class AjaxResponse<T> : AjaxResponse 
    { 
     public T Response { get; set; } 

     public AjaxResponse() { } 
     public AjaxResponse(Exception e) : base(e) { } 
     public AjaxResponse(T resp) 
     { 
      Response = resp; 
     }   
    } 

用法:

 [WebMethod] 
     public AjaxResponse<T> DoStuff(...) 
     { 
      try 
      { 
       T value = new T(...); 
       return new AjaxResponse<T>(value); 
      } 
      catch (Exception ex) 
      { 
       return new AjaxResponse<T>(ex); 
      } 
     }