In the preceding window, you have clearly seen that the WCF Service reference named "ServiceReference" is added into the WPF application. I hope you understand how to add the WCF Service reference into the WPF 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 WPF Application
We have added the WCF Service reference into our WPF application. Now the next thing is how to call the WCF Service method that we created in our WCF Service Application from the WPF Application.
The following is the procedure:
1. Go to the Agecalculator Form of our WPF 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 MainWindow.xaml.cs form will be as follows:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
-
- namespace ConsumingWCFServiceInWPFApp
- {
-
-
-
- public partial class MainWindow : Window
- {
- public MainWindow()
- {
- InitializeComponent();
- }
-
- private void button1_Click(object sender, RoutedEventArgs e)
- {
- int day, Month, Year, TotalDays;
-
-
- ServiceReference.Service1Client age = new ServiceReference.Service1Client();
-
-
- day = int.Parse(textBox1.Text);
- Month = int.Parse(textBox2.Text);
- Year = int.Parse(textBox3.Text);
-
-
- TotalDays = age.calculateDays(day, Month, Year);
-
-
- MessageBox.Show("You are Currently " + Convert.ToString(TotalDays) + " days old");
- }
- }
- }
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 the Textbox1, Textbox2 and Textbox3.
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 Message box 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 WPF application and provide the input of day, Month and Year. I will enter my Date of Birth and then I will click on the "Calculate" button, it will show the output as in the following:
In the preceding screen, you see that currently, I am 9212 days old, which means that for the last 9212 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 a WPF application using my two articles. If you have any suggestion regarding this article then please contact me.