How to check errors from asynchronous Web Services

2019-04-09 13:47发布

I am developing ASP.Net asmx web services. And at client side, if the request to server returns Http error code, like http 500, how could I know from web services client side (I use the automatically generated client proxy by using add web references)?

thanks in advance, George

6条回答
唯我独甜
2楼-- · 2019-04-09 13:57

George, since you are using async WS calls, you must implement exception handling in the call back method. Eg: Following is the sample code I developed to give a demo of async delegates.

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
        }
    }

}

If your WS call is throwing an exception, it only gets caught when you do an EndInvoke on the AsynResult. If you are using a fire & forget mechanism of async WS call, you wont call EndInvoke and hence exception will be lost. So always use callback mechanism when you need to catch exceptions Hope this helps :)

Let me know if you have any more doubts.

查看更多
The star\"
3楼-- · 2019-04-09 14:05

This is a commonly encountered problem because Web services tend only to send you a HTTP 500 (Internal server error) message on encountering an unhandled exception. I use a trick I found long back. Basically, you need to drill into the WebException using a StreamReader to determine the root cause of the exception.

Sample code: (Sorry, didn't have any C# code handy. Please use a converter)

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

Can be converted using the DeveloperFusion converter, which I highly recommend.

查看更多
Anthone
4楼-- · 2019-04-09 14:05

See a previous question, easy-way-to-catch-all-unhandled-exceptions-in-c-net.

For web services running under IIS, it would seem that you need to catch exceptions in all threads by implementing UnhandledExceptionModule.

查看更多
该账号已被封号
5楼-- · 2019-04-09 14:07

You can set up tracing for your webservices, a how to is below from the MSDN:

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

If you have access to the server also, you can set up HealthMonitoring for example which will record any errors which occur on the server side, like you posted the 500 internal server error.

Health Monitoring - http://msdn.microsoft.com/en-us/library/ms998306.aspx

You also have the ever useful Event Viewer if you can remote or log into the server.

Hope this helps:

Andrew

查看更多
霸刀☆藐视天下
6楼-- · 2019-04-09 14:16

Assuming you have Visual Studio import the webservice and you use the Microsoft Web Services Enhancement 3.0 library, it will probably look something like this:

 private void DoWebService()
 {
      try
      {
           MyWebService.MyWebserviceWSE WSE = new MyWebService.MyWebserviceWSE.MyWebserviceWSE();

           WSE.DoSomethingCompleted += new MyWebService.DoSomethingCompletedEventHandler(WSE_DoSomethingCompleted);

           WSE.DoSomethingAsync(/* pass arguments to webservice */);            
      }
      catch (Exception ex)
      {
           // Handle errors
      }
 }

 private void WSE_DoSomethingCompleted(object o, MyWebService.DoSomethingCompletedEventArgs args)
 {
      if (args.Error != null)
      {
           // The following gets the error and shows it in a msg box
           StringBuilder sb = new StringBuilder();

           sb.AppendLine(args.Error.ToString());

           if (args.Error.InnerException != null)
           {
               sb.AppendLine(args.Error.ToString());
           }

           MessageBox.Show(sb.ToString());
      }
      else
      {
           // Do something with the results
      }
 }

Any errors will be returned inside the 'MyWebService.DoSomethingCompletedEventArgs' object.

查看更多
啃猪蹄的小仙女
7楼-- · 2019-04-09 14:19

You can get the info from e.Response.GetResponseStream(). As read said, you might need to look at the server side to get a more complete info.

查看更多
登录 后发表回答