RSS

Call WCF sevice using only WebClient and SOAP xml

14 Dec

Did you ever had to call wcf service using regular SOAP “XML” and C# without adding a web/service reference to your project and proxy class.

Actually You can send SOAP directly to a WCF service without creating a proxy class. This is very easy. All you have to do is create a WebClient object and set “Content-Type” and “SOAPAction” headers then upload the SOAP message to the service. The code will be something like that

using (var client = new WebClient())
{
    var data = GetSoap();
    client.Headers.Add("Content-Type", "text/xml;charset=utf-8");
    client.Headers.Add("SOAPAction", "\"http://tempuri.org/IMyService/MyOperation\"");
    var response = client.UploadString("http://localhost:1234/MyService.svc", data);
    Console.WriteLine(response);
}

As you noticed there is no magic here except for the GetSoap() method. It can be from previously saved requests from your service, or you can make up a soap body yourself. This also is not that hard. Let’s look at a SOAP envelop before we continue.

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Body>
        <MyOperation xmlns="http://tempuri.org/">
            <Name>Maged</Name>
            <email>maged@mail.com</email>
            <phone>123456789</phone>
        </MyOperation>
    </s:Body>
</s:Envelope>

This SOAP envelop is for a request made to MyService WCF service to call MyOperation method which accepts 3 parameters name, email and phone. Very easy isn’t it?. I think anyone can construct SOAP envelop.

Advertisements
 
Leave a comment

Posted by on December 14, 2012 in C#

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: