0

我使用本地 WSDL 向我的项目添加了 SAP Netweaver 服务引用。服务器返回一个空响应,但使用 Fiddler 我可以看到正在发送正确的响应。我认为这意味着响应没有被正确反序列化。这就是在 WSDL 中定义根响应元素的方式:

<xsd:element name="adrl">
    <xsd:complexType>
        <xsd:sequence>
            <xsd:element ref="adr" minOccurs="0" maxOccurs="unbounded"/>
        </xsd:sequence>
    </xsd:complexType>
</xsd:element>

子元素(省略了一些字段):

<xsd:element name="adr">
    <xsd:complexType>
        <xsd:sequence>
            <xsd:element ref="id"/>
        </xsd:sequence>
        <xsd:attribute name="op" use="required">
            <xsd:simpleType>
                <xsd:restriction base="xsd:NMTOKEN">
                    <xsd:enumeration value="update"/>
                    <xsd:enumeration value="delete"/>
                </xsd:restriction>
            </xsd:simpleType>
        </xsd:attribute>
    </xsd:complexType>
</xsd:element>

回复:

<wsdl:output message="p1:p2.adrl"/>

命名空间:

xmlns:p1="urn:mycompany:outbound"
xmlns:p2="http://tempuri.org/mycompany" 

来自服务的 SOAP 响应:

<SOAP:Envelope xmlns:SOAP='http://schemas.xmlsoap.org/soap/envelope/'>
    <SOAP:Header/>
    <SOAP:Body>
        <adrl>
            <adr op='update'>
                <id>9AF1FBA0-81A4-4427-A011-0DCE3BD1F609</id>
            </adr>
        </adrl>
    </SOAP:Body>
</SOAP:Envelope>

WCF 类定义(来自 Reference.cs):

// Some namespaces elided

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.17929")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://tempuri.org/mycompany")]
public partial class adr : object, System.ComponentModel.INotifyPropertyChanged {

    private string idField;

    // adrOP has a suitable enumeration defined elsewhere in the file
    private adrOP opField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Order=0)]
    public string id {
        get {
            return this.idField;
        }
        set {
            this.idField = value;
            this.RaisePropertyChanged("id");
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public adrOP op {
        get {
            return this.opField;
        }
        set {
            this.opField = value;
            this.RaisePropertyChanged("op");
        }
    }

    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.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
[System.ServiceModel.MessageContractAttribute(IsWrapped=false)]
public partial class GetAdrResponse {

    [System.ServiceModel.MessageBodyMemberAttribute(Namespace="http://tempuri.org/mycompany", Order=0)]
    [System.Xml.Serialization.XmlArrayItemAttribute("adr", IsNullable=false)]
    public adr[] adrl;

    public GetAdrResponse() {
    }

    public GetAdrResponse(adr[] adrl) {
        this.adrl = adrl;
    }
}

当我尝试手动反序列化我在 Fiddler 中捕获的响应(以 adrl 作为根元素)时,我得到了内部异常消息“”的 Xml 错误不是预期的。如果我向序列化程序添加根元素,错误就会消失,但只有“op”属性被正确反序列化。其余字段为空。

可能是什么问题呢?我真的无法访问该服务,但如果那里有什么需要改变的地方,我可以转发一个请求。否则,我正在考虑添加一个 MessageInspector 并修改响应,但我不确定应该以哪种方式修改它。

编辑:我在 C# 中创建了对象并对其进行了反序列化,结果如下:

<?xml version="1.0" encoding="utf-16"?>
<ArrayOfAdr xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <adr op="update">
    <id>9AF1FBA0-81A4-4427-A011-0DCE3BD1F609</id>
  </adr>
</ArrayOfAdr>

似乎根元素在序列化方面没有正确定义。

4

1 回答 1

1

当我尝试在 SAP Netweaver EJB DC 中使用 WCF 服务时,我遇到了同样的问题。我通过将 XmlSerializerFormat 标记添加到 WCF 服务中的每个操作并将 XmlRoot 标记添加到数据协定来解决了这个问题:

    [XmlRoot(Namespace = "urn:sap.com:WS:Service1", ElementName = "Entity1")]
    [DataContract]
    public class Entity1
    {
    // your properties
    }

    [ServiceContract(Namespace = "urn:sap.com:WS:Service1", Name = "IService1")]
    public interface IService1
    {
        [XmlSerializerFormat(Style = OperationFormatStyle.Document, Use = OperationFormatUse.Literal)]
        [OperationContract]
        Entity1 GetById(int id);

      [XmlSerializerFormat(Style = OperationFormatStyle.Document, Use = OperationFormatUse.Literal)] 
        [OperationContract]
        string Save(Entity1 entity);
    }

    [ServiceBehavior(Namespace = "urn:sap.com:WS:Service1", Name = "Service1")]
    [BindingNamespaceBehavior(bindingNamespace = "urn:sap.com:WS:Service1")]
    public class Service1 : IService1
    {
    Entity1 GetById(int id)
    {
        // your code
    }

    string Save(Entity1 entity)
    {
       // your code
    }
    }
于 2013-12-04T09:56:43.310 回答