How to: Bind to a Web Service
This example shows how to bind to objects returned by Web service method calls.
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:
Open your project in Visual Studio.
From the Project menu, click Add Web Reference.
In the dialog box, set the URL to http://services.msdn.microsoft.com/contentservices/contentservice.asmx?wsdl.
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 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.
For information about the structure of getContentResponse, see Content Service documentation.