简体   繁体   English

肥皂客户端返回无法破坏响应主体 netcore

[英]soap client return cannot destabilize the response body netcore

I have a dotnet app that consumes a SOAP service, the problem is when I call the service immediately return an exception said cant read the resulting body .我有一个使用 SOAP 服务的 dotnet 应用程序,问题是当我调用该服务时立即返回一个异常,表示无法读取结果正文

I tried to call the service through SOAP-UI, it worked very well.我尝试通过 SOAP-UI 调用该服务,效果很好。

below is the code was written to use the SOAP client.下面是为使用 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));

And this is the code was generated from VS for the client :这是从 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));
        }
    }
}

The expected XML response must be as the following:预期的 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>

the data type that are return from the service are not allowed to mapped to the response for example if you have declared in response will return long and you are return decimal it couldn't be serialised from the response从服务返回的数据类型不允许映射到响应,例如,如果您在响应中声明将返回long并且您返回的是decimal则无法从响应中序列化

private long permitSequenceField;

private long sequenceExceptionField;

the permitSequenceField and sequenceExceptionField are long you cant receive them as any another date type in xmlserializer permitSequenceField 和 sequenceExceptionField 很长,您无法将它们作为 xmlserializer 中的任何其他日期类型接收

hope this solve your problem .希望这能解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM