如何通过在.NET中给出fallexception的tdetail来提取异常的细节?

发布于 2025-01-17 22:49:51 字数 1616 浏览 4 评论 0原文

SOAP 请求的响应是以下 xml 输出。

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
    <SOAP-ENV:Fault>
        <faultcode>SOAP-ENV:Client</faultcode>
        <faultstring xml:lang="en">INVALID_REQUEST - Request is not a valid for XSD rules.</faultstring>
        <detail>
            <ServiceError xmlns="http://test.test.com/xmlschema/common">
                <code>INVALID_REQUEST</code>
                <description>Request is not a valid for XSD rules.</description>
                <details>cvc-datatype-valid.1.2.1: '?' is not a valid value for 'integer'.</details>
                <details>cvc-type.3.1.3: The value '?' of element 'pos:userId' is not valid.</details>
            </ServiceError>
        </detail>
    </SOAP-ENV:Fault>
</SOAP-ENV:Body></SOAP-ENV:Envelope>

在尝试使用FaultException捕获此错误时,我创建了ServiceError类,如下所示,并将其作为TDetail提供给FaultException。但是,我无法捕获此错误的详细信息。它根本不在 catch 块内。我哪里出错了或者你有推荐的方法吗?

        try{
           //here is the code where i got the error
        }
        catch (FaultException<ServiceError> e)
        {
        }
    
        [DataContract]
        public class ServiceError
        {
            [DataMember]
            public string code { get; set; }
            [DataMember]
            public string description { get; set; }
            [DataMember]
            public string[] details { get; set; }
        }

The response of the SOAP request is the following xml output.

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
    <SOAP-ENV:Fault>
        <faultcode>SOAP-ENV:Client</faultcode>
        <faultstring xml:lang="en">INVALID_REQUEST - Request is not a valid for XSD rules.</faultstring>
        <detail>
            <ServiceError xmlns="http://test.test.com/xmlschema/common">
                <code>INVALID_REQUEST</code>
                <description>Request is not a valid for XSD rules.</description>
                <details>cvc-datatype-valid.1.2.1: '?' is not a valid value for 'integer'.</details>
                <details>cvc-type.3.1.3: The value '?' of element 'pos:userId' is not valid.</details>
            </ServiceError>
        </detail>
    </SOAP-ENV:Fault>
</SOAP-ENV:Body></SOAP-ENV:Envelope>

While trying to catch this error with FaultException, I created the ServiceError class as below and gave it as TDetail to FaultException. However, I could not catch the details of this error.It doesn't come inside the catch block at all. Where am I going wrong or is there a method you recommend?

        try{
           //here is the code where i got the error
        }
        catch (FaultException<ServiceError> e)
        {
        }
    
        [DataContract]
        public class ServiceError
        {
            [DataMember]
            public string code { get; set; }
            [DataMember]
            public string description { get; set; }
            [DataMember]
            public string[] details { get; set; }
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笙痞 2025-01-24 22:49:51

您可以尝试以下方法,
消息失败的详细节点应包含 XML。
GetDetail 会将此 XML 反序列化为给定对象。有关详细信息,您可以检查 这篇文章

内容是字符串,代码:

catch (FaultException ex)
{
    MessageFault msgFault = ex.CreateMessageFault();
   var msg = msgFault.GetReaderAtDetailContents().Value;
     //throw Detail
}

如果详细信息不是字符串,代码:

MessageFault msgFault = ex.CreateMessageFault();
XmlReader readerAtDetailContents = msgFault.GetReaderAtDetailContents();
var readOuterXml = readerAtDetailContents.ReadOuterXml(); 
var data = XElement.Parse(readOuterXml);
Dictionary<string, string> element = data.Elements().ToDictionary(elementKey => elementKey.Name.LocalName, elementVal => elementVal.Value, null);

You can try the following method,
The detail node of the message failure should contain XML.
GetDetail will deserialize this XML into the given object.For details you can check this post.

The content is a string, the code:

catch (FaultException ex)
{
    MessageFault msgFault = ex.CreateMessageFault();
   var msg = msgFault.GetReaderAtDetailContents().Value;
     //throw Detail
}

If the details are not strings, the code:

MessageFault msgFault = ex.CreateMessageFault();
XmlReader readerAtDetailContents = msgFault.GetReaderAtDetailContents();
var readOuterXml = readerAtDetailContents.ReadOuterXml(); 
var data = XElement.Parse(readOuterXml);
Dictionary<string, string> element = data.Elements().ToDictionary(elementKey => elementKey.Name.LocalName, elementVal => elementVal.Value, null);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文