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)”
- Define service interface (ServiceContract)
- Define class describe detail of student (DataContract)
- Define Service by implement from server interface
- Using WCF service from window form application
- Find service description of WCF “student”
- Reference WCF service
- 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
Select as figure
Right click on project -> Add new Item
Select as figure
Right click on project -> Add new Item
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
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
Right click on solution -> add new project
Select as figure
Design form as
GUI of client
Add service reference as
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
Client at runtime
Xem hướng dẫn video trên
Mã nguồn: http://www.mediafire.com/download/f4l6dpz2wla513w/WebApplication1.rar
No comments:
Post a Comment