C#调用java web服务:无法捕获FaultException <customtype> </customtype>

时间:2014-02-25 14:15:47

标签: c# web-services wcf soap faultexception

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

我将其作为服务引用添加到其wsdl中,并自动生成引用。

方法调用似乎有效,但是当我尝试捕获错误时,我无法获取第三方服务提供的自定义错误数据。 我在FaultException中可以得到的是“处理时发生错误。”(消息和原因都只设置了这个。)

wsdl具有自定义错误类型,但看起来.NET无法将返回的SOAP响应映射到具有该类型的FaultException。因此,我的代码只获得一个通用的FaultException,其中包含一个通用消息,并且HasDetails = false。

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

你们觉得我搞砸了一些可以打败的东西吗?任何建议都受到高度赞赏,因为我已经感到沮丧。这是我代码中的一块:

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>

最后这是第三方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);

2 个答案:

答案 0 :(得分:1)

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

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

此致

答案 1 :(得分:0)

我实际上通过右键单击Visual Studio中解决方案资源管理器中的服务引用节点并选中始终生成消息合同框来解决此问题。不要问我怎么到那里......我只能解释我是否知道。之后生成的类和方法会发生变化,问题就会消失。虽然,显然,它仍然不是特定于由生成的服务代理传递的空参数引起的错误。

我希望它有所帮助。