WCF (Windows Communication Foundation) is still widely used in legacy enterprise applications, especially when systems need SOAP/XML or lightweight REST services. In this article, we will learn how to create a REST-based WCF Web Service using JSON in Visual Studio 2015.
We will cover:
✔ Creating a WCF Service
✔ Writing a POST API method
✔ Accepting JSON input
✔ Returning JSON output
✔ Configuring web.config for REST/JSON
✔ Using DataContract / DataMember
✔ Working sample code
Step 1: Create a New WCF Application in Visual Studio 2015
Open Visual Studio 2015
Go to File → New → Project
Select
Visual C# → WCF → WCF Service Application
Name the project (Example: CholaWcfService)
Click OK
![wcf1]()
![wcf2]()
This creates:
Service1.svc
Service1.svc.cs
IService1.cs
web.config
Step 2: Modify IService1.cs
We want our API to accept JSON input and return JSON output, so we use:
[OperationContract]
[WebInvoke]
webHttpBinding
IService1.cs
using System.IO;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
namespace CholaWcfService
{
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebInvoke(
Method = "POST",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "/Payments"
)]
Stream Payments(RootObject request);
}
}
Step 3: Create the Data Contract Class
DataContract and DataMember must be used for WCF JSON model binding.
[DataContract]
public class RootObject
{
[DataMember]
public string data { get; set; }
[DataMember]
public string details { get; set; }
[DataMember(Name = "event")]
public string Event { get; set; }
[DataMember]
public payload payload { get; set; }
[DataMember(Name = "Bank Account Number")]
public string bankaccountnumber { get; set; }
[DataMember(Name = "Client Id")]
public string clientcode { get; set; }
[DataMember(Name = "Description")]
public string description { get; set; }
}
Step 4: Implement the Service Method (Service1.svc.cs)
This method accepts JSON, processes it, and returns JSON as string inside a Stream.
Service1.cs
using System;
using System.Configuration;
using System.IO;
using System.ServiceModel.Web;
using System.Text;
public class Service1 : IService1
{
string strcon = ConfigurationManager.ConnectionStrings["cholaPortfolio"].ToString();
public Stream Payments(RootObject request)
{
try
{
string jsonResponse = "{\"status\":\"success\",\"message\":\"Payment processed\"}";
WebOperationContext.Current.OutgoingResponse.ContentType = "application/json";
return new MemoryStream(Encoding.UTF8.GetBytes(jsonResponse));
}
catch (Exception ex)
{
string errorResponse = "{\"status\":\"Error\",\"message\":\"" + ex.Message + "\"}";
WebOperationContext.Current.OutgoingResponse.ContentType = "application/json";
return new MemoryStream(Encoding.UTF8.GetBytes(errorResponse));
}
}
}
Step 5: Configure web.config for WCF JSON REST API
Replace or update with the following configuration:
Required Changes
Your current config is missing 3 important sections:
1. <endpointBehaviors> with <webHttp />
2. <endpoints> using webHttpBinding
3. behaviorConfiguration="jsonBehavior"
<configuration>
<connectionStrings>
<add
name="cholaPortfolio"
connectionString="Data Source=SoftSQL;Initial Catalog=testdb;Connect TimeOut=120; Max Pool Size=10000; user id=sa; password=capmark@09"
providerName="System.Data.SqlClient" />
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.5.2"/>
<httpRuntime targetFramework="4.5.2"/>
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="jsonBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service name="CholaWcfService.Service1">
<endpoint
address=""
binding="webHttpBinding"
contract="CholaWcfService.IService1"
behaviorConfiguration="jsonBehavior"/>
</service>
</services>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https"/>
</protocolMapping>
<serviceHostingEnvironment
aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
</configuration>
Step 6: Test in Postman
URL
http://localhost:port/Service1.svc/Payments
Method
POST
Headers
Content-Type: application/json
Sample JSON Body
{
"data": "123",
"details": "Testing",
"event": "payment.received",
"Bank Account Number": "1234567890",
"Client Id": "CL001",
"Description": "Payment API test"
}
Response
{
"status": "success",
"message": "Payment processed"
}