2009-02-26 96 views
4

我正在開發ASP.Net asmx Web服務。而在客戶端,如果向服務器的請求返回Http錯誤代碼,如http 500,我怎麼能從Web服務客戶端(我使用自動生成的客戶端代理通過使用添加Web引用)知道?如何檢查來自異步Web服務調用的錯誤

由於事先 喬治

+0

可能值得在問題標題/描述中說明您正在詢問關於異步調用的問題 – 2009-02-27 10:34:58

+0

遇到同樣的問題,HTTP 500響應不會引發異常。 – Lenny 2016-04-28 11:53:02

回答

0

您可以設置跟蹤你的web服務,一個從MSDN下面是如何:

http://msdn.microsoft.com/en-us/library/bb885203.aspx

如果你有訪問服務器也,您可以設置HealthMonitoring作爲例子,它將記錄服務器端發生的任何錯誤,如發佈500內部服務器錯誤。

健康監測 - http://msdn.microsoft.com/en-us/library/ms998306.aspx

也有日益有用的事件查看器,如果你可以遠程或登錄到服務器。

希望這有助於:

安德魯

+0

嗨安德魯,你的解決方案是在服務器端,但我想跟蹤客戶端。 – George2 2009-02-26 09:26:58

0

你可以從e.Response.GetResponseStream的信息()。正如所述,您可能需要查看服務器端以獲取更完整的信息。

+0

好主意!但是因爲我正在使用異步Web服務調用,所以在完整的事件處理程序中,我怎麼能得到Response對象? – George2 2009-02-26 09:31:00

0

這是一個常見的問題,因爲Web服務只會在遇到未處理的異常時向您發送HTTP 500(內部服務器錯誤)消息。我用我發現很久的一個技巧。基本上,您需要使用StreamReader鑽取WebException來確定異常的根本原因。

示例代碼:(對不起,沒有任何C#代碼得心應手請使用轉換器)

Try 
    'Hit the webservice. 
Catch ex As WebException 
    Dim r As HttpWebResponse = CType(ex.Response(), HttpWebResponse) 
    Using sr As StreamReader = New StreamReader(r.GetResponseStream()) 
    Dim err As String = sr.ReadToEnd() 
    'Log the error contained in the "err" variable. 
    End Using 
    Return Nothing 
Finally 
    'Clean up 
End Try 

可以使用DeveloperFusion converter,我強烈建議轉換。

+0

你的代碼非常酷!但是因爲我正在使用異步Web服務調用,所以在完整的事件處理程序中,我怎麼能得到Response對象? – George2 2009-02-26 09:32:16

+0

Response對象是從捕獲的異常中提取的。你是否收到異常? P.S:如果答案很酷,請投票! ;-) – Cerebrus 2009-02-26 09:38:12

1

George,由於您使用的是異步WS調用,因此必須在回調方法中實現異常處理。 例如: 以下是我開發的示例代碼,以演示異步代理。

public class TransformDelegateWithCallBack 
{ 
    /// <summary> 
    /// Delegate which points to AdapterTransform.ApplyFullMemoryTransformations() 
    /// </summary> 
    /// <param name="filename">Transformation file name</param> 
    /// <param name="rawXml">Raw Xml data to be processed</param> 
    /// <param name="count">Variable used to keep a track of no of async delegates</param> 
    /// <returns>Transformed XML string</returns> 
    public delegate string DelegateApplyTransformations(string filename, string rawXml, int count); 

    public ArrayList resultArray; 


    //// Declare async delegate and result 
    DelegateApplyTransformations delegateApplyTransformation; 
    IAsyncResult result; 

    /// <summary> 
    /// Constructor to initialize the async delegates, results and handles to the no of tabs in excel 
    /// </summary> 
    public TransformDelegateWithCallBack() 
    { 
     resultArray = ArrayList.Synchronized(new ArrayList()); 
    } 


    /// <summary> 
    /// Invoke the async delegates with callback model 
    /// </summary> 
    /// <param name="filename">Transformation file name</param> 
    /// <param name="rawXml">Raw Xml data to be processed</param> 
    /// <param name="count">Variable used to keep a track of no of async delegates</param> 
    public void CallDelegates(string fileName, string rawXml, int count) 
    { 
     try 
     { 
      AdapterTransform adapterTrans = new AdapterTransform(); 
      // In the below stmt, adapterTrans.ApplyFullMemoryTransformations is the web method being called 
      delegateApplyTransformation = new DelegateApplyTransformations(adapterTrans.ApplyFullMemoryTransformations); 
      // The below stmt places an async call to the web method 
      // Since it is an async operation control flows immediately to the next line eventually coming out of the current method. Hence exceptions in the web service if any will NOT be caught here. 
      // CallBackMethod() is the method that will be called automatically after the async operation is done 
      // result is an IAsyncResult which will be used in the CallBackMethod to refer to this delegate 
      // result gets passed to the CallBackMethod 
      result = delegateApplyTransformation.BeginInvoke(fileName, rawXml, count, new AsyncCallback(CallBackMethod), null); 
     } 
     catch (CustomException ce) 
     { 
      throw ce; 
     } 
    } 

    /// <summary> 
    /// Callback method for async delegate 
    /// </summary> 
    /// <param name="o">By default o will always have the corresponding AsyncResult</param> 
    public void CallBackMethod(object o) 
    { 

     try 
     { 
      AsyncResult asyncResult = (AsyncResult)o; 
      // Now when you do an EndInvoke, if the web service has thrown any exceptions, they will be caught 
      // resultString is the value the web method has returned. (Return parameter of the web method) 
      string resultString = ((DelegateApplyTransformations)asyncResult.AsyncDelegate).EndInvoke((IAsyncResult)asyncResult); 

      lock (this.resultArray.SyncRoot) 
      { 
       this.resultArray.Add(resultString); 
      } 
     } 
     catch (Exception ex) 
     { 
      // Handle ex 
     } 
    } 

} 

如果您的WS調用拋出一個異常,它只被當你在AsynResult的EndInvoke會抓住。如果您使用異步WS調用的遺忘機制,則不會調用EndInvoke,因此異常將會丟失。所以當你需要捕捉異常時總是使用回撥機制 希望這有助於:)

讓我知道如果你有任何疑問。

0

假設您有Visual Studio導入web服務,並使用Microsoft Web Services Enhancement 3。0庫,它可能會是這個樣子:

​​

任何錯誤都將在「MyWebService.DoSomethingCompletedEventArgs」對象內返回。