Wednesday, November 4, 2009

Call from Javascript to Server via WCF/XML

Here is a simple example of calling to server from javascript side based WCF,XML

This example contain 3 options:
· Return string from server
· Return object from server
· Send object to server

1. create a new project name = ‘wcf2’

2. add new aspx page name Default.aspx.

3. paste code to javascript:






4. create new wcf service name 'Service.svc'

5. paste code the interface fileusing System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Web;

// NOTE: If you change the interface name "IService" here, you must also update the reference to "IService" in Web.config.
[ServiceContract]
public interface IService
{
[OperationContract]
string GetsProperty1();
[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Xml,
ResponseFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "/Person")]
Person GetDTO();

///
/// This is not working a real post. it still goes to the quesry string
///

[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Xml,
ResponseFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "Person2?Property1={sProperty1}&Property2={nProperty2}&Property3={bProperty3}")]
Person GetDTO2(string sProperty1, int nProperty2, bool bProperty3);

[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Xml,
ResponseFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "Person4?Property1={sProperty1}&Property2={nProperty2}&Property3={bProperty3}")]
Person GetDTO4(string sProperty1, int nProperty2, bool bProperty3);
}

// Use a data contract as illustrated in the sample below to add composite types to service operations.
[DataContract]
public class Person
{
[DataMember]
public string sProperty1 = "123";

[DataMember]
public int nProperty2 = 123;

[DataMember]
public bool bProperty3 = true;

}

6. paste code to service fileusing System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Text;
using System.IO;

// NOTE: If you change the class name "Service" here, you must also update the reference to "Service" in Web.config and in the associated .svc file.
public class Service : IService
{
private Person oPerson = new Person();

public string GetsProperty1()
{
return oPerson.sProperty1;
}
public Person GetDTO()
{
return this.oPerson;
}

public Person GetDTO2(string sProperty1, int nProperty2, bool bProperty3)
{
return this.oPerson;
}

public Person GetDTO4(string sProperty1, int nProperty2, bool bProperty3)
{
return this.oPerson;
}

public int GetDTO3(string sProperty1, int nProperty2, bool bProperty3)
{
return 590;
}
}
7. run project when default.aspx file is the startup page....
good luck,
Rami Heleg