WCF SOAP and REST Services - Blogs

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.


What are Frames? How to handle frames in Selenium WebDriver with C#?

IFrame (FullForm: Inline Frame) is an HTML document that is included in another HTML document and is...

Read More >

What is Synchronization? Handling Synchronization in Selenium WebDriver using C#:

Synchronization meaning: when two or more components involved to perform any action, we expect these...

Read More >

Sending Test reports by Email using Office 365, Gmail

Wouldn’t it be great if Test Report are sent automatically across team as soon the Test Execut...

Read More >

ASP.NET: Audit Trail Implementation using Entity Framework

When you are working with certain projects which involves the Customer records, you might need to tr...

Read More >

Create Restful service using Web API

What is Asp.Net Web API?Asp.Net Web API is a framework for building HTTP services that can be consum...

Read More >

Create custom project templates in Visual Studio

Visual studio installation comes with the various predefined project templates, and we can use one o...

Read More >

What's New In C# 6.0?

Microsoft has released the preview version of Visual Studio 2015 and .NET 4.6 for developers to try ...

Read More >

Creating an RDLC report in Asp.Net

Step 1.  Create a new website and Add DataSet to the WebsiteRight Click on the project, Add new...

Read More >

How to add Overlay in Windows phone 8

Windows Phone is windows mobile operating system from Microsoft.In my project, there was a requireme...

Read More >

Converting UserControl into a dll

User controls are containers into which you can put markup and Web server controls. You can then tre...

Read More >


Try DevOpSmartBoard Ultimate complete Azure DevOps End-to end reporting tool

Sign Up

  • Recent
  • Popular
  • Tag
Monthly Archive

Contact Us
  • *
  • *