Export (0) Print
Expand All

Using the API with Microsoft .Net Framework using SOAP

Bing

This version of the API offers enhanced support for SOAP through stronger typing. The previous version had a single SearchRequest object that included all request parameters and a single SearchResponse object that included all response fields. The fields in the response were populated (or not) based on the SourceTypes in the request. In this version, every SourceType has its own request and response objects.

noteNote
The changes that created stronger typing in SOAP were made in the underlying object model. Hence, the benefits of strong typing are present in all interfaces, not just SOAP.

The advantage of using the SOAP interface over the XML interface from statically-typed languages like C# and VB.NET is that there is less XML processing to do, since the web services infrastructure in .NET handles deserializing the SOAP XML responses from the SOAP interface into strongly-typed .NET collections.

To use the SOAP interface from a .NET project, you can use the Add Web Reference functionality in Visual Studio to consume the Bing WSDL found at http://api.search.live.net/search.wsdl?AppID=<YourAppIDHere>. For more information, see Using SOAP.

Example 8 shows typical .NET code using the SOAP API (after the web service reference has been added).

Example 8: Using the Bing SOAP API

LiveSearchService soapClient = new LiveSearchService();
SearchRequest request = new SearchRequest();
request.AppId = AppId;
request.Sources = new SourceType[] { SourceType.Image };
request.Query = query;
SearchResponse response = soapClient.Search(request);
if (response.Image != null && response.Image.Results.Count() > 0)
{
    var results = from uris in response.Image.Results
                  select new LiveSearchResultImage
                  {
                      URI = uris.Url,
                      Title = uris.Title,
                      ThumbnailURI = uris.Thumbnail.Url,

                  };
    return results;
}

In Example 8, we continue to use the LiveSearchResultImage to simplify our data binding code, but notice that we can use LINQ to Objects instead of XLINQ (which saves us the burden of the XML processing).

Instead of passing the sources as a query string parameter, there is a strongly-typed enumeration, SourceType, which is passed as an array on the SearchRequest object. SearchRequest represents the parameters that, in Getting Started: Using the Bing API from a Browser, we passed as query string parameters to the HTTP endpoint. SearchRequest.Query represents the query we’d like to run. If you want to set parameters like Offset or Count, there are strongly-typed properties on SearchRequest for doing this for different SourceTypes. For example, to change the Offset for this search (using the image SourceType), we’d create an instance of ImageRequest, set its Offset property to the appropriate value, and set the SearchRequest.ImageRequest on our SearchRequest instance to the new instance of ImageRequest.

Show:
© 2015 Microsoft