In WPF, you bind to objects returned by web service method calls the same way you bind to any other objects. To demonstrate, we’ll walk through a simple application that consumes the MSDN/TechNet Publishing System (MTPS) Content Service, discussed here. Our application implements a very simple scenario that retrieves the list of languages supported by a given document.

 

Create a Reference to the Web Service

The first step is to create a reference to the MTPS web service. To create a web reference using Visual Studio:

 

  1. Open your project in Visual Studio.
  2. From the Project menu, click Add Web Reference.
  3. In the dialog box, set the URL to the following: http://services.msdn.microsoft.com/contentservices/contentservice.asmx?wsdl
  4. Press Go, then Add Reference, to create the web reference.

 Call the Web Service and Set the DataContext

We are now ready to call the GetContent web service method and bind to the returned object. All we need to do is call the web service and set the DataContext property of the appropriate window or control to the object returned by the web service method. In this app, we provide a specific contentIdentifier for our simple request. (If you are interested in creating other requests, check out the MTPS Web Services document.)

 

The following code shows you how to do that:

 

// 1. Include the web service namespace

using BindtoContentService.com.microsoft.msdn.services;

          . . .

      // 2. Set up the request object

      // To use the MSTP web service, we need to configure and send a request

      // In this example, we create a simple request using the ID of the XmlReader.Read method page

      getContentRequest request = new getContentRequest();

      request.contentIdentifier = "abhtw0f1";

 

      // 3. Create the proxy

      ContentService proxy = new ContentService();

 

      // 4. Call the web service method and set the DataContext of the Window

      // (GetContent returns an object of type getContentResponse)

      this.DataContext = proxy.GetContent(request);

 

Create the Bindings

Now that the DataContext has been set, we can create the bindings. In the following XAML, we bind the TextBlock to the contentGuid. We want the ItemsControl to show the list of available languages of the requested content, and so we have the ItemsControl bind to and display the locale values of availableVersionsAndLocales. Take a look at this document to see the structure of getContentResponse.

 

      <TextBlock Text="Content Guid:"/>

      <TextBlock Text="{Binding Path=contentGuid}" />

      <TextBlock Text="Available Locales:"/>

      <ItemsControl ItemsSource="{Binding Path=availableVersionsAndLocales}"

                          DisplayMemberPath="locale"/>

 

Here’s a screenshot of the app, which you can find in the attachment section of this post:

 

Enjoy!