服务引用方法返回null

时间:2015-11-02 10:16:22

标签: c# soap

当我调用SOAP方法时,在C#中它返回null,但在Fiddler中我看到实际上服务器返回了一些数据。

WSDL的片段:

<xsd:element name="GetRulesResponse">
    <xsd:complexType>
        <xsd:sequence>
            <xsd:element name="rules" type="tns:ArrayOfRulesType"/>
        </xsd:sequence>
    </xsd:complexType>
</xsd:element>

<xsd:complexType name="ArrayOfRulesType">
    <xsd:sequence>
        <xsd:element name="rule" type="tns:RulesType" maxOccurs="unbounded" minOccurs="0"/>
    </xsd:sequence>
</xsd:complexType>

<xsd:complexType name="RulesType">
    <xsd:sequence>
        <xsd:element name="field1" type="xsd:string"/>
        <xsd:element name="field2" type="xsd:string"/>
        <xsd:element name="field3" type="xsd:long"/>
    </xsd:sequence>
</xsd:complexType>

实际服务器响应:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:gam="http://some.url/">
    <soap:Body>
        <gam:GetRulesResponse>
            <rule>
                <gam:field1>sss</gam:field1>
                <gam:field2>sss</gam:field2>
                <gam:field3>100</gam:field3>
            </rule>
        </gam:GetRulesResponse>
    </soap:Body>
</soap:Envelope>

C#生成的代码:

[System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
    [System.Runtime.Serialization.CollectionDataContractAttribute(Name="ArrayOfRulesType", Namespace="http://some.url", ItemName="rule")]
    [System.SerializableAttribute()]
    public class ArrayOfRulesType : System.Collections.Generic.List<Namespaces.RulesType> {
    }

    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
    [System.Runtime.Serialization.DataContractAttribute(Name="RulesType", Namespace="http://some.url")]
    [System.SerializableAttribute()]
    public partial class RulesType : object, System.Runtime.Serialization.IExtensibleDataObject, System.ComponentModel.INotifyPropertyChanged {

        [System.NonSerializedAttribute()]
        private System.Runtime.Serialization.ExtensionDataObject extensionDataField;

        private string field1Field;

        private string field2Field;

        private long field3Field;


        [System.Runtime.Serialization.DataMemberAttribute(IsRequired=true, EmitDefaultValue=false)]
        public string field1 {
            get {
                return this.field1Field;
            }
            set {
                if ((object.ReferenceEquals(this.field1Field, value) != true)) {
                    this.field1Field= value;
                    this.RaisePropertyChanged("field1");
                }
            }
        }

        [System.Runtime.Serialization.DataMemberAttribute(IsRequired=true, EmitDefaultValue=false)]
        public string field2 {
            get {
                return this.field2Field;
            }
            set {
                if ((object.ReferenceEquals(this.field2Field, value) != true)) {
                    this.field2Field = value;
                    this.RaisePropertyChanged("field2");
                }
            }
        }

        [System.Runtime.Serialization.DataMemberAttribute(IsRequired=true, Order=2)]
        public long field3 {
            get {
                return this.field3Field;
            }
            set {
                if ((this.field3Field.Equals(value) != true)) {
                    this.field3Field = value;
                    this.RaisePropertyChanged("field3");
                }
            }
        }

        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 GetRulesResponse {

        [System.ServiceModel.MessageBodyMemberAttribute(Name="GetRulesResponse", Namespace="http://some.url", Order=0)]
        public Namespace.GetRulesResponseBody Body;

        public GetRulesResponse() {
        }

        public GetRulesResponse(Namespace.GetRulesResponseBody Body) {
            this.Body = Body;
        }
    }

    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
    [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
    [System.Runtime.Serialization.DataContractAttribute(Namespace="http://some.url")]
    public partial class GetRulesResponseBody {

        [System.Runtime.Serialization.DataMemberAttribute(EmitDefaultValue=false, Order=0)]
        public Namespace.ArrayOfRulesType rules;

        public GetRulesResponseBody() {
        }

        public GetRulesResponseBody(Namespace.ArrayOfRulesType rules) {
            this.rules = rules;
        }
    }

这是第三方服务,所以我无法访问来源。

我该怎么做才能解决它?

1 个答案:

答案 0 :(得分:1)

在解析期间接收响应时检查xml是否已正确解析。如果没有,那么你得到空值。