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.
One service contract file along with a service file (.svc) is created automatically. We can modify the default contract file and service file.
- using System.ServiceModel;
- namespace WcfSOAPandREST
- {
- [ServiceContract]
- public interface IService1
- {
- [OperationContract]
- string Greeting(string value);
- }
- }
Service1.svc.cs
- namespace WcfSOAPandREST
- {
- public class Service1 : IService1
- {
- public string Greeting(string value)
- {
- return string.Format($"Hello {value}! Welcome to C# Corner");
- }
- }
- }
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...".
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Greetings.aspx.cs" Inherits="WcfSOAPandREST.Greetings" %>
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
- <asp:Button ID="Button1" runat="server" Text="Greet" />
- <br />
- <br />
- <asp:Label ID="Label1" runat="server"></asp:Label>
- </div>
- </form>
- </body>
- </html>
We can add the below code to Greetings.aspx.cs file for Button-click event.
- using System;
- namespace WcfSOAPandREST
- {
- public partial class Greetings : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- }
- protected void Button1_Click(object sender, EventArgs e)
- {
- ServiceReference1.Service1Client soap = new ServiceReference1.Service1Client();
- Label1.Text = soap.Greeting(TextBox1.Text);
- }
- }
- }
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.
The entire Web.config file is given below.
- <?xml version="1.0"?>
- <configuration>
- <appSettings>
- <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
- </appSettings>
- <system.web>
- <compilation debug="true" targetFramework="4.5.2" />
- <httpRuntime targetFramework="4.5.2"/>
- </system.web>
- <system.serviceModel>
- <services>
- <service name="WcfSOAPandREST.Service1">
- <endpoint address="" binding="webHttpBinding"
- contract="WcfSOAPandREST.IService1" behaviorConfiguration="web">
- </endpoint>
- </service>
- </services>
- <bindings>
- <basicHttpBinding>
- <binding name="BasicHttpBinding_IService1" />
- </basicHttpBinding>
- </bindings>
- <client>
- <endpoint address="http://localhost:51004/Service1.svc" binding="basicHttpBinding"
- bindingConfiguration="BasicHttpBinding_IService1" contract="ServiceReference1.IService1"
- name="BasicHttpBinding_IService1" />
- </client>
- <behaviors>
- <serviceBehaviors>
- <behavior>
- <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
- <serviceDebug includeExceptionDetailInFaults="false"/>
- </behavior>
- </serviceBehaviors>
- <endpointBehaviors>
- <behavior name="web">
- <webHttp/>
- </behavior>
- </endpointBehaviors>
- </behaviors>
- <protocolMapping>
- <add binding="basicHttpsBinding" scheme="https" />
- </protocolMapping>
- <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
- </system.serviceModel>
- <system.webServer>
- <modules runAllManagedModulesForAllRequests="true"/>
- <directoryBrowse enabled="true"/>
- </system.webServer>
- </configuration>
We can decorate the IService1.cs contract with the below attributes for REST operation.
- using System.ServiceModel;
- using System.ServiceModel.Web;
- namespace WcfSOAPandREST
- {
- [ServiceContract]
- public interface IService1
- {
- [OperationContract]
- [WebInvoke(
- Method = "GET",
- UriTemplate = "/Greeting/{value}",
- RequestFormat = WebMessageFormat.Json,
- ResponseFormat = WebMessageFormat.Json)]
- string Greeting(string value);
- }
- }
We have added a “WebInvoke” attribute to this interface. Let us run the application. It will list the entire directory.


DeepanPosted Jul 9, 2019, 1:34 AM
Great article..
Marwa BalhoudiPosted Jan 4, 2019, 6:42 AM
Good Blog, Thank you for the sharing
Rushi MehtaPosted Dec 8, 2018, 2:16 AM
Very Nice Explain done... Thanks, Sarathal for Sharing