Expose WCF 4.0 Service as SOAP and REST
Posted by: Mahesh Sabnis
in Category Windows Communication Foundation (WCF)
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.
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.
Step 4: Open Service.svc.cs and write the following code:
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:
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:
The Endpoint address used by the client application to interact with WCF with SOAP will be:
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:
..and you should see the following XML
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:
Step 7: Open MainWindow.xaml and write the XAML shown below:
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:
Step 9: In the MainWindow.Xaml.cs, write code for every button click:
SOAP Call Button click:
REST Call Button Click:
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:
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
C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.
We at DotNetCurry are very excited to announce the pre-order of The Absolutely Awesome Book on C# and .NET. This is a concise technical eBook and will be available in PDF, ePub, and mobi.
Organized around concepts, this eBook aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core. Use these concepts in your next .NET Project or to crack your next .NET Interview.
Click here to Pre-Order this eBook at a Discounted Price!