'SOAP request with WEB API

There is a SOAP web service and I'm accessing it with my WEB API.

Here is the SOAP xml request.

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAPENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchemainstance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SOAP-ENV:Body>
    <UserCredential>
        <Username>admin</Username>
        <Password>Admin123</Password>
    </UserCredential>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Here is the Class, that I'm using.

   [XmlRoot(ElementName="UserCredential")]
   public class UserCredential
   {
      [XmlElement(ElementName="Username")] 
      public string Username { get; set; } 

      [XmlElement(ElementName="Password")] 
      public string Password { get; set; } 
  }

API reply is something like below

 <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header/>
 <SOAP-ENV:Body>
     <UserCredentialReply>
        <AccessAlow>true</AccessAlow>
     </UserCredentialReply>
 </SOAP-ENV:Body>
 </SOAP-ENV:Envelope>

It's C# class,

[XmlRoot(ElementName="UserCredentialReply")]
public class UserCredentialReply { 

    [XmlElement(ElementName="AccessAlow")] 
    public bool AccessAlow { get; set; } 
}

Here I call the API,

using (var client = new HttpClient())
{
    var firstLine = "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:SOAPENC=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchemainstance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><SOAP-ENV:Body>";
    var lastLine = "</SOAP-ENV:Body></SOAP-ENV:Envelope>";

    var request = new HttpRequestMessage(HttpMethod.Post, requestUrl);

    string strRequest = "";
    XmlSerializer xsSubmit = new XmlSerializer(typeof(UserCredential));
    using (var strWriter = new StringWriter())
    {
        using (XmlWriter writer = XmlWriter.Create(strWriter))
        {
            xsSubmit.Serialize(writer, cred);
            strRequest = strWriter.ToString();
        }
    }
    
    strRequest = strRequest.Replace("<?xml version=\"1.0\" encoding=\"utf-16\"?>", "");//POINT 1
    var xmlString = firstLine + strRequest + lastLine; // POINT 2
    var content = new StringContent(xmlString, Encoding.UTF8, MediaTypeNames.Application.Xml);
    request.Content = content;
    var res = client.SendAsync(request).Result;
    
    if (res.IsSuccessStatusCode)
    {
        var returnedString = res.Content.ReadAsStringAsync().Result;
        var reply = DeserializeXml<UserCredentialReply>(returnedString);//POINT 3
    }

}

This is how I deserialize,

    private T DeserializeXml<T>(string responseObjStr)
    {
        var serializer = new XmlSerializer(typeof(T));
        using (var stringReader = new StringReader(responseObjStr))
        {
            return (T)serializer.Deserialize(stringReader);
        }
    }

POINT 1 : As you can see when serializing object to the XML will generate, "<?xml version="1.0" encoding="utf-16"?>" part and should remove it for getting proper response. This is ugly.

POINT 2 : SOAP-ENV tags should send along with the request for getting a successful response. This also not upto standard.

POINT 3 : Response comes with SOAP-ENV tags. So it cannot deserialize properly.

Is there any proper way to use SOAP web services with WEB API project? How can I clean the code for above 3 points?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source