Creating WCF RESTful service for POST method

Here I am creating the WCF Rest service to insert some data into database using POST method. So my database designs as follows:


Step by step procedure to create the WCF Restful service

1. Create a project with WCF


2. Delete unwanted code from Service1.svc.cs and IService1.cs

3. Now our class and interface file looks as:



4. Add below code in the Service1.svc.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace CreateService
{
    public class Service1 : IService1
    {
        [WebInvoke(UriTemplate = "ADDStudent",
            Method = "POST",
            ResponseFormat = WebMessageFormat.Json)]

        public void AddStudent(StudentDetails sd)
        {
            string constr = ConfigurationManager.ConnectionStrings["ACTMiniQConnection"].ConnectionString;
            SqlConnection con = new SqlConnection(constr);
            SqlCommand cmd = new SqlCommand("insert into students values (@Studentname,@SDepartment,@SAddress,@SMobile)", con);
            con.Open();
            cmd.Parameters.AddWithValue("@Studentname", sd.StudentName);
            cmd.Parameters.AddWithValue("@SDepartment", sd.SDepartment);
            cmd.Parameters.AddWithValue("@SAddress", sd.SAddress);
            cmd.Parameters.AddWithValue("@SMobile", sd.SMobile);
            cmd.ExecuteNonQuery();
            con.Close();
        }
    }

    [DataContract(Namespace = "")]
    public class StudentDetails
    {
        [DataMember(Order = 0)]
        public string StudentName { get; set; }
        [DataMember(Order = 1)]
        public string SDepartment { get; set; }
        [DataMember(Order = 2)]
        public string SAddress { get; set; }
        [DataMember(Order = 3)]
        public string SMobile { get; set; }
    }

}


5. Add below code in the IService1.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace CreateService
{
    [ServiceContract]
    public interface IService1
    {
        [OperationContract()]
        void AddStudent(StudentDetails sd);
    }
}


6. Change the web.config file. Make changes in <system.serviceModel>

<system.serviceModel>
    <services>
      <service behaviorConfiguration="ServiceBehaviour" name="CreateService.Service1">
        <endpoint address="" behaviorConfiguration="Service1"
          binding="webHttpBinding" bindingConfiguration="" contract="CreateService.IService1">
        </endpoint>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehaviour">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="Service1">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />

  </system.serviceModel>

7. Your whole web.config looks like below


 8. Right-click in your Service1.svc file and click on view in browser.


Now WCF Restful Service for POST Method have been created successfully 

Credits
S . Divya