2009-02-26 95 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”对象内返回。