Expose WCF 4.0 Service as SOAP and REST

Posted by: Mahesh Sabnis , on 6/29/2011, in Category Windows Communication Foundation (WCF)
Views: 128182
Abstract: By exposing a WCF service on 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. This article shows how to do so.

WCF services in enterprises are used for developing distributed enterprise application. There are lots of advantages of using WCF services. WCF service can be exposed on multiple endpoints e.g. HTTP, TCP etc. and the client applications have a choice to select the specific endpoint for the communication. WCF 3.5 had introduced support for Representational State Transfer (REST) using which the response from the service can be directly send using Plain Old Xml form (POX). If the WCF service is using REST/POX, then it is not necessary for the client to consume the WCF service proxy. For REST, WCF introduced a new binding i.e. WebHttpBinding. A client application that can do HTTP communication and can process XML, could now directly make a call to the WCF service and perform operations using XML.

 

And now the million dollar question which motivated me to write this article – If an existing service is already exposing SOAP using bindings like the basicHttpBinding, then can the same service expose REST i.e. using WebHttpBinding too? The answer is YES and in this article, I have explained the same.

Step 1: Open VS2010 and create a Blank solution, name it as ‘WCF_SOAP_REST’. In this solution, add a new WCF service application and call it ‘WCF_SOAP_REST_Service’. Make sure that .NET 4.0 framework is selected.

Step 2: Rename IService1.cs to IService.cs and rename Service1.svc to Service.svc . Add the following ServiceContract, OperationContract and DataContract in IService.cs.

wcf soap rest service

The OperationContract GetEmployees() also applied with WebGet attribute for REST exposure.

Step 3: Open the Web.Config file and 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. Define EndpointBehavior for the REST endpoint so that the XML response can be send and the help page is displayed to the end user.

rest-endpoint

Step 4: Open Service.svc.cs and write the following code:

wcf-service-aspnet

The attribute AspNetCompabilityRequirements is used for specifying an ASP.NET compatible environment for WCF service execution.

Step 5: Right-Click on the Service.svc and select ‘View in Browser’ and the result will be as shown below:

WCF service Demo

The above image shows SOAP hosting and you can verify it by clicking on the URL with ?wsdl. You should see the wsdl as below:

SOAP Wsdl

The Endpoint address used by the client application to interact with WCF with SOAP will be:

“http://localhost:5460/Service.svc/soapService”

In the Web.Config file, we have created a REST endpoint of the name ‘XMLService’. So to check the REST-POST (XML) response, change the browser’s address bar contents as shown below and hit Enter:

“http://localhost:5460/Service.svc/XmlService/Employees”

..and you should see the following XML

XML Service

We have successfully exposed the WCF service both on SOAP and REST.

Consuming the WCF service in a Project

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

“http://localhost:5460/Service.svc”

Step 7: Open MainWindow.xaml and write the XAML shown below:

wcf-soap-rest-wpf-xaml

The above xaml declares two buttons and DataGrids, each for the SOAP and REST call.

Step 8: Use the following namsepaces in the MainWindow.xaml.cs:

using System.Net;

using System.Runtime.Serialization;  

Step 9: In the MainWindow.Xaml.cs, write code for every button click:

SOAP Call Button click:

SOAP call

REST Call Button Click:

WCF REST Call

The above code creates a WebRequest using the WCF Service Url with REST endpoint name. The response is collected using WebResponse class. Since the response is containing a stream of objects using DataContractSerializer, the response message stream is deserialized and the result is stored in an Employee[].

Step 10: Run the client application and click on both buttons. The result will be as shown below:

WPF WCF SOAP REST call

Conclusion: By exposing a WCF service on 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.

The entire source code of this article can be downloaded over here

Give a +1 to this article if you think it was well written. Thanks!
Recommended Articles
Mahesh is having 10 years of experience in IT education and development. He is a Microsoft Certified Trainer (MCT) since 2005 and has conducted various Corporate Training programs for .NET Technologies (all versions). Follow him on twitter @maheshdotnet


Page copy protected against web site content infringement by Copyscape


User Feedback
Comment posted by Ashraf on Monday, August 8, 2011 3:06 PM
Mahesh very well presented, It's worth for an MSDN article.
As a side note, If you use .Net 4, then there is an interesting way you can set contract and endpoint. here is the link.

http://stackoverflow.com/questions/5552695/wcf-4-soap-and-rest-endpoints
Comment posted by Biju Nair on Tuesday, October 11, 2011 3:51 PM
Hello Mahesh,

Nice article. I would like to extend this further. Lets say you want to expose the REST data as either XML or JSON. What would be the best approach to handle that without having to create a separate service method with an attribute set as ResponseFormat = WebMessageFormat.Json.

Thanks
Comment posted by pankaj jha on Thursday, February 2, 2012 11:08 PM
Thanks a lot for your article .It was a great help for me.

Thanks and Regards
Pankaj Jha
Comment posted by duoct on Monday, February 27, 2012 10:57 PM
Biju, have you tried setting the response format like so:
ResponseFormat=WebMessageFormat.Xml | WebMessageFormat.Json
Comment posted by Dave on Friday, May 18, 2012 11:24 AM
I found this article to be very helpful. In your config file you specified helpEnabled=true for the Rest service; after adding a description I got the default help page to show. Do you have any pointers on how to get a similar help page to show up for the Soap endpoint?
Comment posted by anil on Tuesday, July 24, 2012 6:27 AM
Hi good,
Tell me how to create WCF REST service &perform INSERT,UPDATE,DELETE operations in DATABASE,
And also consume this service My ASP.NET webpages,
plz tell me one SIMPLE Example
help me plz.......
send source code to my mail ID:mandla.anilbabu@gmail.com
Comment posted by anil on Thursday, July 26, 2012 6:01 AM
I found this article to be helpfull,
provide me Insert,update ,Delete also as soon as possible?
plz help me
Comment posted by anil on Thursday, July 26, 2012 6:08 AM
Provide me exasmple Of performing using Database table plz help me
Comment posted by Rajani Kasaram on Thursday, August 30, 2012 7:36 AM
Excellent article. It was a great help.
Comment posted by Divakar on Thursday, September 6, 2012 7:04 AM
Mahesh,

Good Article, I want to see the SOAP xml like SOAP Header SOAP Body and all?
Comment posted by Srilakshmi on Thursday, September 27, 2012 5:10 AM
Good article. Helped me a lot.
Comment posted by anil on Saturday, November 3, 2012 1:05 AM
I FOUND THIS ARTICLE VERY VERY HELP FULL. THANKS SO MUCH
Comment posted by ramizmoh on Wednesday, December 19, 2012 2:47 AM
Very comprehensive. Well Done. +1
Comment posted by Baijnath pathak on Tuesday, January 1, 2013 1:06 AM

very informative article, Nice one, Thanks Baijnath
Comment posted by miningInformation on Thursday, March 28, 2013 2:46 PM
Nice Article. Well explained.
Comment posted by chitranjan on Friday, April 26, 2013 8:15 AM
This is very nice article if you want to get more example of wcf restful web service then use this link
http://www.dotnetnukes.blogspot.in/2013/04/create-restful-wcf-service-api-using.html
Comment posted by Prabhat on Tuesday, May 7, 2013 1:20 AM
Very good article. Thanks.
Comment posted by Rod on Wednesday, May 22, 2013 10:27 AM
Thank you for posting this article.  It was very helpful.  The only things I ran into is that it used "MyRef" as a reference in the client instead of the actual name given the service as defined in the article: WCP_SOAP_REST_Service.  That slowed me down a little bit but some Googling helped me resolve it.  It required adding a reference to the service project in the client project and then changing the "MyRef" to the "WCF_SOAP_REST_Service".
Comment posted by Mehmet on Friday, June 21, 2013 7:45 AM
Thank you for your article.It helped me so much. But when I used wcf rest service,suddenly usage of cpu rising from %10 to %20.Can you help me ?
Comment posted by Prashath on Monday, July 8, 2013 2:13 AM
How to call the REST Service having where condition EMPID , EMPNAME, DAEPNAME
Comment posted by Mayank Jain on Saturday, July 20, 2013 3:22 AM
I am getting error
"the character encoding of the html document was not declared the document will render with garbled text"

while calling directly with the url what is the issue that causes.
Comment posted by Lokesh on Wednesday, July 24, 2013 7:02 AM
Nice article Mahesh. For me its a starting step and really helped me to explore a lot!!
Comment posted by Rajeev Kumar Baliyan on Thursday, July 25, 2013 6:29 AM
Nice article Mahesh ji.
This article will help to learn about the difference between SOAP and REST service.
Comment posted by Amol on Friday, August 2, 2013 9:33 AM
Great article helped me a lot great efforts :)
Comment posted by Dharmendra Mistry on Thursday, August 15, 2013 9:32 AM
Hi, Mahesh
Great article. It solves our big problem.

Thank you very much from myself and my friend Nilesh Mistry.
Comment posted by Sanjit Kumar on Thursday, August 22, 2013 4:43 AM
Hi Mahesh,
This is the good article for REST Services and easily understandle. Thnaks for providing this.
Regards,
Sanjit
Comment posted by Mohit Raja on Saturday, August 31, 2013 10:39 AM
Hi Mahesh,
Thank you very much for this awsome article.
I was new to RestFul service and this article helped me to understand it very well.
Along with this, I  also learnt about WPF application.:-)
Thanks a lot.
Comment posted by Ramcharan on Thursday, October 10, 2013 2:01 AM
Hi
I created wcf rest service to insert employee details
I dont know how to consume to my asp.net mvc4 application
please send code to my mail id
thanks
Comment posted by Pooja on Friday, October 18, 2013 3:25 AM
i am new to.net and i need to work on wcf and mfc technologies would you suggest me a good source to learn this ??
    i am known to C# and ASP.net but new to this !! plz revert on my email
Comment posted by Jessica McNally on Friday, December 20, 2013 6:54 PM
This still leaves the question, when consuming the WCF service in an ASP.NET MVC SPA, do you need to define the endpoint in your web config file? Also, do you need to map the service in your route config file? Finally, do you need to have a controller as well as a view to consume the service? Note I am not trying to develop a service! Just consume one RESTful WCF with json
Comment posted by Akhil on Friday, June 6, 2014 4:52 AM
Pretty helful article..this was realyy a piece of cake.. :) :D..thanks alot..GOD bless..
Comment posted by abbid siddiqui on Friday, July 11, 2014 6:22 PM
Hello Mahesh
Are there any quick steps to convert this into JSON based (both REST and SOAP) instead of XML based?  Also need to know the changes on the Win Application we need to do in order to consume this.
Comment posted by anil on Tuesday, July 15, 2014 11:47 AM
{
[DataContract]
public class Employee
{
[DataMember]
public int EmpNo { get; set; }

[DataMember]
public string EmpName { get; set; }



//[DataMember]
//public List AllSales { get; set; }

[DataMember]
public string DeptName { get; set; }

[DataMember]
public List AllDepart { get; set; }
[DataMember]
public List AllSales { get; set; }

[DataMember]
public List Purchages { get; set; }

}
[DataContract]
public class Order
{
[DataMember]
public int Oid { get; set; }

[DataMember]
public string OName { get; set; }

//[DataMember]
//public List Sa { get; set; }
}

[DataContract]
public class Purchage
{
[DataMember]
public int Id { get; set; }

[DataMember]
public string IName { get; set; }
}

[DataContract]
public class Department
{
[DataMember]
public int DeptNo { get; set; }

[DataMember]
public string Dname { get; set; }

[DataMember]
public string Location { get; set; }
}
[DataContract]
public class Sales
{
[DataMember]
public string SName { get; set; }

[DataMember]
public string Sloc { get; set; }

[DataMember]
public List Orders { get; set; }


}

public partial class EmployeeData
{
private static readonly EmployeeData _instance = new EmployeeData();

private EmployeeData() { }

public static EmployeeData Instance
{
get
{
return _instance;
}
}

List empList = new List
{

new Employee() { EmpNo=101,EmpName="Anil",
AllDepart=new List { new Department (){ DeptNo=1,Dname="Sales",Location="Vij"}},
DeptName="DEV",
AllSales=new List { new Sales (){ SName="jjj",Sloc="ban",Orders=new List{new Order (){Oid=200,OName="ebay"}}}},
Purchages=new List {new Purchage () {Id=200,IName="Mobiles"}


}}
};
public List EmployeeList
{
get
{
return empList;
}
}

public void Add(Employee newEmployee)
{
empList.Add(new Employee
{
EmpNo = newEmployee.EmpNo,
EmpName = newEmployee.EmpName,
DeptName = newEmployee.DeptName,

});
}

public void UpdateEmp(Employee obj)
{

empList.Where(p => p.EmpNo == obj.EmpNo).Update(p => p.EmpName = obj.EmpName);

}
}

public static class LinqUpdates
{

public static void Update(this IEnumerable source, Action action)
{
foreach (var item in source)
action(item);
}

}

}
Actually this is my total code,

The using this update method ,i want to update and Insert total List of data,

The below list data i want to update and Insert
please give me that two methods for multiple classes

Post your comment
Name:  
E-mail: (Will not be displayed)
Comment:
Insert Cancel