Consuming WCF Service In Console Application


In my previous article 
Creating WCF Service we learned how to create a simple WCF Service application. This article explains how to consume the WCF Service application in a Console Application.

And remember, I have written this article only focusing on beginners. So let us start step-by-step so beginners can understand it very easily.


You need to keep a WCF Service Application in running mode so it can be accessible for use, so go to my article Creating WCF Service
and create a WCF service and keep it in running mode. I hope you have done that.


Many beginners are confused about what consuming means, but its very simple; it means to use the WCF Services in an application.

For example, I have created a WCF Service and now I want to use it in a real requirement so I used it in Console  Application. In other words, I am consuming the WCF Service in a Console Application. Similarly you can use the same WCF Service in Java and other applications.

I hope you understand the word "consuming".

 So let us create the simple Console Application using:
  1. "Start" - "All Programs" - "Microsoft Visual Studio 2010".
  2. "File" - "New" - "Project..." then Consoles Application.
  3. Give the project a name, such as "ConsumingWCFServiceInConsoleApp" or another as you wish and specify the location. 
Now your Solution Explorer for the Console Application will be as in the following:
Adding a WCF Service Reference to the Console Application

The most important task when consuming a WCF Service in a Console Application is to add the WCF Service Reference into the Console Application. So how to add it? Let us see how.

1. Right-click on the
Console Application and click on "Add Service Reference" as in the following:


Then after clicking on the above option, the following window will appear. Now this is a very important step, when adding the WCF service reference to the Console Application. Since you see the "URL" option in the following window, on that window we need to paste or type in the WCF Service URL address.
 So how to add the URL Reference in the preceding URL box? Let us see the procedure again.
  • Run the WCF Service we created in my article Creating WCF Service by clicking on F5 or whatever other option you are familiar with, it will then show the following WFC Client window and copy the address as:

Now you just need to copy the preceding URL that I circled in Red and paste it into the window URL option, as shown in the following and click the Discover button, it will show the following output:

After pasting the URL in the preceding window box, click on the Green right headed arrow button, it will discover the WCF Services available related to that URL address and you see that in that related URL a WCF Service is found message is displayed along with the WCF Service name, "WCF Services" in the preceding right hand side window.

  • WCF Service Reference Name

In the right hand corner of the window you can see the option for the Service Reference name; the Service Reference name is anything you wish and this name will be added to your console   Application as an alias name for the WCF Service. In my article I have given the reference name as "ServiceReference".

Then after adding the WCF Service Reference in the Console Application the Solution Explorer will look as in the following: 


In the preceding window, you have clearly seen that the WCF Service Reference named "ServiceReference" is added into the Console Application. I hope you understand how to add the WCF Service Reference into the Console Application.

Now after adding the WCF Service Reference the following endpoints are by default added into the App.config file as:
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <configuration>  
  3.     <startup>   
  4.         <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0.3,Profile=Client" />  
  5.     </startup>  
  6.     <system.serviceModel>  
  7.         <bindings>  
  8.             <basicHttpBinding>  
  9.                 <binding name="BasicHttpBinding_IService1" />  
  10.             </basicHttpBinding>  
  11.         </bindings>  
  12.         <client>  
  13.             <endpoint address="http://localhost:3901/Service1.svc" binding="basicHttpBinding"  
  14.                 bindingConfiguration="BasicHttpBinding_IService1" contract="ServiceReference.IService1"  
  15.                 name="BasicHttpBinding_IService1" />  
  16.         </client>  
  17.     </system.serviceModel>  
  18. </configuration> 

Calling the WCF Service method from the Console Application

We have added the WCF Service Reference into our Console Application. Now next is how to call the WCF Service method that we created in our WCF  Service Application from the console  Application.

The following is the procedure:

    1. Go to the Agecalculator Form of our Console Application and double-click on the button that we have placed on the Agecalculator form.

    2. Now write the following code in the button click to create the object of the WCF Service class as:

          ServiceReference.Service1Client age = new ServiceReference.Service1Client();
In the code above, I have created the object named "age" of the WCF Service client followed by the Web reference name ("ServiveReference") and WCF  Service client, I hope you understand how to create the object of the WCF Service client.
 The entire code of the Program.cs will be as follows:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  6. namespace ConsumingWCFServiceInConsoleApp  
  7. {  
  8.     class Program  
  9.     {  
  10.         static void Main(string[] args)  
  11.         {  
  13.             int day, Month, Year, TotalDays;  
  15.             //creating the object of WCF service client       
  16.             ServiceReference.Service1Client age = new ServiceReference.Service1Client();  
  18.             //assigning the input values to the variables    
  19.             Console.WriteLine("Day");  
  20.             day = int.Parse(Console.ReadLine());  
  21.             Console.WriteLine("Month");  
  22.             Month = int.Parse(Console.ReadLine());  
  23.             Console.WriteLine("Year");  
  24.             Year = int.Parse(Console.ReadLine());  
  26.             //assigning the output value from service Response       
  27.             TotalDays = age.calculateDays(day, Month, Year);  
  28.             Console.Clear();  
  29.             Console.WriteLine();  
  30.             //assigning the output value to the lable to show user       
  31.             Console.WriteLine("You are Currently " + Convert.ToString(TotalDays) + " days old");  
  32.             Console.ReadLine();  
  33.         }  
  34.     }  

Code Explanation

In the code above, I first created the object as "age" of the WCF Service class followed by the WCF Service Reference name ("ServiceReference") and WCF Service Client ("service1client").

Then I declared the three integer variables "day", "Month" and "Year" to store the values provided by the user as input from console.

Now, in the next step, as you know our WCF Service method takes three parameters, so I ed the three input parameters "day", "Month" and "Year" to the WCF Service method "calculateDays". Then I declared another integer variable, "TotalDays", to store the values returned by the WCF Service method "calculateDays". And finally I displayed the values returned by the WCF Service method "calculateDays" on the console screen using the variable "TotalDays" because, as you know, we have stored the returned values of the method into the variable TotalDays, so the final result will be stored in the variable TotalDays.

Now, run the Console Application and provide the input of day, Month and Year. I will enter my Date of Birth as:

Now press the Enter button of the keyboard, it will show the output as in the following: 

In the preceding screen, you see that currently, I am 9199 days old, which means that for the last 9199 days, I have been on this earth.

  • For detailed code please download the Zip file attached above.
  • Also refer to my previous article about Creating WCF Service.


I hope that beginners as well as students understand the creation and consumption of a WCF Service Application in Console Applications using my two articles. If you have any suggestion regarding this article then please contact me.


Similar Articles