gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 17:40:21 25 4
gpt4 key购买 nike

我成功地使用了第三方 soap 服务。我添加了对自动生成类的 soap Web 服务的服务引用。

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

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

我可以捕获错误但无法提取细节。我尝试了以下代码:

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

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

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

这是第三方 API,所以我无法更改响应。

最佳答案

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

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

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

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

这种方法奏效了。

关于c# - 从 WCF FaultException 响应中提取详细信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16523399/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com