Open Visual studio 2010
Step1:- Create New ProjectàWCF Service Application
Step 2:- open solution explorer delete Iservice1.cs and Service1.SVC

        

Step 3:- Right click on Solution Add new item-> WCF Service

Step 4:- Add a method  in Iservice1.cs file to get data in json format.
Using System.ServiceModel;
using System.ServiceModel.Web;
namespace WCFRestWithJSONResult
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface IService1
    {
       
        [OperationContract]
        [WebInvoke (Method="GET", ResponseFormat=  WebMessageFormat.Json,  BodyStyle=WebMessageBodyStyle.Wrapped, UriTemplate= "json/{id}")]
        string GetJsonDta(string id);
    }
}

Step5:-Now open Service1.svc and add following code


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

namespace WCFRestWithJSONResult
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    public class Service1 : IService1
    {
        public string GetJsonDta(string id)
        {
            return "You have entered" + id;
        }
    }
}


Step 6:- Open web.config file and do the changes as below.


<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
 
</configuration>

Step 7:- open browser with the url





Comments

Popular posts from this blog