4

我正在开发一个 WCF 应用程序,我的一项服务需要使用用 Java 开发的第 3 方 Web 服务。第三方服务由政府提供,几乎不可能获得其代码/要求更改。

我从它的 wsdl 中将它添加为服务引用,并且该引用是自动生成的。

方法调用似乎有效,但是当我尝试捕获故障时,我无法获取第 3 方服务提供的自定义故障数据。我能在 FaultException 中得到的只是“处理时发生故障”。(message 和 reason 都只设置了这个。)

wsdl 具有自定义错误类型,但看起来 .NET 无法将返回的 SOAP 响应映射到具有该类型的 FaultException。所以,我的代码只是得到一个带有通用消息和 HasDetails=false 的通用 FaultException。

嗯,这对我们来说是个问题,因为我们有义务向我们的客户展示政府的网络服务返回了什么类型的故障。

你们觉得我在搞什么可打败的事情吗?任何建议都会受到高度赞赏,因为我已经很沮丧了。这是我的代码中的一部分:

try
{
    //this is the 3rd party Java web service
    EFaturaPortTypeClient efService = new EFaturaPortTypeClient();

    //setting here the object to be sent to the 3rd party web service

    var returnVal = efService.sendDocument(docRequest);
}
catch (FaultException<EFaturaFaultType> ex)
{
    //need to get the "Reason" or "Detail" from the returned soap fault
    //but cannot even get in here
}
catch (FaultException ex)
{
    //this is where the debug goes

    //tried below method, but retrieves nothing worthy
    MessageFault mf = ex.CreateMessageFault();

    throw new FaultException<EFaturaFaultType>(mf.GetDetail<EFaturaFaultType>());
}

这是我嗅到的错误的 SOAP 响应:

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
   <soap:Body>
      <soap:Fault>
         <soap:Code>
            <soap:Value>soap:Receiver</soap:Value>
         </soap:Code>
         <soap:Reason>
            <soap:Text xml:lang="en">2006:GECERSIZ ZARF ADI</soap:Text>
         </soap:Reason>
         <soap:Detail>
            <ns3:EFaturaFault xmlns:ns3="http://gib.gov.tr/vedop3/eFatura" xmlns:xmime="http://www.w3.org/2005/05/xmlmime">
               <code>2006</code>
               <msg>GECERSIZ ZARF ADI</msg>
            </ns3:EFaturaFault>
         </soap:Detail>
      </soap:Fault>
   </soap:Body>
</soap:Envelope>

最后是第 3 方 Web 服务的 wsdl:

<?xml version='1.0' encoding='UTF-8'?><wsdl:definitions name="EFatura" targetNamespace="http://gib.gov.tr/vedop3/eFatura" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:tns="http://gib.gov.tr/vedop3/eFatura" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <wsdl:types>
<xs:schema targetNamespace="http://www.w3.org/2005/05/xmlmime" version="1.0" xmlns:tns="http://www.w3.org/2005/05/xmlmime" xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <xs:complexType name="base64Binary">
      <xs:simpleContent>
         <xs:extension base="xs:base64Binary">
            <xs:attribute ref="tns:contentType" />
         </xs:extension>
      </xs:simpleContent>
   </xs:complexType>
   <xs:complexType name="hexBinary">
      <xs:simpleContent>
         <xs:extension base="xs:string">
            <xs:attribute ref="tns:contentType" />
         </xs:extension>
      </xs:simpleContent>
   </xs:complexType>
   <xs:attribute name="contentType" type="xs:string" />
</xs:schema><xs:schema targetNamespace="http://gib.gov.tr/vedop3/eFatura" version="1.0" xmlns:ns1="http://www.w3.org/2005/05/xmlmime" xmlns:tns="http://gib.gov.tr/vedop3/eFatura" xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <xs:import namespace="http://www.w3.org/2005/05/xmlmime" />
   <xs:element name="EFaturaFault" type="tns:EFaturaFaultType" />
   <xs:element name="documentRequest" type="tns:documentType" />
   <xs:element name="documentResponse" type="tns:documentReturnType" />
   <xs:element name="getAppRespRequest" type="tns:getAppRespRequestType" />
   <xs:element name="getAppRespResponse" type="tns:getAppRespResponseType" />
   <xs:complexType name="getAppRespRequestType">
      <xs:sequence>
         <xs:element minOccurs="0" name="instanceIdentifier" type="xs:string" />
      </xs:sequence>
   </xs:complexType>
   <xs:complexType name="getAppRespResponseType">
      <xs:sequence>
         <xs:element minOccurs="0" name="applicationResponse" type="xs:string" />
      </xs:sequence>
   </xs:complexType>
   <xs:complexType name="EFaturaFaultType">
      <xs:sequence>
         <xs:element minOccurs="0" name="code" type="xs:int" />
         <xs:element minOccurs="0" name="msg" type="xs:string" />
      </xs:sequence>
   </xs:complexType>
   <xs:complexType name="documentType">
      <xs:sequence>
         <xs:element minOccurs="0" name="fileName" type="xs:string" />
         <xs:element minOccurs="0" name="binaryData" type="ns1:base64Binary" />
         <xs:element minOccurs="0" name="hash" type="xs:string" />
      </xs:sequence>
   </xs:complexType>
   <xs:complexType name="documentReturnType">
      <xs:sequence>
         <xs:element minOccurs="0" name="msg" type="xs:string" />
         <xs:element minOccurs="0" name="hash" type="xs:string" />
      </xs:sequence>
   </xs:complexType>
</xs:schema>  </wsdl:types>
  <wsdl:message name="EFaturaFaultMessage">
    <wsdl:part element="tns:EFaturaFault" name="EFaturaFaultMessage">
    </wsdl:part>
  </wsdl:message>
  <wsdl:message name="sendDocument">
    <wsdl:part element="tns:documentRequest" name="document">
    </wsdl:part>
  </wsdl:message>
  <wsdl:message name="getApplicationResponse">
    <wsdl:part element="tns:getAppRespRequest" name="getApplicationResponseRequestPart">
    </wsdl:part>
  </wsdl:message>
  <wsdl:message name="getApplicationResponseResponse">
    <wsdl:part element="tns:getAppRespResponse" name="getApplicationResponseResponsePart">
    </wsdl:part>
  </wsdl:message>
  <wsdl:message name="sendDocumentResponse">
    <wsdl:part element="tns:documentResponse" name="sendDocumentReturn">
    </wsdl:part>
  </wsdl:message>
  <wsdl:portType name="EFaturaPortType">
    <wsdl:operation name="getApplicationResponse">
      <wsdl:input message="tns:getApplicationResponse" name="getApplicationResponse">
    </wsdl:input>
      <wsdl:output message="tns:getApplicationResponseResponse" name="getApplicationResponseResponse">
    </wsdl:output>
      <wsdl:fault message="tns:EFaturaFaultMessage" name="EFaturaFaultMessage">
    </wsdl:fault>
    </wsdl:operation>
    <wsdl:operation name="sendDocument">
      <wsdl:input message="tns:sendDocument" name="sendDocument">
    </wsdl:input>
      <wsdl:output message="tns:sendDocumentResponse" name="sendDocumentResponse">
    </wsdl:output>
      <wsdl:fault message="tns:EFaturaFaultMessage" name="EFaturaFaultMessage">
    </wsdl:fault>
    </wsdl:operation>
  </wsdl:portType>
  <wsdl:binding name="EFaturaSoapBinding" type="tns:EFaturaPortType">
    <soap12:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" />
    <wsdl:operation name="getApplicationResponse">
      <soap12:operation soapAction="getApplicationResponse" style="document" />
      <wsdl:input name="getApplicationResponse">
        <soap12:body use="literal" />
      </wsdl:input>
      <wsdl:output name="getApplicationResponseResponse">
        <soap12:body use="literal" />
      </wsdl:output>
      <wsdl:fault name="EFaturaFaultMessage">
        <soap12:fault name="EFaturaFaultMessage" use="literal" />
      </wsdl:fault>
    </wsdl:operation>
    <wsdl:operation name="sendDocument">
      <soap12:operation soapAction="sendDocument" style="document" />
      <wsdl:input name="sendDocument">
        <soap12:body use="literal" />
      </wsdl:input>
      <wsdl:output name="sendDocumentResponse">
        <soap12:body use="literal" />
      </wsdl:output>
      <wsdl:fault name="EFaturaFaultMessage">
        <soap12:fault name="EFaturaFaultMessage" use="literal" />
      </wsdl:fault>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:service name="EFatura">
    <wsdl:port binding="tns:EFaturaSoapBinding" name="EFaturaSoap12">
      <soap12:address location="https://merkeztest.efatura.gov.tr/EFaturaMerkez/services/EFatura" />
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>

编辑:来自自动生成的 Reference.cs 的相关部分:

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.18408")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://gib.gov.tr/vedop3/eFatura", TypeName = "EFaturaFaultType")]
public partial class EFaturaFaultType : object, System.ComponentModel.INotifyPropertyChanged {

    private int codeField;

    private bool codeFieldSpecified;

    private string msgField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, Order=0)]
    public int code {
        get {
            return this.codeField;
        }
        set {
            this.codeField = value;
            this.RaisePropertyChanged("code");
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool codeSpecified {
        get {
            return this.codeFieldSpecified;
        }
        set {
            this.codeFieldSpecified = value;
            this.RaisePropertyChanged("codeSpecified");
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, Order=1)]
    public string msg {
        get {
            return this.msgField;
        }
        set {
            this.msgField = value;
            this.RaisePropertyChanged("msg");
        }
    }

    public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

    protected void RaisePropertyChanged(string propertyName) {
        System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
        if ((propertyChanged != null)) {
            propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
        }
    }
}

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0"
[System.ServiceModel.ServiceContractAttribute(Namespace="http://gib.gov.tr/vedop3/eFatura", ConfigurationName="EFService.EFaturaPortType")]
public interface EFaturaPortType {

    [System.ServiceModel.OperationContractAttribute(Action="sendDocument", ReplyAction="*")]
    [System.ServiceModel.FaultContractAttribute(typeof(BF.EFService.EFaturaFaultType), Action="sendDocument", Name="EFaturaFault")]
    [System.ServiceModel.XmlSerializerFormatAttribute(SupportFaults=true)]
    BF.EFService.sendDocumentResponse sendDocument(BF.EFService.sendDocument request);
4

2 回答 2

1

实际上,我通过右键单击 Visual Studio 解决方案资源管理器中的服务引用节点并选中框Always generate message contract来解决这个问题。不要问我是怎么到那里的……我只能在我知道的情况下解释。之后生成的类和方法发生变化,问题就消失了。虽然,显然,它仍然没有具体说明由通过生成的服务代理传递的空参数引起的错误。

我希望它有所帮助。

于 2014-02-27T13:44:24.400 回答
1

EFaturaFaultType我认为您可能需要定义具有该属性的类(如果尚未定义),并使用该DataContract属性标记任何可序列化的成员DataMember

以下链接提供了 WCF 错误处理的可靠概述。 http://www.codeproject.com/Articles/26320/WCF-Error-Handling-and-Fault-Conversion

问候,

于 2014-02-25T18:49:32.170 回答