从 WCF FaultException 响应中提取详细信息

编程入门 行业动态 更新时间:2024-10-26 07:36:25
本文介绍了从 WCF FaultException 响应中提取详细信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我成功地与第三方肥皂服务合作.我添加了对自动生成类的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/2001/XMLSchema" xmlns:xsi="http://www.w3/2001/XMLSchema-instance" xmlns:SOAP-ENV="http://schemas.xmlsoap/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/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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-26 20:17:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1142802.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:详细信息   WCF   FaultException

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!