Search code examples
c#xmlvisual-studio-2012soapsoap-client

Calling SOAP Service in Visual Studio 2012 without Service Reference


The proxy generation is flawed when I add a service reference, so I have to dropdown to using XML / constructing soap envelopes etc.

What are the best classes to use for this purpose?

Currently I am using the WebClient class to try and send the HTTP request with the soap envelope as the payload and a soap action header etc. but are there other classes I am unaware of? e.g. Class for creating a soap envelope wrapper? Class for creating a SoapClient?

private string SendServiceCall(string action, XElement requestData, string url)
        {
            //load our cert


                string response = "";
                //MyWebClient : System.Net.WebClient
                using (MyWebClient client = new MyWebClient(signingCertificate)) {

                    client.Headers.Add("SOAPAction", @"http://tempuri.org/HelloWorld");
                    client.Headers.Add("Content-Type", "text/xml; charset=utf-8");

                    string payload = @"<?xml version=""1.0"" encoding=""utf-8""?><soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema""><soap:Body>" + requestData.ToString() + "</soap:Body></soap:Envelope>";

                    //log request
                    System.IO.File.AppendAllText("c:/temp/request.xml", payload);
                    byte[] data = Encoding.UTF8.GetBytes(payload);



                    //parse response
                    try {
                        byte[] result = client.UploadData(url, data);
                        response = Encoding.Default.GetString(result);
                        System.IO.File.AppendAllText(@"c:/temp/response.xml", payload);
                    } catch (Exception ex) {

                        Log(String.Format("Send Service Call Failed Action: {0} URL: {1} Message: {2}",action,url,ex.Message));

                    }


                }



            return "";


        }

Solution

  • The result of this was to have the service provider give me their interface classes as the WSDL did not generate proxies properly. I then ran the interface through a channelfactory like so:

    ChannelFactory<ICourtRecordMDEPort> factory = new ChannelFactory<ICourtRecordMDEPort>("epConfig");
    ICourtRecordMDEPort client = factory.CreateChannel();
    CreateCaseResponse resp = client.CreateCase(req);
    factory.Close();
    

    "epConfig" is the name attribute of a configured endpoint in web/app.config.