加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

通过SoapException捕获Webservice异常

发布时间:2020-12-17 01:22:10 所属栏目:安全 来源:网络整理
导读:这两天在研究Silverlight端如何捕获webService远程抛出的异常,没有太大进展,希望有了解的人能给我留言。以下是非silverlight的一般程序捕获webService端异常的做法: 1.在webservice端将抛出的一般异常封装成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;??????????? }??????? }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读