Enable SOAP And REST On Same WCF Service And Contract

In this post, we will see both SOAP and REST enabled in same WCF service using same contract. People may think, WCF is an outdated technology after Web API became so popular. But I feel, we can still use WCF for many occasions. That is the reason behind I write this post.

In this post, we will see both SOAP and REST enabled in the same WCF Service using the same contract.

People may think, WCF is an outdated technology after Web API became so popular. But I feel we can still use WCF for many occasions. That is the reason behind writing this post.

We can create a WCF service application.
 
Enable SOAP and REST on same WCF Service and Contract 
 
The project structure is shown below.
 
Enable SOAP and REST on same WCF Service and Contract 

One service contract file along with a service file (.svc) is created automatically. We can modify the default contract file and service file.

IService1.cs
  1. using System.ServiceModel;  
  2.   
  3. namespace WcfSOAPandREST  
  4. {  
  5.     [ServiceContract]  
  6.     public interface IService1  
  7.     {  
  8.         [OperationContract]  
  9.         string Greeting(string value);  
  10.     }  
  11. }  

Service1.svc.cs

  1. namespace WcfSOAPandREST  
  2. {  
  3.     public class Service1 : IService1  
  4.     {  
  5.         public string Greeting(string value)  
  6.         {  
  7.             return string.Format($"Hello {value}! Welcome to C# Corner");  
  8.         }  
  9.     }  
  10. }  

By default, WCF supports SOAP binding. We can add a client to this service and test the application. For that, we can use the same project. Right-click the solution and click "Add Service Reference...".

Enable SOAP and REST on same WCF Service and Contract 
 
Click “Discover” and choose “Services in Solution”.
 
Enable SOAP and REST on same WCF Service and Contract 
 
It will list the existing service “Service1.svc”. We can double click on it. It will install the service reference to the project.
 
Enable SOAP and REST on same WCF Service and Contract 
 
If you look at the Web.config file, you can see that the client reference is now added automatically. Also notice that "basicHttpBinding" is added. This binding is used for SOAP operations.
 
Enable SOAP and REST on same WCF Service and Contract 
 
We can add a WebForm to test the SOAP service. I will add a “Greetings.aspx” file and copy the below code to that file.
 
Greetings.aspx
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Greetings.aspx.cs" Inherits="WcfSOAPandREST.Greetings" %>  
  2.   
  3. <!DOCTYPE html>  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7.     <title></title>  
  8. </head>  
  9. <body>  
  10.     <form id="form1" runat="server">  
  11.         <div>  
  12.             <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>  
  13.             <asp:Button ID="Button1" runat="server" Text="Greet" />  
  14.             <br />  
  15.             <br />  
  16.             <asp:Label ID="Label1" runat="server"></asp:Label>  
  17.         </div>  
  18.     </form>  
  19. </body>  
  20. </html>  

We can add the below code to Greetings.aspx.cs file for Button-click event.

Greetings.aspx.cs
  1. using System;  
  2.   
  3. namespace WcfSOAPandREST  
  4. {  
  5.     public partial class Greetings : System.Web.UI.Page  
  6.     {  
  7.         protected void Page_Load(object sender, EventArgs e)  
  8.         {  
  9.   
  10.         }  
  11.   
  12.         protected void Button1_Click(object sender, EventArgs e)  
  13.         {  
  14.             ServiceReference1.Service1Client soap = new ServiceReference1.Service1Client();  
  15.             Label1.Text = soap.Greeting(TextBox1.Text);  
  16.         }  
  17.     }  
  18. }  

We have consumed the service reference and invoked the “Greeting” method in it.

Right-click the Greetings.aspx file and click “View in Browser” option.

Enable SOAP and REST on same WCF Service and Contract 
 
Enter a value in the text box and click the “Greet” button. It will execute the SOAP service and return the value to label as shown below.
 
Enable SOAP and REST on same WCF Service and Contract 
Let us now add REST binding to this project. For that, we can add “webHttpBinding” configuration to Web.config file.
 
Enable SOAP and REST on same WCF Service and Contract 
 
Define a behavior configuration “web” for this binding.
 
Enable SOAP and REST on same WCF Service and Contract 

The entire Web.config file is given below.

Web.config
  1. <?xml version="1.0"?>  
  2. <configuration>  
  3.   
  4.   <appSettings>  
  5.     <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />  
  6.   </appSettings>  
  7.   <system.web>  
  8.     <compilation debug="true" targetFramework="4.5.2" />  
  9.     <httpRuntime targetFramework="4.5.2"/>  
  10.   </system.web>  
  11.   <system.serviceModel>  
  12.     <services>  
  13.       <service name="WcfSOAPandREST.Service1">  
  14.         <endpoint address="" binding="webHttpBinding"  
  15.           contract="WcfSOAPandREST.IService1" behaviorConfiguration="web">  
  16.         </endpoint>  
  17.       </service>  
  18.     </services>  
  19.       
  20.     <bindings>  
  21.       <basicHttpBinding>  
  22.         <binding name="BasicHttpBinding_IService1" />  
  23.       </basicHttpBinding>  
  24.     </bindings>  
  25.     <client>  
  26.       <endpoint address="http://localhost:51004/Service1.svc" binding="basicHttpBinding"  
  27.         bindingConfiguration="BasicHttpBinding_IService1" contract="ServiceReference1.IService1"  
  28.         name="BasicHttpBinding_IService1" />  
  29.     </client>  
  30.     <behaviors>  
  31.       <serviceBehaviors>  
  32.         <behavior>  
  33.           <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>  
  34.           <serviceDebug includeExceptionDetailInFaults="false"/>  
  35.         </behavior>  
  36.       </serviceBehaviors>  
  37.   
  38.       <endpointBehaviors>  
  39.         <behavior name="web">  
  40.           <webHttp/>  
  41.         </behavior>  
  42.       </endpointBehaviors>  
  43.     </behaviors>  
  44.       
  45.     <protocolMapping>  
  46.       <add binding="basicHttpsBinding" scheme="https" />  
  47.     </protocolMapping>  
  48.     <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />  
  49.   </system.serviceModel>  
  50.   <system.webServer>  
  51.     <modules runAllManagedModulesForAllRequests="true"/>  
  52.     <directoryBrowse enabled="true"/>  
  53.   </system.webServer>  
  54.   
  55. </configuration>  

We can decorate the IService1.cs contract with the below attributes for REST operation.

IService1.cs
  1. using System.ServiceModel;  
  2. using System.ServiceModel.Web;  
  3.   
  4. namespace WcfSOAPandREST  
  5. {  
  6.     [ServiceContract]  
  7.     public interface IService1  
  8.     {  
  9.         [OperationContract]  
  10.         [WebInvoke(  
  11.             Method = "GET",   
  12.             UriTemplate = "/Greeting/{value}",   
  13.             RequestFormat = WebMessageFormat.Json,  
  14.             ResponseFormat = WebMessageFormat.Json)]  
  15.         string Greeting(string value);  
  16.     }  
  17. }  

We have added a “WebInvoke attribute to this interface.  Let us run the application. It will list the entire directory.

Enable SOAP and REST on same WCF Service and Contract 
 
Click the “Service1.svc” service file.
 
Enable SOAP and REST on same WCF Service and Contract 
 
Now, let us call the REST service.
 
Enable SOAP and REST on same WCF Service and Contract 
We have successfully executed the Greeting method using REST.
 
Enable SOAP and REST on same WCF Service and Contract
 
In this post, we have created a WCF service application and executed the default SOAP Service in the client application. For that, we have added a web form and executed the service. After that, we enabled the REST binding and configuration and added a WebInvoke attribute in the contract file. We have successfully executed the REST service in the browser. We have seen both, SOAP and REST, working in the same WCF service with the same contract.