How to: Bind to a Web Service

This example shows how to bind to objects returned by Web service method calls.

Example

This example uses the MSDN/TechNet Publishing System (MTPS) Content Service to retrieve the list of languages supported by a specified document.

Before you call a Web service, you need to create a reference to it. To create a Web reference to the MTPS service using Microsoft Visual Studio, follow the following steps:

  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 http://services.msdn.microsoft.com/contentservices/contentservice.asmx?wsdl.

  4. Press Go and then Add Reference.

Next, you call the Web service method and set the DataContext of the appropriate control or window to the returned object. The GetContent method of the MTPS service takes a reference to the getContentRequest object. Therefore, the following example first sets up a request object:

' 1. Include the web service namespace
Imports 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 that has the ID of the XmlReader.Read method page
            Dim request As New getContentRequest()
            request.contentIdentifier = "abhtw0f1"

            ' 3. Create the proxy
            Dim proxy As New ContentService()

            ' 4. Call the web service method and set the DataContext of the Window
            ' (GetContent returns an object of type getContentResponse)
            Me.DataContext = proxy.GetContent(request)
// 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 that has 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);

After the DataContext has been set, you can create bindings to the properties of the object that the DataContext has been set to. In this example, the DataContext is set to the getContentResponse object returned by the GetContent method. In the following example, the ItemsControl binds to and displays the locale values of availableVersionsAndLocales of getContentResponse.

<ItemsControl Grid.Column="1" Grid.Row="2" Margin="0,3,0,0"
              ItemsSource="{Binding Path=availableVersionsAndLocales}"
              DisplayMemberPath="locale"/>

For information about the structure of getContentResponse, see Content Service documentation.

See Also

Tasks

How to: Make Data Available for Binding in XAML

Concepts

Data Binding Overview

Binding Sources Overview