Thursday, 19 January 2012

Wcf webHttpBinding And Sending Objects:-

While using webHttpBinding in wcf service we have to send json data.Wcf internally uses the DataContractSerializer to convert json data into objects.

namespace WebHttpBindingXmlHttpRequestWcf
  public interface IService
          Person GetData(Person objPerson);

  public class Person
   public string Name { get; set; }

   public string Age { get; set; }


namespace WebHttpBindingXmlHttpRequestWcf
   public class Service : IService
     public Person GetData(Person objPerson)
       return objPerson;

     <service name="WebHttpBindingXmlHttpRequestWcf.Service">
       <endpoint address="webHttpBinding" 
         <behavior name="webHttpBindingEndpointBehavior">

Javascript File:-

//call the wcf service
function callservice(objPerson)
      //url of the service
      var url=’’;

       //here the name(objPerson) must match with the name in the operation contract
       var data = '{"objPerson":' + $.toJSON(objPerson) + '}';

//jquery ajax function to call wcf service
$.ajax({ type: 'POST',
        async: true,
        url: url,
        contentType: 'application/json; charset=utf-8',
        data: data,
        success: function (res) { alert(‘success...’); },
        error: function (err) { alert(‘error occurred...’); }

Let me know, if you have any feedback. Mail me for source code. Enjoy reading my articles…

No comments:

Post a Comment