0

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 "";


        }
Scott Moniz
  • 650
  • 11
  • 20
  • [You are doing it wrong](http://www.codemag.com/Article/0809101) –  Nov 09 '17 at 22:42
  • 1
    I've been helping a few OPs out recently with similar request. He is a link to the most complete response : https://stackoverflow.com/questions/46722997/saml-assertion-in-a-xml-using-c-sharp/46724392#comment80642919_46724392 – jdweng Nov 09 '17 at 22:53
  • @MickyD my wsdl does not properly generate proxies - so I cannot use proxies. I need to manually build the calls at a lower level. I cannot use svcutil, wcf, channels etc. I am forced to use the low level webclient / httpwebrequests etc. – Scott Moniz Nov 10 '17 at 19:03
  • @jdweng i will take a look at that as part of it is similar (soap envelope etc). Thanks for the resource – Scott Moniz Nov 10 '17 at 19:04
  • Then fix your WSDL. Until you do, yours is a badly described SOAP service –  Nov 10 '17 at 23:29
  • @MickyD it is not my WSDL. It was provided to me and I have 0 control over the WSDL. It makes use of something called substitution groups which apparently is valid but microsoft tools have an issue with? Im not sure. Have a call with the third party service provider soon to discuss. – Scott Moniz Nov 13 '17 at 21:25

2 Answers2

0

There is no client or library for SOAP service except the WCF. If you don't want to add wsdl by service reference in VS then you should try HttpWebRequest with soap headers rather than WebClient. Because WebClient is higher class of HttpWebRequest and its slower.

uugan
  • 27
  • 6
  • _"...There is no client or library for SOAP service..."_ - there is actually, it's called WCF. You might want to rephrase your answer –  Nov 10 '17 at 12:10
  • I will look into HttpWebRequest - but if WebClient is a wrapper around WebRequest I think it is still a similar approach. I have tried using WCF but it chokes on the WSDLs due to substitution groups so I have to go low-level and send the http calls / soap envelopes manually. – Scott Moniz Nov 10 '17 at 19:02
0

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.

Scott Moniz
  • 650
  • 11
  • 20