|
In my last article Basic Windows Communication Foundation Service, I explained how to create basic WCF service. This article will explain how to consume WCF service by client. You can download the code of WCF service from my article Basic Windows Communication Foundation Service.
Step 1: Run the WCF service created in the last article and copy the metadata address.

Step 2: Create a new ASP.NET Web Application with name EmployeeClient. Right click on the project and slect "Add Service Reference". Enter the metadata address in the Address textbox and click on Go. All the three(DeleteEmployee, GetEmployees, SaveEmployee) operation which has been created in the WCF service will be displayed in the Operations. Enter EmployeeServiceReference in Namespace textbox. Click on OK button.

Step 3: Now EmployeeClient project will have ServiceReference with name EmployeeServiceReference. Right click on EmployeeServiceReference and select View Object Browser.
In object browser we can see Employee class has been created, EmployeeServiceContract which is proxy has been created, IEmployeeService the service contract is available.

Step 4: Add below controls in the aspx page.
1. GridView with boundfield(EmpId, FirstName, LastName, Address, Age, Designation). 2. A button to save data. 3. A button to get data. 4. A label, textbox and a button to delete data.
<asp:GridView ID="gvImage" runat="server" AutoGenerateColumns="false"> <Columns> <asp:BoundField HeaderText="EmpId" DataField="EmpId"></asp:BoundField> <asp:BoundField HeaderText="FirstName" DataField="FirstName"></asp:BoundField> <asp:BoundField HeaderText="LastName" DataField="LastName"></asp:BoundField> <asp:BoundField HeaderText="Address" DataField="Address"></asp:BoundField> <asp:BoundField HeaderText="Age" DataField="Age"></asp:BoundField> <asp:BoundField HeaderText="Designation" DataField="Designation"></asp:BoundField> </Columns> <HeaderStyle HorizontalAlign="Left" Height="0px" BackColor="#880015" ForeColor="#ffffff" Font-Bold="true" Font-Size=".75em" BorderColor="Black" BorderStyle="Solid" BorderWidth="1px" /> <AlternatingRowStyle BackColor="#eeeeee" /> </asp:GridView> <asp:Button ID="btnAdd" runat="server" onclick="btnAdd_Click" Text="Save" /> <asp:Button ID="btnGetData" runat="server" onclick="btnGetData_Click" Text="Get Data" /> <asp:Label ID="lblEmpId" runat="server" Text="Enter employee id to be deleted"></asp:Label> <asp:TextBox runat="server" ID="txtEmpId"></asp:TextBox> <asp:Button ID="btnDelete" runat="server" onclick="btnDelete_Click" Text="Delete" /> <asp:Label ID="lblError" runat="server"></asp:Label>
Step 5: Now add btnAdd_Click method to save data, btnGetData_Click method to retrieve data, btnDelete_Click method to delete data of the employee whose empid will be provided in the textbox.
protected void btnAdd_Click(object sender, EventArgs e) { EmployeeServiceClient empClient = new EmployeeServiceClient("WSHttpBinding_IEmployeeService"); Employee objEmp = new Employee(); Random rnd = new Random(); int intRnd = rnd.Next(); objEmp.EmpId = intRnd; objEmp.FirstName = "ABC" + intRnd.ToString(); objEmp.LastName = "XYZ" + intRnd.ToString(); objEmp.Address = "Add" + intRnd.ToString(); objEmp.Designation = "S/W Engg"; objEmp.Age = 21; empClient.SaveEmployee(objEmp); } protected void btnGetData_Click(object sender, EventArgs e) { EmployeeServiceClient empClient = new EmployeeServiceClient("WSHttpBinding_IEmployeeService"); Employee[] arrEmp = empClient.GetEmployees(); gvImage.DataSource = arrEmp; gvImage.DataBind(); } protected void btnDelete_Click(object sender, EventArgs e) { lblError.Text = ""; try { EmployeeServiceClient empClient = new EmployeeServiceClient("WSHttpBinding_IEmployeeService"); empClient.DeleteEmployee(int.Parse(txtEmpId.Text)); gvImage.DataSource = empClient.GetEmployees(); gvImage.DataBind(); } catch (Exception ex) { lblError.Text = "Error occurred."; } } EmployeeServiceClient methods take parameter endpointConfigurationName WSHttpBinding_IEmployeeService. endpointConfigurationName gets added in the Web.config file once you add WCF service.

Step 6: Now run the WCF service first followed by EmployeeClient project. You will get Save, Get Data, TextBox and Delete button.
Click on Save which will save the data, Get Data will retrieve the data and Delete will delete the matching record of the empid provided in the textbox.
|