b

Saturday 26 May 2012

REST & JSON format(Hosting in IIS7 in WCF)


Build Basic WCF Service.

Step 1)  Create a Class Library (name it as wcftest)
                    Add reference System.ServiceModel  


            add 4 files   1)  Interface file 
                                2) Implementation File
                                3) SVC file
                                4) Web config file
Interface file


File name :  interface1.cs


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

namespace restapi
{
    // 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
    {

        [WebGet(RequestFormat=WebMessageFormat.Json,ResponseFormat = WebMessageFormat.Json, UriTemplate = "/GetPerson")]
        [OperationContract]
        Person getPersoninJSON();
        [WebGet(ResponseFormat = WebMessageFormat.Json, UriTemplate = "/GetData")]
        [OperationContract]
        String  GetData();

    }

[DataContract]
    public class Person
{
    [DataMember]
    public String Name;
    [DataMember]
    public int Age;
    [DataMember]
    public string color;
    public Person(string n,int a,string c)
    {
        Name = n;
        Age = a;
        color = c;
    }
}
}



Implementation file

file name is intImpl.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Runtime.Serialization;
using System.ServiceModel.Activation;
namespace restapi
{
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class Service1 : IService1
    {
        //Person p = new Person();
        public Person getPersoninJSON()
        {
            
            //p.Age = 38; p.color = "medium"; p.Name = "sitapati";
            
            return new Person("sirpatai",38,"medium");
        }

        public string GetData()
        {
            return "getting JSON data";
        }
    }
}



Add  SVC file
file name is wcftest.svc

<%@ ServiceHost Service="restapi.Service1"  Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>



Add   Web/application config file
file name:  web.config



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

  <system.web>
    <compilation debug="true"/>
  </system.web>
  <!-- When deploying the service library project, the content of the config file must be added to the host's 
  app.config file. System.Configuration does not support config files for libraries. -->
  <system.serviceModel>
    <standardEndpoints>
      <webHttpEndpoint>
        <standardEndpoint helpEnabled="true" automaticFormatSelectionEnabled="true">
          
        </standardEndpoint>
      </webHttpEndpoint>
    </standardEndpoints>
    <services>
      <service name="restapi.Service1" behaviorConfiguration="webbeh">
        <endpoint contract="restapi.IService1" binding="webHttpBinding" ></endpoint>
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="jsonbe">
          <webHttp helpEnabled="true" automaticFormatSelectionEnabled="true" defaultOutgoingResponseFormat="Json"/>
        </behavior> 
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="webbeh">
          <!-- 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="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

<startup><supportedRuntime version="v2.0.50727"/>
</startup>
</configuration>


Build Class Library-> dll will be produced.



Now hosting WCF Service in IIS 7.0


Step 1)  Runt  Inetmgr.msc from run command


Step 2)   Add new Site as shown below


Step 3)   add site name as mywcfone, create folder with same or different in c:\inetpub\wwwroot\wcfone
Step 4)  Click on Connect as...   button and provide ur system's administrator name and password
               to test connection Click on Test Settings Button...(testing purpose only)
Step 5)  Add port as 8080   or some unused port from 1025-65535. Click OK


Step 6)  Previously Created WCF files copy to c:\inetput\wwwroot\wcfone  folder

Step 7)   create a bin folder in wcfone
                    copy   wcftest.dll to bin folder only (not to debug folder)
                  copy  svc file and   web.config file to    c:\inetput\wwwroot\wcfone  folder.

folders & files
 c:\inetput\wwwroot\wcfone\bin  has wcftest.dll
c:\inetput\wwwroot\wcfone   has  wcftest.svc and web.config files

open browser type 

http://localhost:7070/RestJSON.svc/GetData
http://localhost:7070/RestJSON.svc/GetPerson
following output will come for GetPerson method. similar thing for GetData also

No comments:

Post a Comment