从WCF客户端消耗非wcf SOAP错误(定义了soap fault)

时间:2011-12-06 09:44:07

标签: c# wcf soap wcf-client

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

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

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

[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获得正确的回复:

<?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 =&#34;&#34;)并且我没有看到任何暴露BtsSoapException实例的属性。就像它没有被反序列化一样(即使wcf知道它需要做的一切)。我究竟做错了什么?如何让WCF给我FaultException<BtsSoapException>

2 个答案:

答案 0 :(得分:2)

如果您尝试过类似下面显示的代码,您需要捕获具有您正在寻找的信息的特定肥皂错误合同。 this MSDN article中的信息应该会有所帮助。

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();
}

答案 1 :(得分:0)

我认为Sixto Saez是对的。您可以在FaultException中看到BtsSoapException实例。

定义:

<complexType name="BtsSoapException">
    <sequence>
        <element name="error_code" type="xsd:int"/>
        <element name="error_string" nillable="true" type="xsd:string"/>
    </sequence>
</complexType>

响应:

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

如果你这样做

catch (FaultException<BtsSoapException> bse)
{
    console.write(bse.Detail.error_string)
}

在调试时,必须在输出窗口中看到“java.lang.Exception:数据库中没有为fakeuser定义用户配置文件”。