通过SoapException捕获Webservice异常

这两天在研究Silverlight端如何捕获webService远程抛出的异常,没有太大进展,希望有了解的人能给我留言。以下是非silverlight的一般程序捕获webService端异常的做法:

1.在webservice端将抛出的一般异常封装成soapException

在这里,我们知道webService传递数据的方法是以xml格式的,故其异常也存在于xml文档中

public enum FaultCode
        {
            Client = 0,
            Server = 1
        }

        /// <summary>
        /// 封装异常为SoapException
        /// </summary>
        /// <param name="uri">引发异常的方法uri</param>
        /// <param name="errorMessage">错误信息</param>
        /// <param name="errorNumber">错误号</param>
        /// <param name="errorSource">错误源</param>
        /// <param name="code">异常类型</param>
        /// <returns>封装后的SoapException</returns>
        public SoapException RaiseException(
                                                string uri,
                                                string errorMessage,
                                                string errorNumber,
                                                string errorSource,
                                                FaultCode code
                                            )
        {
            //初始化限定名
            XmlQualifiedName faultCodeLocation = null;

            //异常类型代码转换
            switch (code)
            {
                case FaultCode.Client:
                    faultCodeLocation = SoapException.ClientFaultCode;
                    break;
                case FaultCode.Server:
                    faultCodeLocation = SoapException.ServerFaultCode;
                    break;
            }

            //构建异常信息结构体
            string strXmlOut = @"<detail>"
                             + "<Error>"
                             + "<ErrorNumber>" + errorNumber + "</ErrorNumber>"
                             + "<ErrorMessage>" + errorMessage + "</ErrorMessage>"
                             + "<ErrorSource>" + errorSource + "</ErrorSource>"
                             + "</Error>"
                             + "</detail>";

            //装载为Xml文档
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(strXmlOut);

            //实例化SoapException
            SoapException soapEx = new SoapException(errorMessage, faultCodeLocation, uri, xmlDoc.DocumentElement);

            //返回SoapException
            return soapEx;
        }

2. 在webMethod内调用该封装方法,

        [WebMethod]
        public string HelloWorld()
        {
            try
            {
                throw new Exception("webService error");
                return "HelloWorld";

            }
            catch (System.Exception ex)
            {
                throw RaiseException(
                                        "WSReadPersonByID",
                                        ex.Message,
                                        "1000",
                                        ex.Source,
                                        FaultCode.Server
                                    );
            }
        }

3.在客户端获得异常XML并解析

public class SoapExceptionInfo
    {
        /// <summary>
        /// 错误号
        /// </summary>
        public string ErrorNumber = string.Empty;
        /// <summary>
        /// 错误信息
        /// </summary>
        public string ErrorMessage = string.Empty;
        /// <summary>
        /// 错误源
        /// </summary>
        public string ErrorSource = string.Empty;

        /// <summary>
        /// SoapExceptionInfo构造方法
        /// </summary>
        public SoapExceptionInfo()
        {

        }

        /// <summary>
        /// SoapExceptionInfo构造方法
        /// </summary>
        /// <param name="soapEx">SoapException</param>
        public SoapExceptionInfo(SoapException soapEx)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(soapEx.Detail.OuterXml);
            XmlNode categoryNode = doc.DocumentElement.SelectSingleNode("Error");

            this.ErrorNumber = categoryNode.SelectSingleNode("ErrorNumber").InnerText;
            this.ErrorMessage = categoryNode.SelectSingleNode("ErrorMessage").InnerText;
            this.ErrorSource = categoryNode.SelectSingleNode("ErrorSource").InnerText;
        }
    }

4.在客户端捕获异常

public Form1()
        {
            InitializeComponent();
            ServiceReference1.Service1SoapClient t = new wfTest.ServiceReference1.Service1SoapClient();
            try
            {
                label1.Text = t.HelloWorld();
            }
            catch (SoapException ex)
            {
                SoapExceptionInfo s = new SoapExceptionInfo(ex);
               
                label1.Text = s.ErrorMessage;
                //label1.Text = ex.Message;
            }
            catch(Exception e)
            {
                label1.Text = e.Message;
            }
        }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值