In this article we are going to learn how to consume a WCF service in WPF application. If you are new to the WCF, or you have limited knowledge on how to create a WCF service then I would suggest you to refer my previous article that shows how to create a WCF service.
In one of my previous article I have explained how to call WCF service in console application or in asp.net. There are again so many ways you can host your WCF service. Refer this page to know more on the WCF hosting.
The example given in this article is very basic & simple example and is just to understand the concept to consume the wcf service in wpf.
Before we start I am assuming that the WCF service is already built and ready to consume. In this article I am using the same WCF service that was created in article - how to create a WCF service. We have implemented one method (i.e. OperationContract) called as WelComeMessage that returns a string message. You must know the URL of your wcf service. Run your WCF application and you will get URL from below screen. Note down this URL.![]() |
| WCF Service URL |
Follow below steps to learn how to call WCF service in WPF application –
1. Open Visual Studio IDE and create a new WPF project. File -> New -> Project..
![]() |
| Create a new WPF Project |
2. As shown in below image, select WPF Application and name it as
FirstWPFApplication.
![]() |
| New WPF Project |
3. The WPF project screen will be like –
![]() |
| WPF Project Initial Screen |
4. Add a textbox, as shown in below image –
![]() |
| WPF Project - Add Textbox in XAML |
5. Now add a button control as shown below.
![]() |
| WPF Project - Add button in XAML |
6. We need to add button click event, on which we can display the string message
coming from WCF service in a textbox.
![]() |
| WPF Project - Button_Click event in WPF |
enter key, a click event code will be added in MainWindow.xaml.cs file.
![]() |
| WPF Project - MainWindows.xaml.cs in WPF |
7. Now the next step is to add WCF service url as a service reference to this WPF
application. Goto solution explorer, right click on project name
“FirstWPFApplication” and then click on “Add Service Reference..”
8. Enter your WCF URL and click on Go button. Make sure the WCF service is running
before clicking on Go button –
![]() |
| WPF Project - Adding WCF service in WPF |
Click on Ok button to finish the Add service reference wizard.
9. Now enter code in Button1_Click event of MainWindow.xaml.cs file to consume the WCF service.
private void Button1_Click(object sender, RoutedEventArgs e)
{
MyFirstWCFWebServiceReference.Service1Client wcfServiceClient = new MyFirstWCFWebServiceReference.Service1Client();
textBox1.Text = wcfServiceClient.WelComeMessage("Ashish Ramteke");
}
10. The procedure of calling a wcf service in WPF is completed. Now save all files and
hit F5 key to execute the WPF application.
11. Now click on “Button”, and it will show the text in textbox. The final output will be
like –
![]() |
| WPF Project - Consume WCF service in WPF |
Please feel free to comment if you have any queries. I would try my best to resolve your doubts. If you are happy with the contents then please share this article with your friends on your favorite social media with the help of below "Sharing is Caring" Widget.
Sharing is Caring »»
|
|
|
Tweet |










LOL! That was really funny! YcDress and DressesForBest
ReplyDelete