我正在开发一个WCF应用程序,其中一个服务需要使用用Java开发的第三方web服务。第三方服务由政府提供,几乎不可能达到其代码/要求更改。
我从wsdl中将其添加为服务引用,该引用将自动生成。
方法调用似乎有效,但当我试图捕捉故障时,我无法获得第三方服务提供的自定义故障数据我在FaultException中只能得到"Fault occured which processing"(处理时发生故障)。(消息和原因都只能用这个设置。)
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>
最后是第三方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);
我实际上通过右键单击Visual Studio中解决方案资源管理器中的服务引用节点并选中"始终生成消息契约"框来解决了这个问题。不要问我是怎么到那里的。。。我只能在知道的情况下解释。生成的类和方法在那之后发生了更改,问题就消失了。尽管显然,它仍然没有针对通过生成的服务代理传递的空参数所导致的错误。
我希望它能有所帮助。
我认为您可能需要定义具有DataContract
属性的EFaturaFaultType
类,并使用DataMember
属性标记任何可序列化成员。
以下链接提供了WCF错误处理的完整概述。http://www.codeproject.com/Articles/26320/WCF-Error-Handling-and-Fault-Conversion
问候,