肥皂客户的退货不会破坏响应主体网核的稳定性

时间:2019-12-19 13:06:03

标签: c# asp.net xml soap

我有一个使用SOAP服务的dotnet应用,问题是当我调用该服务时立即返回一个异常,该异常说 不能读取生成的正文

我试图通过SOAP-UI调用该服务,效果很好。

下面是编写使用SOAP客户端的代码。

        var request = new testRequest
        {
            moduleC = model.ModuleC.ToString(),
            moduleT = model.ModuleT.ToString(),
            Number = model.Number.ToString(),
        };
        var response = GetSoapResponse<TestRequest, TestResponse>((p) =>
        {
            var result = _client.TestMethod(request);
            return result;
        }, request);


private TResult GetSoapResponse<TParameters, TResult>(Func<TParameters, TResult> callback, TParameters parameters)
    {
        using (new OperationContextScope(_client.InnerChannel))
        {
            SetCredentials();
            var result = callback.Invoke(parameters);
            return result;
        }
    }

protected void SetCredentials() => OperationContext.Current.OutgoingMessageHeaders.Add(
                new UDBSecurityHeader(
                    Options.Username,
                    Options.Password));

这是从VS为客户端生成的代码:

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.8.3752.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://www.sampple.com/test")]
public partial class TestResponse : object, System.ComponentModel.INotifyPropertyChanged {

    private TestResponseData dataField;

    private string statusCodeField;

    private string[] errorsField;

    private string isSucceedField;

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

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(DataType="integer", Order=1)]
    public string statusCode {
        get {
            return this.statusCodeField;
        }
        set {
            this.statusCodeField = value;
            this.RaisePropertyChanged("statusCode");
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("errors", Order=2)]
    public string[] errors {
        get {
            return this.errorsField;
        }
        set {
            this.errorsField = value;
            this.RaisePropertyChanged("errors");
        }
    }

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

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

public partial class TestResponseData : object, System.ComponentModel.INotifyPropertyChanged {

    private long permitSequenceField;

    private bool permitSequenceFieldSpecified;

    private long sequenceExceptionField;

    private bool sequenceExceptionFieldSpecified;

    private string statusField;

    public long permitSequence {
        get {
            return this.permitSequenceField;
        }
        set {
            this.permitSequenceField = value;
            this.RaisePropertyChanged("permitSequence");
        }
    }

    public bool permitSequenceSpecified {
        get {
            return this.permitSequenceFieldSpecified;
        }
        set {
            this.permitSequenceFieldSpecified = value;
            this.RaisePropertyChanged("permitSequenceSpecified");
        }
    }

    public long sequenceException {
        get {
            return this.sequenceExceptionField;
        }
        set {
            this.sequenceExceptionField = value;
            this.RaisePropertyChanged("sequenceException");
        }
    }

    public bool sequenceExceptionSpecified {
        get {
            return this.sequenceExceptionFieldSpecified;
        }
        set {
            this.sequenceExceptionFieldSpecified = value;
            this.RaisePropertyChanged("sequenceExceptionSpecified");
        }
    }

    public string status {
        get {
            return this.statusField;
        }
        set {
            this.statusField = value;
            this.RaisePropertyChanged("status");
        }
    }

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

预期的XML响应必须如下:

<tns:TestResponse xmlns:tns="http://www.sampple.com/test">
     <tns:data>
        <tns:permitSequence>0.0</tns:permitSequence>
        <tns:sequenceException>0.0</tns:sequenceException>
        <tns:status>0</tns:status>
     </tns:data>
     <tns:statusCode>200</tns:statusCode>
     <tns:errors/>
     <tns:isSucceed>true</tns:isSucceed>

1 个答案:

答案 0 :(得分:2)

不允许从服务返回的数据类型映射到响应,例如,如果您已声明响应将返回long,而您返回decimal,则无法序列化从响应

private long permitSequenceField;

private long sequenceExceptionField;

permitSequenceField和sequenceExceptionField很长,您无法以xmlserializer中的任何其他日期类型接收它们

希望这可以解决您的问题。