83

Expose WCF 4.0 Service as SOAP and REST

Share on TwitterShare on TumblrSubmit to StumbleUponSave on DeliciousDigg This

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

prajapat