Monday, 18 February 2013

Filled Under:
, ,

C#.net How to: Consume a WCF service in WPF application

What we learn from earlier articles that WCF is a Windows Communucation Foundation which is meant for designing and deploying Distributed Application under SOA (Service oriented Architecture). And WPF is a Windows Presentation Foundation is a vector based graphical subsytem usefult to build windows application with rich user interface.

Do you know we can host and call wcf service from so many places? Like we can host wcf service in IIS and consume in console application, we can also host wcf service in Windows service and can consume wcf service hosted in Windows Service. WCF also supports self-hosting. We have already covered all these WCF service hosting and consuming options.
WCF in WPF
WCF in WPF
In this wpf tutorial we are going to learn how to consume a WCF service in WPF application. So before we start I am assuming that your WCF service is already built and ready to consume. 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.
I am using the same WCF service that was created in that article. In brief 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
WCF Service URL
WCF with WPF is a good combination and a goal here is to retrieve a welcome message string from WCF service and display it in a textblock in WPF application.

Steps to consume WCF service in WPF application.

  1. Open Visual Studio IDE and create a new WPF project. File -> New -> Project..
    WPF Application - Create new Project
    WPF Application - Create new Project
  2. As shown in below image, select WPF Application and name it as FirstWPFApplication.
    Create a new WPF Application
    Create a new WPF Application
    Select the location of the project and Click on "OK" button to create a new WPF project.
  3. As soon as you click on Ok button, the MainWindows.XAML would look like –
    WPF Application - MainWindow.xaml in Design mode
    WPF Application - MainWindow.xaml in Design mode
  4. Now we need a Textbox control to display a message string coming from WCF service in wpf. To add textbox go to toolbox and add a textbox control, as shown in below image –
    WPF Application - Adding Textbox control
    WPF Application - Adding Textbox control
  5. Now add a button control as shown below. The purpose of adding a button control in our WPF application is to show a message in textbox on click event of the button.
    WPF Application - Adding Button control
    WPF Application - Adding Button control
  6. We need to add button click event. In this event we would code to call a wcf service in wpf application. click on "Events" tab in Properties window to add some events to button. In this example we are going to implement "Button_Click" event, hence write "Button_Click" in Click event and press enter.
    WPF Application - Button_Click event
    WPF Application - Selecting Button_Click event
    As soon as you press enter key, a click event code will appear in MainWindow.xaml.cs file.
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, RoutedEventArgs e)
            {
               
            }
        }
  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.
    WCF Service Methods
    WCF Service Methods
    Click on Ok to finish the Add service reference wizard.
  9. Your WCF service is added in WPF application. Now enter below code in Button1_Click event of MainWindow.xaml.cs file to consume 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 application is completed. Now save all files and hit F5 key to execute the WPF application.
  11. Click on "Button", and it will show the text in textbox. Look at below image for final output -
    Consume WCF service in WPF
    Consume WCF service in WPF
In this c# tutorial we learned how WPF consumes WCF service. You can also consume a WCF service in Asp.net web application.

I hope you enjoyed the article. If you find this article helpful, then could you please share the article on your social media?

More articles -

Protected by Copyscape Plagiarism Check

Sharing is Caring...





3 comments:

  1. This comment has been removed by a blog administrator.

    ReplyDelete
  2. Simple and straightforward ! Thanks

    ReplyDelete