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:
- <?xml version="1.0" encoding="utf-8" ?>
- <configuration>
- <startup>
- <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0.3,Profile=Client" />
- </startup>
- <system.serviceModel>
- <bindings>
- <basicHttpBinding>
- <binding name="BasicHttpBinding_IService1" />
- </basicHttpBinding>
- </bindings>
- <client>
- <endpoint address="http://localhost:3901/Service1.svc" binding="basicHttpBinding"
- bindingConfiguration="BasicHttpBinding_IService1" contract="ServiceReference.IService1"
- name="BasicHttpBinding_IService1" />
- </client>
- </system.serviceModel>
- </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:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
-
- namespace ConsumingWCFServiceInConsoleApp
- {
- class Program
- {
- static void Main(string[] args)
- {
-
- int day, Month, Year, TotalDays;
-
-
- ServiceReference.Service1Client age = new ServiceReference.Service1Client();
-
-
- Console.WriteLine("Day");
- day = int.Parse(Console.ReadLine());
- Console.WriteLine("Month");
- Month = int.Parse(Console.ReadLine());
- Console.WriteLine("Year");
- Year = int.Parse(Console.ReadLine());
-
-
- TotalDays = age.calculateDays(day, Month, Year);
- Console.Clear();
- Console.WriteLine();
-
- Console.WriteLine("You are Currently " + Convert.ToString(TotalDays) + " days old");
- Console.ReadLine();
- }
- }
- }
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.
Note:
- For detailed code please download the Zip file attached above.
- Also refer to my previous article about Creating WCF Service.
Summary
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.