Friday 20 July 2012

WCF Basic Tutorial


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 ->
3)      Choose Visual C# from the Left pane.
4)      Choose Web and from the templates available, choose WCF Service Application.
5)      For our tutorial we will use the name of the project as ServiceDB.
6)      Create a connection to the database "EmployeeDB".
7)      Add one Linq to SQL Classes from the templates; name it as "DataClasses1.dbml".
8)      Add the above tables to the designer of the Linq to SQL.
9)      In the class file "IService1.cs" add your custom method as [Operation Contract]. Add the following code to the interface “IService1”.
//Custom Method
        [OperationContract]
        string AllEmployees();
10)   In "Service1.svc.cs" add the above method's implementation.
public string AllEmployees()
        {
            DataContext db = new DataContext(@"Data Source=DIPTI\SQLEXPRESS;Initial Catalog=EmployeeDB;Integrated Security=True");
            Table<Employee> emp = db.GetTable<Employee>();
            var query = from c in emp select c;

            string result = "<EmployeeDB>";
            foreach (var e in query)
            {
                result +=  "<Employee><EmpId>"+e.EmpId+
                                "</EmpId><FirstName>"+e.FirstName+
                                "</FirstName><LastName>"+e.LastName+
                                "</LastName><Email>"+e.EmployeeInfo.Email+
                                "</Email><Address>"+e.EmployeeInfo.Address+
                                "</Address><Phone>"+e.EmployeeInfo.Phone+
                                "</Phone></Employee>";
            }

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

No comments:

Post a Comment