How to consume third party webservice through Wcf service SOAP with out service reference

1.2k views Asked by At

I am trying to call the third party webservice in WCF service.

i am able to call the the service by adding the service reference in(URL) through WCF service, but while doing that i heard from the people it is not the right way of doing this, when i googled this, found through channelfactory we can call the service .

But the problem is it is saying the contract dlls should get shared between client and server, this i am not getting.

can any body provide the sample to call the service with out adding service reference. http://www.codeproject.com/Tips/558163/Difference-between-Proxy-and-Channel-Factory-in-WC

and what are the issues if we add service reference in the project and what are the pitfalls?

I am sure that i have to call SOAP service only

Thanks

1

There are 1 answers

2
Hakunamatata On

You can use HttpWebRequest to create a web request and then pass the XML document describing whole SOAP request and get the response. Something like below code I used in one of my application. I have removed few bits from it that are specific to my application but you will get the idea.

public static string ProcessRequestSOAP()
        {
            string result = "";
            try
            {
                XmlDocument soapEnvelop = new XmlDocument();
                soapEnvelop.LoadXml(SOAPXML_Request); // This is the SOAP xml document that is generated when calling web service. You have to manually create this.

                string url = webServiceURL; // External Web Service URL
                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
                webRequest.ContentType = "text/xml;charset=\"utf-8\"";
                webRequest.Accept = "text/xml";
                webRequest.Method = "POST";

                webRequest.Credentials = new NetworkCredential(userName,password); // Username/password to call web service if required


                using (Stream stream = webRequest.GetRequestStream())
                {
                    soapEnvelop.Save(stream);
                }

                //get response from server
                WebResponse response = webRequest.GetResponse();

                //read response stream
                using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                {
                    if (reader != null)
                    {
                        result = System.Xml.Linq.XDocument.Load(reader).ToString();
                    }
                    else
                    {
                        result = null;
                    }
                }
            }
            catch (SoapException ex)
            {
                result = new ExceptionXElement(ex, false).ToString();
            }
            catch (Exception ex)
            {
                result = new ExceptionXElement(ex, false).ToString();
            }
            return result;

        }