I require developing a .NET 4.5 Client for a SOAP based web service. The problem is the company who are developing these SOAP based services do not provide WSDLs. However they do provide the request response schemas (XSD files). Since there are no WSDLs I'm unable to add a web reference and get the client proxy code autogenerated.
Are there any .NET 4.5 libraries out there that I can use to make these SOAP base service calls? It needs to support SOAP 1.1 and SOAP attachements too.
In the Logic tab, open the Integrations folder. Right-click the SOAP element and select Consume SOAP Web Service.... In the displayed dialog, specify the location of the Web Service definition (WSDL) and click Consume. When providing a remote URL, type it exactly the same way as you would type it into your web browser.
In the IDE I can generate the required code for the webservice call using the provided 'generate code: web service invoke operation' feature. This has added a @WebServiceRef(wsdlLocation = "WEB-INF/wsdl/servicename. wsdl") and some code to create a port and call the target webservice operation.
If for some reason you don't want to create the WSDL file, the example below could be used to manually construct a SOAP HTTP request:
var url = Settings.Default.URL; //'Web service URL'
var action = Settings.Default.SOAPAction; //the SOAP method/action name
var soapEnvelopeXml = CreateSoapEnvelope();
var soapRequest = CreateSoapRequest(url, action);
InsertSoapEnvelopeIntoSoapRequest(soapEnvelopeXml, soapRequest);
using (var stringWriter = new StringWriter())
{
using (var xmlWriter = XmlWriter.Create(stringWriter))
{
soapEnvelopeXml.WriteTo(xmlWriter);
xmlWriter.Flush();
}
}
// begin async call to web request.
var asyncResult = soapRequest.BeginGetResponse(null, null);
// suspend this thread until call is complete. You might want to
// do something usefull here like update your UI.
var success = asyncResult.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(5));
if (!success) return null;
// get the response from the completed web request.
using (var webResponse = soapRequest.EndGetResponse(asyncResult))
{
string soapResult;
var responseStream = webResponse.GetResponseStream();
if (responseStream == null)
{
return null;
}
using (var reader = new StreamReader(responseStream))
{
soapResult = reader.ReadToEnd();
}
return soapResult;
}
private static HttpWebRequest CreateSoapRequest(string url, string action)
{
var webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Headers.Add("SOAPAction", action);
webRequest.ContentType = "text/xml;charset=\"utf-8\"";
webRequest.Accept = "text/xml";
webRequest.Method = "POST";
return webRequest;
}
private static XmlDocument CreateSoapEnvelope()
{
var soapEnvelope = new XmlDocument();
soapEnvelope.LoadXml(Settings.Default.SOAPEnvelope); //the SOAP envelope to send
return soapEnvelope;
}
private static void InsertSoapEnvelopeIntoSoapRequest(XmlDocument soapEnvelopeXml, HttpWebRequest webRequest)
{
using (Stream stream = webRequest.GetRequestStream())
{
soapEnvelopeXml.Save(stream);
}
}
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