使用来自 WCF 客户端的非 wcf SOAP 错误(定义了肥皂错误) [英] Consuming non-wcf SOAP fault from WCF client (soap fault defined)

查看:22
本文介绍了使用来自 WCF 客户端的非 wcf SOAP 错误(定义了肥皂错误)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从 WCF 客户端调用的非 wcf 服务器,我需要访问注册的soap错误以防服务器抛出它(它包含我需要用户的反馈).我使用了 How to access SOAP 1.1 fault detail from WCF client (no fault contract) 中的示例,但考虑到我很痛苦务必在 wsdl 中定义故障契约,至少按照 SOAP 规范,并且故障包含错误代码和错误字符串.

I have a non-wcf server that I call from WCF client and I need to gain access to registered soap fault in case server throws it (it contains a feedback I need for user). I used the example from How to access SOAP 1.1 fault detail from WCF client (no fault contract) but its a pain given I DO have the fault contract defined in the wsdl, at least per SOAP specification and the fault contains error code and error string.

<wsdl:types>
    <schema elementFormDefault="qualified" targetNamespace="http://www.cisco.com/BTS10200/i01" xmlns="http://www.w3.org/2001/XMLSchema">
        ...
        <complexType name="BtsSoapException">
            <sequence>
                <element name="error_code" type="xsd:int"/>
                <element name="error_string" nillable="true" type="xsd:string"/>
            </sequence>
        </complexType>
        <element name="fault" type="impl:BtsSoapException"/>
...

<wsdl:message name="BtsSoapException">
    <wsdl:part element="impl:fault" name="fault"/>
</wsdl:message>
...

<wsdl:portType name="Bts10200Operations">
    <wsdl:operation name="login">
        <wsdl:input message="impl:loginRequest" name="loginRequest"/>
        <wsdl:output message="impl:loginResponse" name="loginResponse"/>
        <wsdl:fault message="impl:BtsSoapException" name="BtsSoapException"/>
    </wsdl:operation>
...

服务导入正确识别所有这些并生成正确的代码结构:

The service import recognizes all this properly and generates proper code constructs:

[System.Runtime.Serialization.DataContractAttribute(Name="BtsSoapException", Namespace="http://www.cisco.com/BTS10200/i01")]
[System.SerializableAttribute()]
public partial class BtsSoapException : object ...

....

[System.ServiceModel.ServiceContractAttribute(Namespace="http://www.cisco.com/BTS10200/i01", ConfigurationName="CiscoBTSService.Bts10200Operations")]
public interface Bts10200Operations {
    [System.ServiceModel.OperationContractAttribute(Action="", ReplyAction="*")]
    [System.ServiceModel.FaultContractAttribute(typeof(TestCiscoBTS.CiscoBTSService.BtsSoapException), Action="", Name="fault")]
    TestCiscoBTS.CiscoBTSService.loginResponse login(TestCiscoBTS.CiscoBTSService.loginRequest request);
...

当我使用无效帐户调用 login() 时,我确实按照 wsdl 得到了正确的响应:

When I call the login() with invalid account I DO get proper response as per wsdl:

<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <soapenv:Body>
        <soapenv:Fault>
            <faultcode>soapenv:Server.generalException</faultcode>
            <faultstring></faultstring>
            <detail>
                <ns1:fault xmlns:ns1="http://www.cisco.com/BTS10200/i01">
                    <error_code>401</error_code>
                    <error_string xsi:type="xsd:string">java.lang.Exception: No user profile defined in the database for fakeuser</error_string>
                </ns1:fault>
                <ns2:exceptionName xmlns:ns2="http://xml.apache.org/axis/">com.sswitch.oam.soap.intf.BtsSoapException</ns2:exceptionName>
                <ns3:stackTrace xmlns:ns3="http://xml.apache.org/axis/">
    at com.sswitch.oam.soap.impl.UserAuth.validateUser(UserAuth.java:63)
    ...

FaultExcpetion 确实会触发,但是它是空白的 (Message = ""),我没有看到任何暴露 BtsSoapException 实例的属性.就像它根本没有被反序列化一样(即使 wcf 知道它需要做的一切).我究竟做错了什么?如何让 WCF 给我 FaultException?

The FaultExcpetion does fire, however its blank (Message = "") and I do not see any property exposing BtsSoapException instance. Like it hasn't been deserialized at all (even though wcf knows everything it needs to do it). What am I doing wrong? How do I make WCF give me FaultException<BtsSoapException>?

推荐答案

您是否尝试过执行类似下面显示的代码的操作,您需要捕获包含您正在寻找的信息的特定soap 故障合同.这篇 MSDN 文章 中的信息应该会有所帮助.>

Have your tried doing something like the code shown below, you need to catch the specific soap fault contract that has the information you are looking for. The information in this MSDN article should help.

var proxy = new WhatEverYourServiceProxyClassIsNamed();
try
{
    //your call logic here

    proxy.Close();
}
catch (FaultException<BtsSoapException> bse)
{
    //Get the info you're looking for in the strongly typed soap fault:

    proxy.Abort();
}
catch (FaultException fe)
{
    //Do something appropriate for a non-typed soap fault

    proxy.Abort();
}
finally
{
    if ((ICommunicationObject)proxy).State != CommunicationState.Closed)
        proxy.Abort();
}

这篇关于使用来自 WCF 客户端的非 wcf SOAP 错误(定义了肥皂错误)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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