Consume a Custom WCF Service in a Console Application

In this article, you will see how to consume the custom WCF service in a Console Application.
SQL database details
Employee_Details table has the following data:
I have created a custom WCF service and hosted it in IIS http://localhost:9002/EmployeesService/EmployeesService.svc which is used to fetch the data from the above SQL database table for the specified id.
public Employee_Detail getEmployeebyId(int empId)
Return Value
Type: EmployeesWCFService.Employee_Detail
You will get the following result for the specified id 102:
Create a Console application
Steps Involved
  1. Open Visual Studio 2010 by going to Start |All Programs | Microsoft Visual Studio 2010 | Right click on MicrosoftVisual Studio 2010 and click on Run as Administrator.
  2. Go to File tab, click on New and thenclick on Project.
  3. In the New Project dialog box, expand theVisual C# node, and then select the Windows node.
  4. In the Templates pane, select ConsoleApplication.
  5. Enter the Name and then click OK.
  6. In the Solution Explorer, right click onthe solution and then click on Properties.
  7. Select the Application tab, check whether".Net Framework 3.5" is selected for Target Framework.
  8. Select the Build tab; check whether "AnyCPU" is selected for Platform Target.
  9. In the Solution Explorer, right click onthe References folder and click on Add Service Reference.
  10. The Add Service Reference wizard will pop up.
  11. In the Address section, enter the WCFservice URL and then click on Go button.

  12. Enter the proper name for the Namespace.
  13. Click on Ok.
  14. Service Reference will be addedsuccessfully.
  15. Replace Program.cs with the followingcode.
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Text;  
    5. using ConsumeCustomWCF.EmployeesServiceReference;  
    6. using System.Net;  
    7. namespace ConsumeCustomWCF  
    8. {  
    9.     class Program  
    10.     {  
    11.         static void Main(string[] args)  
    12.         {  
    13.             EmployeesServiceClient serviceClient = new EmployeesServiceClient();  
    14.             EmployeesServiceReference.Employee_Detail emp = serviceClient.getEmployeebyId(102);  
    15.             Console.WriteLine(emp.EmpId);  
    16.             Console.WriteLine(emp.EmployeeName);  
    17.             Console.WriteLine(emp.EmployeeDesignation);  
    18.             Console.WriteLine(emp.Dept);  
    19.             Console.WriteLine(emp.WorkLocation);  
    20.             Console.ReadLine();  
    21.         }  
    22.     }  
    23. }
  16. Build the solution.
  17. Hit F5.
  18. Output:

Thus in this article, you have seen how to consume a custom WCF service in a Console application.