Wednesday, 1 May 2013

Calling Cross domain WCF Service Using Jquery

 Calling Cross domain WCF Service Using Jquery

--------------------------
.aspx
-------------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $.getJSON("http://localhost:1050/Rest?callback=?", null, function (data) {
                alert(data);
            });

        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
    </div>
    </form>
</body>
</html>



---------------.cs---------------

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

namespace CorssDomainService
{
    [DataContract]
    public class Order
    {
        [DataMember]
        public int ID { get; set; }
        [DataMember]
        public DateTime OrderDate { get; set; }
        [DataMember]
        public string Name { get; set; }

    }
    [ServiceContract]
    public interface IOrderService
    {
        [OperationContract]
        [WebInvoke(Method = "GET", UriTemplate = "/",
           ResponseFormat = WebMessageFormat.Json
   )]
        string ProcessOrder();

    }
    [ServiceBehavior]
    public class OrderService : IOrderService
    {
        [OperationBehavior]
        public string ProcessOrder()
        {
            Order order = new Order
            {
                ID = 1,
                OrderDate = DateTime.Now,
                Name = "Laptop"

            };

            DataContractJsonSerializer json = new DataContractJsonSerializer(typeof(Order));
            MemoryStream stream = new MemoryStream();
            json.WriteObject(stream, order);
            return Encoding.UTF8.GetString(stream.ToArray());

        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            ServiceHost host = new ServiceHost(typeof(OrderService));
            host.Open();
            Console.WriteLine("Service is ready");
            Console.ReadLine();
        }
    }
}

-------------------config-----------------

<?xml version="1.0"?>
<configuration>
  <system.serviceModel>
    <services>
      <service name="CorssDomainService.OrderService" behaviorConfiguration="serviceBehave">
        <endpoint address="rest" bindingConfiguration="crossDomain"  behaviorConfiguration="enableScriptBehaviour"
                  binding="webHttpBinding"
                  contract="CorssDomainService.IOrderService"></endpoint>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:1050/"/>
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="serviceBehave">
          <serviceMetadata httpGetEnabled="true" httpGetUrl="mex"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="enableScriptBehaviour">
          <webHttp></webHttp>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <bindings>
      <webHttpBinding>
        <binding name="crossDomain" crossDomainScriptAccessEnabled="true"></binding>
      </webHttpBinding>
    </bindings>

  </system.serviceModel>
</configuration>