WCF Basics

In this tutorial we will create a web service, which will be hosted by the Host, and the invoking of the web service in client application.

The Database "EmployeeDB" has to be made with the following structure.

The following steps are to be followed to create a Web Service Project.

  1. Open Microsoft Visual Studio 2008.
  2. Click File -> New Project -> Choose Visual C# from the Left pane.
  3. Choose Web and from the templates available, choose WCF Service Application.
  4. For our tutorial we will use the name of the project as ServiceDB.
  5. Create a connection to the database "EmployeeDB".
  6. Add one Linq to SQL Classes from the templates; name it as "DataClasses1.dbml".
  7. Add the above tables to the designer of the Linq to SQL.
  8. In the class file "IService1.cs" add your custom method as [Operation Contract]. Add the following code to the interface “IService1”.
    1. //Custom Method  
    2. [OperationContract]  
    3. string AllEmployees();
  9. In "Service1.svc.cs" add the above method's implementation.

    1. public string AllEmployees()  
    2. {  
    3.  DataContext db = new DataContext(@ "Data Source=DIPTI\SQLEXPRESS;Initial Catalog=EmployeeDB;Integrated Security=True");  
    4.  Table < Employee > emp = db.GetTable < Employee > ();  
    5.  var query = from c in emp select c;  
    6.  string result = "<EmployeeDB>";  
    7.  foreach(var e in query)  
    8.  {  
    9.   result += "<Employee><EmpId>" + e.EmpId +  
    10.    "</EmpId><FirstName>" + e.FirstName +  
    11.    "</FirstName><LastName>" + e.LastName +  
    12.    "</LastName><Email>" + e.EmployeeInfo.Email +  
    13.    "</Email><Address>" + e.EmployeeInfo.Address +  
    14.    "</Address><Phone>" + e.EmployeeInfo.Phone +  
    15.    "</Phone></Employee>";  
    16.  }  
    17.  result += "</EmployeeDB>";  
    18.  return result;  
    19. }

The following steps are to be followed to create a Client Application based on the Web Service. Here we will create a Windows application.

  1. Open Microsoft Visual Studio 2008.
  2. Click File -> New Project -> Choose Visual C# from the Left pane.
  3. Choose Windows and from the templates available, choose Windows Forms Application.
  4. For our tutorial we will use the name of the project as "ClientApplication".
  5. Run the Web Service application and copy the URL of the service.
  6. Add Service reference to the URL. Choose the name of the service reference as "ServiceReference1".
  7. In the Form's design window add the following controls to see the data from the database.
    1. Button as btnView
    2. Data Grid View as dataGridView1
    3. Binding Source as bindingSource1
  8. In the click event of the Button add the following code.
    1. private void btnView_Click(object sender, EventArgs e)  
    2. {  
    3.  ServiceReference1.Service1Client obj = new Service1Client();  
    4.  string list = obj.AllEmployees();  
    5.  DataSet DS = new DataSet();  
    6.  StringReader sr = new StringReader(list);  
    7.  DS.ReadXml(sr, XmlReadMode.InferSchema);  
    8.  DS.AcceptChanges();  
    9.  bindingSource1.DataSource = DS.Tables[0];  
    10.  this.dataGridView1.DataSource = bindingSource1;  
    11. }

To run the application the following steps has to be followed.

  1. Run the Service Application. (Don't close the service application)
  2. Run the Client Application.
  3. Click the Button to see the results.


Similar Articles
View All Comments