Xây dựng và sử dụng WCF đơn giản (WCF basic demo for student)


Objectives

1. Create WCF service (host on web server )

2. Using WCF service from window form application

Hits: Step for create and using service
  • Create WCF service “Student” with two operations “list()” and “detail(id)”
    1. Define service interface (ServiceContract)
    2. Define class describe detail of student (DataContract)
    3. Define Service by implement from server interface
  • Using WCF service from window form application
    1. Find service description of WCF “student”
    2. Reference WCF service
    3. Call operations of student service

Student table

No. Data field Data type
1. S_ID Int
2. S_FullName Text
3. S_Birthdate DateTime
4. S_Address Text

WCF basic demo 2013 (PDF version of this post)

http://www.mediafire.com/?ji9uiz5l70d8n (Source code of this post)

BEGIN

Open visual studio 2008 (or later)

File -> Create Project

clip_image002

Select as figure

Right click on project -> Add new Item

clip_image004

Select as figure

Right click on project -> Add new Item

clip_image006

Select as figure

Modified class as

1 namespace students 2 { 3 [DataContract] 4 public class StudentDetail 5 { 6 [DataMember] 7 public int ID; 8 [DataMember] 9 public string FullName; 10 [DataMember] 11 public DateTime Birthdate; 12 [DataMember] 13 public string Address; 14 } 15 } 16 17 Modified IStudent interface as 18 19 namespace students 20 { 21 [ServiceContract] 22 public interface IStudent 23 { 24 [OperationContract] 25 DataSet List(); 26 [OperationContract] 27 StudentDetail Detail(int ID); 28 } 29 }

Edit Student service as

clip_image002[4]

1 namespace students 2 { 3 public class Student : IStudent 4 { 5 public DataSet List() 6 { 7 DataSet dsS = new DataSet(); 8 // insert C# code to get data from student table 9 return dsS; 10 } 11 public StudentDetail Detail(int ID) 12 { 13 StudentDetail sDetail = new StudentDetail(); 14 return sDetail; 15 } 16 } 17 } 18

Now right click on service and select view in browser

clip_image005

Right click on solution -> add new project

clip_image007

Select as figure

Design form as

clip_image010

GUI of client

Add service reference as

clip_image012

Modify form load event:

1 wcf.StudentClient proxy; 2 private void Form1_Load(object sender, EventArgs e) 3 { 4 proxy = new WCF_Client.wcf.StudentClient(); 5 }

Double click on button Find and insert code as

1 private void btnFind_Click(object sender, EventArgs e) 2 { 3 wcf.StudentDetail detail = proxy.Detail(Convert.ToInt32(txtID.Text)); 4 MessageBox.Show("Student id: " + detail.ID + "\nStudent fullname: " + detail.FullName); 5 } 6

Double click on button List and insert code as

1 private void btnList_Click(object sender, EventArgs e) 2 { 3 dgrStudent.DataSource = proxy.List(); 4 dgrStudent.DataMember = "student"; 5 }

Now run client

clip_image014

Client at runtime

Xem hướng dẫn video trên

Mã nguồn:  http://www.mediafire.com/download/f4l6dpz2wla513w/WebApplication1.rar

Một suy nghĩ 3 thoughts on “Xây dựng và sử dụng WCF đơn giản (WCF basic demo for student)

Phản hồi