Monday, March 18, 2013

WCF With JSONP

Before start with JSONP we should know what is REST ? and how it differ from other protocols? - Learn REST

What is JSONP ?

 JSONP or "JSON with padding" is a communication technique used in JavaScript. It provides a method to request data from a server in a different domain, something prohibited by typical web browsers because of the same origin policy.

By default WCF Services will not return JSON objects as result. if we want to get JSON objects , we have to define at Service implementation level.

If you are new to WCF , check this to get basic understanding about WCF!!!  -  Simple Steps to create the WCF Service

1.  Create WCF Library (WCF Service Library Template)

      a. Create Service contracts

          [OperationContract]
          [WebGet(ResponseFormat = WebMessageFormat.Json)]
          [JSONPBehavior(callback = "method")]
      
          EmployeeWorkDto GetEmployeMoreDetailsByUserName(string logInName);

       

      b. Create Service Implementations (Check this for more details)

Create Business object (employee), Data transfer objects (employeeDto), Data access objects  (employeeDao) , object mapper class(CoreObjectMapper) and mapping   functions(EmployeeDaoToDto).

"GetEmployeByUserName" is the function do all processing and return the result as "employee". then mapper function maps the "employee" object with "employeeDto". finally "employeeDto" will be send as response of that Service call.

         public EmployeeWorkDto GetEmployeMoreDetailsByUserName(string logInName)
        {

              // Do all Your implementation here, but return as "EmployeeWorkDto" Data transfer object
              EmployeeDao empDao = new EmployeeDao();
              return  CoreObjectMapper.EmployeeDaoToDto(empDao.GetEmployeByUserName(logInName));

        }


2. Create WCF Hosting Application (WCF Service Application Template)

      a. Modify Web.config

     i. create new end point

      <endpoint address="jsp"
          binding="customBinding"
          bindingConfiguration="jsonpBinding"
          behaviorConfiguration="restBehavior"
          contract="CoreService.IEmployeeService" />


     and specify binding , binding configuration and behavior configuration

     <endpointBehaviors>
           <behavior name="restBehavior">
              <webHttp />
          </behavior>        

    </endpointBehaviors>

    <customBinding>
           <binding name="jsonpBinding" >
               <jsonpMessageEncoding />
              <httpTransport manualAddressing="true"/>
          </binding>

   </customBinding>

and Host the Service and Check whether it is working or not by invoking the services in Browser.
Use the breakpoints for debug the application (Attach appropriate Processes - IIS App pool process and w3wp.exe  )

Service Url: 
http://ServerName:portNumber/Service.svc/jsp/GetEmployeMoreDetailsByUserName?method=JsonPCallBack&logInName=User1

Result :

JsonPCallBack(
{
    "DateOfBirth":"\/Date(969042600000+0530)\/",
    "AboutMe":"This is about Me ",
    "CurrentATC":"CMB ATC",
    "Department":"Delivery",
    "Experience":8.3,
    "Interests":["UX Design","SharePoint","Photography"],
    "Languages":[""],
    "NoofPastProjects":16,
    "PermanentATC":"CMB ATC",
    "ReportingManager":"Domain\\UserName",
    "Tier":2,
    "Since":"\/Date(852057000000+0530)\/"
}
);








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

5689 said...

zzzzz2018.8.27
pandora jewelry
jordan shoes
ralph lauren uk
giuseppe zanotti
canada goose jackets
coach outlet
kate spade outlet online
tods outlet
converse trainers
canada goose jackets