Wednesday, May 29, 2013

Create JSONP WCF Service




  • Open VS2012 , then Click New Project, Select  WCF Service library as  New Project  Template      (File --> New Project à WCF  --> WCF Service Library) and Change the Name and Solution name as “ClacLib” and “MyCalcultor”  



    
  •      Open IService1.cs file and delete everything and paste this
            using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace ClacLib
{
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        string GetData(int value);

    }
}


  • Add “System.ServiceModel.Web” as Reference to WCF Service Lib project.


  •  Open Service1.cs file and delete everything  and paste this

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

namespace ClacLib
{

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
   
    public class Service1 : IService1
    {

        [Description("Accepts LoginName and Get Employee By Login Name")]
        [WebGet(BodyStyle = WebMessageBodyStyle.Bare,
        RequestFormat = WebMessageFormat.Json,
        ResponseFormat = WebMessageFormat.Json,
     UriTemplate = " GetData?value={value}")]

        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }
    }
}

Explanations:

WebGet – Get Method
Request Format and Response Format – accept and return data as JSON Objects
UriTemplate – PostFix of Service URL


  • Modify App.config file

You can delete this file , since we separate the service part from hosting part. But Keep   it As it is. We want only Web.Config file. 

  • Create new project under the same Solution and Select WCF Service Application as Project Template


  • Delete IService1.cs file and delete the cs file. We don’t want those two files since we have separated Service part from hosting part.




  •      Open Service1.svc file and delete ” CodeBehind="Service1.svc.cs"” part and Change the Service value

                                            Service Lib Project Name              Implemented Class Name


  • Add Service lib as Ref to Service Host project



Then click “OK”.

  • Modify the Web.config file

a.       Delete Service Model Section


b. Copy and paste below Section within that space as New Service Model

<system.serviceModel>

    <bindings>
      <webHttpBinding>
        <binding name="webHttpBindingWithJsonP" />
      </webHttpBinding>
    </bindings>

    <behaviors>

      <endpointBehaviors>
        <behavior name="webHttpBehavior">
          <webHttp helpEnabled="true" />
        </behavior>      
      </endpointBehaviors>

      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true"/>
          <serviceThrottling maxConcurrentCalls="100" maxConcurrentSessions="1000" maxConcurrentInstances="1160"/>
          <dataContractSerializer maxItemsInObjectGraph="655360000"/>
        </behavior>
      </serviceBehaviors>

    </behaviors>

    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />

    <services>
      <service name=" ClacLib.Service1">
        <endpoint address="Calc"
                  behaviorConfiguration="webHttpBehavior"
                  binding="webHttpBinding"
                  bindingConfiguration="webHttpBindingWithJsonP"
                  contract=" ClacLib.IService1"/>
      </service>
    </services>
  </system.serviceModel>

Explanations :

Services Tag –   Endpoint settings
Binding Tag -     Binding related Settings
Behaviors Tag - Behaviors related Settings


  •  Debug the application by press F5 or Click “Run”. It will host your service in localhost and open it in default browser. Give the correct URL and check whether it s working or not.

          Or else Use this URL
          http://localhost:8733/Service1.svc/Calc/GetData?value=4


  • Publish the Site and check again (How to Publish the Site ??? – Check here)

          I published the site in http://cd-ksurendran:8001
          Give some test values for {VALUE}

          Result of this URL look like this :



          If you don’t remember full URL  or want some help, Use this URL , it will display all  Operation Contract under specific Service Contract with some details

Result of this URL look like this :





4 comments:

Anonymous said...

ninest123 16.03
ray ban sunglasses, cheap oakley sunglasses, replica watches, michael kors outlet, oakley sunglasses, jordan shoes, prada handbags, nike outlet, nike air max, nike free, louis vuitton, tiffany and co, ugg boots, louboutin outlet, ray ban sunglasses, louis vuitton outlet, burberry outlet online, michael kors outlet, chanel handbags, uggs on sale, tiffany jewelry, louis vuitton outlet, michael kors outlet, oakley sunglasses, oakley sunglasses, louboutin shoes, prada outlet, ugg boots, ray ban sunglasses, ugg boots, burberry, replica watches, michael kors outlet, tory burch outlet, longchamp outlet, michael kors outlet, louis vuitton, michael kors, polo ralph lauren outlet, longchamp outlet, louis vuitton, polo ralph lauren outlet, louboutin, oakley sunglasses, gucci outlet, ugg boots, christian louboutin outlet, nike air max, longchamp

Anonymous said...

north face, hogan, coach outlet, air force, hollister pas cher, michael kors, coach outlet, lululemon, burberry, sac guess, nike roshe, nike free, true religion jeans, air jordan pas cher, lacoste pas cher, ralph lauren pas cher, michael kors, kate spade outlet, ralph lauren uk, michael kors, new balance pas cher, true religion jeans, ray ban uk, nike roshe run, converse pas cher, michael kors, hermes, mulberry, vans pas cher, louboutin pas cher, true religion outlet, ray ban pas cher, nike air max, oakley pas cher, air max, timberland, nike free run uk, kate spade handbags, nike air max, hollister, tn pas cher, replica handbags, nike air max, vanessa bruno, abercrombie and fitch, sac longchamp, true religion jeans, nike blazer, coach purses, longchamp pas cher, north face

Anonymous said...

ugg,uggs,uggs canada, canada goose, ugg,ugg australia,ugg italia, canada goose, hollister, wedding dresses, moncler, louis vuitton, canada goose outlet, swarovski, pandora charms, pandora jewelry, moncler outlet, moncler, sac louis vuitton pas cher, replica watches, louis vuitton, marc jacobs, louis vuitton, vans, moncler, swarovski crystal, pandora jewelry, toms shoes, moncler, juicy couture outlet, doudoune canada goose, karen millen, converse, lancel, canada goose outlet, coach outlet, gucci, ray ban, thomas sabo, moncler, supra shoes, canada goose uk, juicy couture outlet, links of london, barbour jackets, louis vuitton, canada goose, barbour, ugg boots uk, pandora charms, bottes ugg, converse outlet, moncler, montre pas cher, canada goose, ugg pas cher, moncler
ninest123 16.03

menna said...

فرسان العرب
ارخص شركة صيانة مسابح في الرياض
افضل شركة صيانة مسابح في الرياض
شركات صيانة مسابح في الرياض