gpt4 book ai didi

C# 调用 java web 服务 : cannot catch FaultException

转载 作者:太空宇宙 更新时间:2023-11-03 13:27:12 28 4
gpt4 key购买 nike

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

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

方法调用似乎有效,但当我 try catch 故障时,我无法获得第 3 方服务提供的自定义故障数据。 我在 FaultException 中所能得到的只是“处理时发生错误。”(消息和原因都仅以此设置。)

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

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

你们认为我在搞乱一些可以打败的东西吗?任何建议都非常感谢,因为我已经很沮丧了。这是我的代码的一部分:

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

最佳答案

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

希望对你有帮助。

关于C# 调用 java web 服务 : cannot catch FaultException<CustomType>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22016792/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com