- Create User defined data type called Customer. This data type should be identified for serialization and deserialization by mentioning with [DataContract] and [DataMember] attribute.
- Add Wcf Service at DataContractinWCF.Web(Silverlight Web Project),We get IService1.cs and Service1.svc files.
- IService1.cs :
namespace DataContractinWCF.Web
{
[ServiceContract]
public interface IService1
{
[OperationContract]
List<Customer> GetCustomerData(int CustomerID);
}
[DataContract]
public class Customer
{
private string m_Name;
private int m_Age;
private int m_Salary;
private string m_Designation;
private string m_Manager; [DataMember]
public string Name
{
get { return m_Name; }
set { m_Name = value; }
}
[DataMember]
public int Age
{
get { return m_Age; }
set { m_Age = value; }
}
[DataMember]
public int Salary
{
get { return m_Salary; }
set { m_Salary = value; }
}
[DataMember]
public string Designation
{
get { return m_Designation; }
set { m_Designation = value; }
}
[DataMember]
public string Manager
{
get { return m_Manager; }
set { m_Manager = value; }
}
}
In GetCustomerData method we have created the Customer instance and return to the client.
As we have created the data contract for the Employee class, client will aware of this instance whenever he creates proxy for the service.
- Service1.svc.cs :
namespace DataContractinWCF.Web
{
public classService1 : IService1
{
public Cusomer GetCustomerData (int empId)
{
Cusomer CusDetail = new Cusomer ();
//Do something to get Customer details and assign to CusDetail properties
return CusDetail;
}
}
- Client Side
On client side we can create the proxy for the service and make use of it. The client side code is shown below.
Build Web project and then Add Service reference to client side.Now we can access the service methods at client side.
On button click event access the service as follows :
Public void btnGetDetails_Click(object sender, EventArgs e) {
ServiceReference1.Service1Client service = new ServiceReference1.Service1Client();
Cusomer CusDetails;
CusDetails = service.GetCustomerData(CusomerId);
//Do something on Customer details
}