Click here to Skip to main content
13,295,178 members (69,714 online)
Click here to Skip to main content
Add your own
alternative version


26 bookmarked
Posted 20 Jul 2009

How to Consume a Web Service in a WPF Application?

, 20 Jul 2009
Rate this:
Please Sign up or sign in to vote.
This article explains how to consume a Web Service in a WPF Windows application. It requires binding of WPF controls to data coming from a Web Service.


This article explains how to consume a Web Service in a WPF Windows application. It requires binding of WPF controls to data coming from a Web Service.

The main object participating in the binding process is the DataContext. It is available as a Dependency Property for the WPF Window and most of the WPF Framework Elements. Data context is a concept that allows elements to inherit information from their parent elements about the data source that is used for binding, as well as other characteristics of the binding, such as the path. Data context can be set directly to a Common Language Runtime (CLR) object, with the bindings evaluating to properties of that object. Alternatively, you can set the data context to a DataSourceProvider object. This Dependency Property inherits property values. If there are child elements without other values for DataContext established through local values or styles, then the property system will set the value to be the DataContext value of the nearest parent element with this value assigned.

Creating a Web Service

To demonstrate the purposes of this article, we must first create an ASP.NET Web Service project named “MyWebService” and add a custom class WebContent to the project. This class has a string property, GreetingMessage, and a field Names, a string array.

public class WebContent
    string greet;
    public string GreetingString
            return greet;
            greet = value;
    public string[] Names;

For convenience, create a Web Service method that sets the values of the properties and return the object of this custom type.

Here is the code for the Web Service definition:

public class Service1 : System.Web.Services.WebService

    public WebContent CreateObject()
        WebContent wc = new WebContent();
        wc.GreetingString = "Hello World";
        wc.Names = new string[5] {"Bala", "Murali", 
           "Balaji", "Krish", "Chris" };
        return wc;

Build the Web Service project and do the test run to execute the method.

Creating a WPF Window that consumes the Web Service

Now, to consume the Web Service created above in the WPF application, create a new WPF window project and add a few TextBlock and ListBox controls on to the form. To call a Web Service, add a Service Reference from the Project menu and supply the URL (http://localhost/MyWebService/service1.asmx) in the dialog shown.

Create a proxy for the Web Service reference in the OnLoad event handler of the WPF window, and call the CreateObject method as below:

void OnLoad(object sender, RoutedEventArgs e)
    WebReference.Service1SoapClient proxy = 
                 new WebReference.Service1SoapClient();
    this.DataContext = proxy.CreateObject();

The output of the CreateObject method is of WebContent type and is set as a DataContext for the Window object. Once we do this, all accessible properties of the type are available for binding to different elements in the window.

Now, modify the XAML code with bindings to the properties, as below:

<TextBlock Grid.Column="0" Grid.Row="0" 

  FontWeight="Bold" Text="Greeting Message:"/>
<TextBlock Grid.Column="1" Grid.Row="0" 

  FontWeight="Bold" Text="{Binding Path=GreetingString}"  Foreground="Blue" />
<TextBlock Grid.Column="0" Grid.Row="1" 

  FontWeight="Bold" Text="List of Names: "/>
<ListBox  Grid.Column="1" Grid.Row="1" Margin="0,3,0,0" 

  ItemsSource="{Binding Path=Names}"  Foreground="Blue" />

Build the WPF window project and press F5 to run it.


A Web Service is created and consumed in a WPF window application. The DataContext property can be assigned to the type of the object returned by a Web Service method. This way, properties of any type can be bound to the appropriate WPF elements using Web Service methods.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Balamurali Balaji
Founder BB Systems CIT-GPNP
India India
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
miroslav6518-Oct-10 11:22
membermiroslav6518-Oct-10 11:22 
GeneralMy vote of 2 Pin
Vlad Bezden29-Jul-09 6:18
memberVlad Bezden29-Jul-09 6:18 
GeneralSimple Exploration Pin
Balamurali Balaji26-Jul-09 19:40
memberBalamurali Balaji26-Jul-09 19:40 
GeneralMy vote of 1 Pin
JRiggs21-Jul-09 6:10
memberJRiggs21-Jul-09 6:10 
GeneralMy vote of 1 Pin
Yuriy Yurchenko21-Jul-09 4:07
memberYuriy Yurchenko21-Jul-09 4:07 
General[My vote of 2] example Pin
Md. Marufuzzaman20-Jul-09 22:06
memberMd. Marufuzzaman20-Jul-09 22:06 
GeneralRe: [My vote of 2] example Pin
msp.netdev20-Jul-09 22:24
membermsp.netdev20-Jul-09 22:24 
GeneralRe: [My vote of 2] example Pin
Ners rule3-Aug-09 10:50
memberNers rule3-Aug-09 10:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171207.1 | Last Updated 21 Jul 2009
Article Copyright 2009 by Balamurali Balaji
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid