Consuming Web Service In WPF Application

Background

In my previous article we learned how to create a simple Web Service and  consume a Web Service in a web application. This article explains how to consume the Web Service in an WPF 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.

If you are a beginner and want to learn about Web Services then please refer to the following articles of mine:

  1. Introduction to Web Service with Example in ASP.Net
  2. Consuming Web Service In an ASP.Net Web Application
  3. Consuming Web Service In a Console Application

Requirements

You need to keep a Web Service application in running mode so it can be accessible for use, so go to my article
Introduction to Web Service with Example in ASP.Net and create a Web Service and keep it in running mode; I hope you have done that.

So let us create the simple WPF application as:

  1. "Start" - "All Programs" - "Microsoft Visual Studio 2010".
  2. "File" - "New" - "Project..." then in the New Project WPF Application "C#"".
  3. Provide the project a name, such as "ConsumingWebServiceInWPF" or another as you wish and specify the location.

Then design the Windows Forms form as in the following:

 
 In the preceding form, note that I have used three text boxes to get input from users because we know that our Web Service method created as in my article takes the three input values day, month and year so I have taken the three text boxes and button click event. We will call the Web Service method and the output will be displayed in a message box.

I hope you understand it.

Adding a Web Service Reference in the ASP.NET WPF application

The most important task when consuming a Web Service in any application is adding the Web Service reference into the application. So how to add it? Let us see the procedure.


1. Right-click on the ASP.NET Windows application and click on "Add Service Reference" as in the following:

AddServiRef.png

2.  Then after clicking on the preceding option, the following window will appear, then click on the "Advanced" tab.

Advtab.png

3. Now after clicking on the Advanced tab, it will show the following window, then click on the "Add Web Reference" option as in the following in a circle:

FAddsevice.png
.
4. After clicking on the Add Web Reference tab, it will show the following window. Now this is a very important step, when adding the web reference to the ASP.NET Windows application. Since you see the "URL" option in the following window, on that window we need to paste or type the Web Service URL address.

RefScreen.png
 

Now in the preceding box, paste the URL of the Web Service that you have seen in the browser URL while running the Web Service.


-----What After Pasting the URL in the preceding URL box

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

The Name of the Web Service is "WebService" because I have given the class name as Web Service, that's why the name is Web Services, in your case it might be different or the class name is anything so you can use any name for Web Service so don't be confused about it. Then click on the "OK" button.

Then after adding the Web Service reference in the WPF application the Solution Explorer will look as in the following:
 
 

In the preceding window, you have clearly seen that the Web Service reference named "localhostService" has been added to the  WPF application. I hope you understand how to add the Web Service reference into the WPF  application.

Calling the Web Service method from the WPF application

We have added the Web Service reference to our  WPF application. Now next is how to call the Web Service method that we created in our Web Service application from the WPF application.

The following is the procedure:

  1.  Double-click on the button that we placed on the WPF form.
  2.  Now write the following code in the button click to create the object of the Web Service class:

localhostservice.webservice objservice=new localhostservice.webservice ();

 In the code above, I have created the object of Web Service class named "objservice" followed by the Web reference name ("localhostservice") and Web Service class ("webservice"), I hope you understand how to create the object of the Web Service class.

The entire code will be as follows:

  1. using System;  
  2. using System.Windows;  
  3.   
  4. namespace ConsumingWebServiceInWPF  
  5. {  
  6.     /// <summary>  
  7.     /// Interaction logic for MainWindow.xaml  
  8.     /// </summary>  
  9.     public partial class MainWindow : Window  
  10.     {  
  11.         //creating the object of web service class  
  12.         localhostService.WebService objWebservice = new localhostService.WebService();  
  13.         public MainWindow()  
  14.         {  
  15.             InitializeComponent();  
  16.         }  
  17.   
  18.         private void button1_Click(object sender, RoutedEventArgs e)  
  19.         {  
  20.             int day, Month, Year,Days;  
  21.             day = Convert.ToInt32(textBox1.Text);  
  22.             Month = Convert.ToInt32(textBox3.Text);  
  23.             Year = Convert.ToInt32(textBox2.Text);  
  24.             Days = objWebservice.converttodaysweb(day, Month, Year);  
  25.             MessageBox.Show("You are   " + Days + "   days old");  
  26.         }  
  27.     }  

Code Explanation

In the code above, I first created the object of the Web Service class named "objservice" followed by Web reference name ("localhostservice") and Web Service class ("webservice").

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 new Web Service method takes three parameters, so I ed the three input parameters "day", "month" and "year" to the Web Service method "converttodaysweb".

Then I declared another integer variable, "days", to store the values returned by the Web Service method "converttodaysweb".

And finally I displayed the values returned by the Web Service method "converttodaysweb" on the Messagebox using the variable "days"  because, as you know, we have stored the returned values of the method into the variable days, so the final result will be stored in the variable days.

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 9122 days old, that means that for the last 9122 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 a Web Service.

Summary

I hope that beginners as well as students understand the creation and consumption of Web Services in WPF  Applications using my two articles. If you have any suggestion regarding this articles then please contact me. Student suggestions are also welcomed.


Similar Articles