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.
I tried to call the service through SOAP-UI, it worked very well.
below is the code was written to use the SOAP client.
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 :
[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:
<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
private long permitSequenceField;
private long sequenceExceptionField;
the permitSequenceField and sequenceExceptionField are long you cant receive them as any another date type in xmlserializer
hope this solve your problem .
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With