Canarys | IT Services


WCF SOAP and REST Services


WCF (Windows Communication Foundation) is a programming platform and runtime system for building, configuring and deploying network-distributed services. It is the latest service oriented technology; Interoperability is the fundamental characteristics of WCF. WCF provides a common platform for all .NET communication.

WCF provides a runtime environment for your services, enabling you to expose CLR types as services, and to consume other services as CLR types. So what is SOAP and REST?

What is SOAP?

SOAP (Simple Object Access Protocol) is a simple solution for interaction of different applications built in different languages and running on different platforms as it uses HTTP as its transport and XML as its payload for sending and receiving messages. It is a lightweight and a loosely coupled protocol for exchange of information in a decentralized and a distributed environment.

What is REST?

REST (Representational State Transfer), attempts to codify the architectural style and design constraints that make the Web what it is. REST emphasizes things like separation of concerns and layers, statelessness, and caching, which are common in many distributed architectures because of the benefits they provide. These benefits include interoperability, independent evolution, interception, improved scalability, efficiency, and overall performance."

Actually only the difference is how clients access our service. 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. But first, let’s start from the beginning.

So how to enable both SOAP and REST services in the same WCF service.

STEP 1: Create a new WCF service application.

STEP 2:   Let us create two service contracts . One for SOAP and one for REST Service. I have created a Service Contract IService1 for SOAP and Service Contract IService2 for REST as shown below,




    public interface IService1



        List<Customers> GetCustomers();


        void Add(Customers cust);



    public class Customers


        public int CustomerID { get; set; }

        public string CustomerName { get; set; }

        public string CustomerCity { get; set; }





    public interface IService2



        [WebGet(UriTemplate = "/GetCustomer", RequestFormat = WebMessageFormat.Json,

        ResponseFormat = WebMessageFormat.Json)]

        List<Customers> GetCustomers();



STEP 3:  Implementing both SOAP and REST service i.e. IService1 and IService2 in Service1.svc file.Add below code in Service1.svc

    public class Service1 : IService1, IService2


      List<Customers> lstCustomers = new List<Customers>();

        public void Add(Customers cust)




        public List<Customers> GetCustomers()


            lstCustomers.Add(new Customers { CustomerID = 1, CustomerName = "praveen", CustomerCity = "bangalore" });

            return lstCustomers;




STEP 4:  Configuring WebConfig file

Configure service for both basicHttpBinding and webHttpBinding, So in Web.Config file define two endpoints – one each for SOAP and REST. The binding for SOAP is basicHttpBinding and for REST is webHttpBinding. Also define ServiceBehavior for publishing metadata for SOAP endpoint so that the client application can consume the WCF service. Add this below code in web config.


xml version="1.0"?>



    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />



    <compilation debug="true" targetFramework="4.5" />

    <httpRuntime targetFramework="4.5"/>





        <behavior name="servicebehavior">


          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>


          <serviceDebug includeExceptionDetailInFaults="false"/>




        <behavior name="restbehavior">






      <service name ="RESTvsSOAP.Service1"

               behaviorConfiguration ="servicebehavior">

        <endpoint name ="SOAPEndPoint"

                  contract ="RESTvsSOAP.IService1"

                  binding ="basicHttpBinding"

                  address ="soap" />

        <endpoint name ="RESTEndPoint"

                  contract ="RESTvsSOAP.IService2"

                  binding ="webHttpBinding"

                  address ="rest"

                  behaviorConfiguration ="restbehavior"/>




      <add binding="basicHttpsBinding" scheme="https" />


    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />



    <modules runAllManagedModulesForAllRequests="true"/>


    <directoryBrowse enabled="true"/>




STEP 5: Consuming the services

  To consume the WCF service in the same solution, add a new project, name it as ‘ConsumingServices’. In this project, add the service reference of the WCF service using the following address:


In program.cs file add below code

        static void Main(string[] args)





        static void CallingSOAPfunction()


            Service1Client proxy = new Service1Client();

            List<Customers> lstCustomer = proxy.GetCustomers();

            foreach (var customers in lstCustomer)


                Console.WriteLine("CustomerId : " + customers.CustomerID);

                Console.WriteLine("CustomerName : " + customers.CustomerName);

                Console.WriteLine("CustomerCity : " + customers.CustomerCity);





        static void CallingRESTfunction()


             HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:62333/Service1.svc/Rest/GetCustomer");

            //Get the Web Response

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            Stream responseStream = response.GetResponseStream();

            //Seting Up the Stream Reader

            StreamReader readerStream = new StreamReader(responseStream, System.Text.Encoding.GetEncoding("utf-8"));

            string json = readerStream.ReadToEnd();


            var jo = JObject.Parse(json.Replace("[", "").Replace("]", ""));

            Console.WriteLine("CustomerId : " + (string)jo["CustomerID"]);

            Console.WriteLine("CustomerName : " + (string)jo["CustomerName"]);

            Console.WriteLine("CustomerCity : " + (string)jo["CustomerCity"]);



 SO exposing a WCF service as REST and SOAP, you can make it more accessible to all types of clients. This is especially useful for the clients who can consume the proxy as well as can understand only HTTP communication with XML data processing.

Leave a Reply

Your email address will not be published. Required fields are marked *

Reach Us

With Canarys,
Let’s Plan. Grow. Strive. Succeed.