从 WCF FaultException 响应中提取详细信息 [英] Extracting detail from a WCF FaultException response

查看:28
本文介绍了从 WCF FaultException 响应中提取详细信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我成功地与第三方肥皂服务合作.我添加了对自动生成类的soap web 服务的服务引用.

I am successfully working with a third party soap service. I have added a service reference to a soap web service which has auto generated the classes.

当发生错误时,它会返回这样的soap响应:

When an error occurs it returns a soap response like this:

<SOAP-ENV:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
   <SOAP-ENV:Body>
      <SOAP-ENV:Fault>
         <faultcode>SOAP-ENV:Client</faultcode>
         <faultstring xsi:type="xsd:string">Error while reading parameters of method 'Demo'</faultstring>
         <detail xsi:type="xsd:string">Invalid login or password. Connection denied.</detail>
      </SOAP-ENV:Fault>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

我可以捕获错误,但无法提取详细信息.我尝试了以下代码:

I can catch the error but not extract the detail. I have tried the following code:

catch (FaultException ex)
{
    MessageFault msgFault = ex.CreateMessageFault();
    var elm = msgFault.GetDetail<string>();
    //throw Detail
}

但是,由于详细信息节点不是对象,因此出现以下错误:

However it Errors with the following as detail node is not an object:

Expecting element 'string' from namespace 'http://schemas.datacontract.org/2004/07/MyDemoNamespace'.. Encountered 'Text'  with name '', namespace ''.

这是第三方 API,因此我无法更改响应.

This is third party API so I cannot change the response.

推荐答案

消息错误的明细节点应包含 XML.GetDetail 将此 XML 反序列化为给定的对象.

The detail node of the message fault is expected to contain XML. The GetDetail will deserialize this XML into the given object.

由于内容不是 XML,因此可以使用此方法.

As the contents is not XML it was possible to use this method.

但是,您可以访问 XML 并读取 innerXml 值:

You can however get access to the XML and read the innerXml value:

MessageFault msgFault = ex.CreateMessageFault();
var msg = msgFault.GetReaderAtDetailContents().Value;

这种方法奏效了.

这篇关于从 WCF FaultException 响应中提取详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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