Sie sind auf Seite 1von 7

October 12, 2013

[WCF BASIC DEMO]

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. 1. 2. 3. 4. Data field S_ID S_FullName S_Birthdate S_Address Data type Int Text DateTime Text

CUSC 2013

Page 1

October 12, 2013

[WCF BASIC DEMO]

Open visual studio 2008 (or later) File -> Create Project

Select as figure

Right click on project -> Add new Item

Select as figure CUSC 2013 Page 2

October 12, 2013

[WCF BASIC DEMO]

Right click on project -> Add new Item

Select as figure

Modified class as
namespace students { [DataContract] public class StudentDetail { [DataMember] public int ID; [DataMember] public string FullName; [DataMember] public DateTime Birthdate; [DataMember] public string Address; } }

Modified IStudent interface as


namespace students { [ServiceContract] public interface IStudent { [OperationContract] DataSet List(); [OperationContract] StudentDetail Detail(int ID); } }

CUSC 2013

Page 3

October 12, 2013

[WCF BASIC DEMO]

Edit Student service as

namespace students { public class Student : IStudent { #region IStudent Members public DataSet List() { DataSet dsS = new DataSet(); // insert C# code to get data from student table return dsS; } public StudentDetail Detail(int ID) { StudentDetail sDetail = new StudentDetail(); // insert C# code to find data from student table with ID from input param // assign value for all properties of sDetail return sDetail; } #endregion } }

Now right click on service and select view in browser

wsdl of service

CUSC 2013

Page 4

October 12, 2013

[WCF BASIC DEMO]

Right click on solution -> add new project

Select as figure

Design form as

DataGridView

GUI of client CUSC 2013 Page 5

October 12, 2013

[WCF BASIC DEMO]

Add service reference as

Modify form load event:


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

Double click on button Find and insert code as


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

CUSC 2013

Page 6

October 12, 2013

[WCF BASIC DEMO]

Double click on button List and insert code as


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

Now run client

Client at runtime

CUSC 2013

Page 7

Das könnte Ihnen auch gefallen