How to check errors from asynchronous web service calls

I am developing an ASP.Net asmx web service. And on the client side, if a request to the server returns an Http error code, for example, http 500, how can I find out from the client side of web services (I use an automatically generated client proxy by adding web links)?

thanks in advance George

+4
source share
6 answers

George, since you are using async WS calls, you must implement exception handling in the callback method. For example: The following is sample code that I developed to demonstrate asynchronous 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 throws an exception, it only gets caught when you execute EndInvoke in AsynResult. If you use fire and forget the async WS call mechanism, you will not call EndInvoke, and therefore the exception will be lost. So always use the callback mechanism when you need to catch exceptions. Hope this helps :)

Let me know if you have more doubts.

+1
source

You can configure tracing for your web services, as is done below from MSDN:

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

If you have access to the server, you can configure HealthMonitoring, for example, which will record any errors that occur on the server side, for example, you posted an internal server 500 error.

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

You also have a useful event viewer if you can remotely or log in to the server.

Hope this helps:

Andrew

0
source

You can get information from e.Response.GetResponseStream (). As stated above, you may need to look at the server information to get more complete information.

0
source

This is a common problem because web services usually send you an HTTP 500 error (Internal server error) when encountering an unhandled exception. I am using a trick that I found a long time ago. Basically, you need to drill through a WebException using StreamReader to determine the root cause of the exception.

Code example: (Sorry, you did not have a C # code, 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 

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

0
source

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

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

0
source

Assuming Visual Studio is importing a web service and you are using 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.

0
source

Source: https://habr.com/ru/post/1234690/


All Articles