Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending raw SOAP XML directly to WCF service from C#

Tags:

c#

.net

soap

wcf

I have a WCF service reference:

http://.../Service.svc(?WSDL)

and I have an XML file containing a compliant SOAP envelope

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
  <soapenv:Body>
    <MyXML>
       ...

Now, I would like to send this raw data directly to the service (and receive the response) via some C# code without using a Visual Studio service reference.

Is this possible, and if so, how?

like image 955
lox Avatar asked Nov 13 '09 10:11

lox


People also ask

How do you get SOAP request XML for a Web service call?

It sounds to me like you could create a sort of proxy application - change the URL in your client to point to the proxy - proxy would record the entire SOAP request message and send it on to the web service - receive the response, log it, and send it on to your client.

Does WCF support SOAP?

Normally, a WCF service will use SOAP, but if you build a REST service, clients will be accessing your service with a different architectural style (calls, serialization like JSON, etc.). Exposing a WCF service with both SOAP and REST endpoints, requires just a few updates to the codebase and configuration.

Is WCF the same as SOAP?

In ASP.NET Development services, SOAP messages are exchanged over HTTP, but WCF services can exchange the message using any format over any transport protocol. Though, SOAP is a default format that WCF uses. 10. WCF services have timeouts by default that can be configured.


1 Answers

You could use UploadString. You need to set the Content-Type and SOAPAction headers appropriately:

class Program
{
    static void Main(string[] args)
    {
        using (var client = new WebClient())
        {
            // read the raw SOAP request message from a file
            var data = File.ReadAllText("request.xml");
            // the Content-Type needs to be set to XML
            client.Headers.Add("Content-Type", "text/xml;charset=utf-8");
            // The SOAPAction header indicates which method you would like to invoke
            // and could be seen in the WSDL: <soap:operation soapAction="..." /> element
            client.Headers.Add("SOAPAction", "\"http://www.example.com/services/ISomeOperationContract/GetContract\"");
            var response = client.UploadString("http://example.com/service.svc", data);
            Console.WriteLine(response);
        }
    }
}
like image 155
Darin Dimitrov Avatar answered Oct 12 '22 04:10

Darin Dimitrov