如何检查从异步Web服务调用错误 [英] How to check errors from asynchronous Web Services calls

查看:133
本文介绍了如何检查从异步Web服务调用错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发ASP.Net ASMX Web服务。而在客户端,如果请求到服务器返回的HTTP错误code,如HTTP 500,我怎么会从Web服务客户端知道(我用添加Web引用使用自动生成的客户端代理)?

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

推荐答案

乔治,因为你正在使用异步调用WS,您必须实现回调方法异常处理。
例如:
以下是示例code我开发给异步委托的演示。

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

}

如果您的WS调用抛出一个异常,它只是被当你做对的AsynResult抓到的EndInvoke。如果您使用的是消防放大器;忘了异步WS调用机制,你不会调用EndInvoke,因此异常将丢失。因此请务必使用回调机制,当您需要捕捉异常
希望这有助于:)

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.

这篇关于如何检查从异步Web服务调用错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆