Exploring the Bing API Using WCF
The TV commercials took me and many others by surprise. A stream-of-conscienceless-style ad that had our brains struggling to catch up with the next topic that the next person blurted out. Funny enough, the bouncing from topic to topic by the actors in the commercial seem to be a familiar metaphor to anyone that has traversed search results at one time or another. And at the end, a familiar logo appears: Microsoft, followed by a not-so-familiar one: Bing.
Microsoft’s new search engine, or more to their own description, decision engine, seemed to come out of nowhere. Indeed, even as a Microsoft MVP who has the luxury of knowing about new products slightly ahead of the public, I was taken by surprise (as where many of my fellow MVPs). Still, it came as a pleasant and well-received surprise because upon trying it out we discovered it works quite well. Bing is not only a good search engine, but it has a very attractive user interface while still maintaining the simplicity that we come to expect from a search engine.
Microsoft’s revamp of its Internet-Search end of the business is as developer-friendly as many of their other products
Not surprisingly, Microsoft’s revamp of its Internet-Search end of the business is as developer friendly as many of their other products. Bing exposes quite a robust API that allows developers to use all its services from their own applications, whether they are on a Web or rich-client platform. The API allows for the use of either SOAP or REST protocols, and I’ll cover both in this article for your programming pleasure. Surprisingly, I’ve yet to come across an article on the Bing API or online code samples that use WCF to access it. I think you’ll find this article refreshing in the way that it provides a very different coverage from other documentation out there. WCF is the preferred technology to use when dealing with connected systems, and Bing certainly fits into that category. It only made sense to me that the two should be able to play nice together, so in this article I emphasize using WCF.
“WCF is the preferred technology to use when dealing with connected systems and Bing certainly fits into that category.”
I’m a WCF guy so I tend to like the REST API over the SOAP one simply because it gives me the ability to name the API members to my own liking and not be forced to adhere to the exact object model conventions defined in the SOAP API; but I’ll show you how to do that later. The SOAP API, however, is definitely easier to get started with and a lot quicker. You’ll see that I’ll use WCF in both techniques to access Bing, but it will be much more evident when I get to the REST API.
What the API Provides
The Bing API exposes functionality for many areas of the Bing engine including searching for text, images, videos, news, and even a specific mobile device-oriented search. But this is just search I’m talking about. Bing itself provides functionality for managing advertising, spell checking, location services, and translation services. It also provides an instant-answer search that uses the Encarta online encyclopedia. Each of these types of functionality is known as a Source Type and will be continuously discussed throughout the rest of this article.
The more common use for the Bing search engine is for standard Web queries. The API lets you perform Web queries that return site links and content, images, videos, or new sources.
This kind of search is directly equivalent to typing in search text on the Bing home page (or its images or videos section) and pressing the search button. The Bing site also has a News section which lets you type in some text and get back hits on various news sources relevant to your search text. The Source Types that corresponds to these types of searches are Web, Image, Video, and News.
On the Bing Web site, you can ask Bing questions that start with phrases like “what is” or “what does … mean”. In these cases, Bing will return hits using the Microsoft Encarta encyclopedia. When using the Bing API, this becomes a separate Source Type called InstantAnswer. When using the API in this manner, you can send questions like “How many ounces in a pound” or “What is 78f in Celsius.” You can also ask for word definitions like “What does bloviate mean?” This section of the API also lets you access flight information by sending in a carrier code and flight number, like “CO1579.”
Bing also exposes the ads that come up when you execute a search as a specific set of search results that you can access with the API. For whatever reason, you may want to show ads from Bing in your application or your site, based on the content currently showing on the page or the content of a particular Windows form you’re on. I know, I know; why would you want this feature in your rich client apps? I have no clue. It’s there so you can use it if you’d like. The Ads section of the API is a little more involved and requires membership information beyond the scope of this article.
Bing’s spell-check capability lets you send in a word or even a full sentence and returns the corrected text, if any of the words are recognized as being misspelled. For connected applications, this can add some tremendous capabilities for your users, especially if they spel like me.
Bing’s location services let you send in either a name or a place of business, as well as intelligent phrases like “hotels near Seattle” or “Italian restaurants near 07035.” The engine will return result data responding to your specific request which not only includes name and information about each item but also a map link that uses Bing Maps to map out each resulting location.
Last, but not least, we have Bing’s translation services. This part of the engine lets you send in a word, phrase, or sentence along with both a source and target language (using standard language codes). The results are simple: a direct translation of your query from the source language into the target language. Of course, it’s entirely your responsibility that the query text you send in is in the language you designate as the source language.
So now that you know what the Bing API has to offer, let’s see how to actually employ these services using the easy-to-use SOAP API.
By: Miguel Castro
Miguel is an architect with IDesign who specializes in architecture consulting and building .NET solutions. He is a Microsoft MVP and INETA speaker and has been a software developer for over 22 years. With a Microsoft background that goes all the way back to VB 1.0 (and QuickBasic in fact), Miguel jumped on .NET as soon as the first public Beta was released and has provided .NET solutions for clients around the country in a variety of industries. He considers himself to be a .NET Developer and Architect and has equal love for both VB and C#, and no tolerance for language bigotry. He’s spoken at numerous user groups around the country as well as developer conferences.
He’s the author of the CodeBreeze code-generator, which among things can be found on his Web site:
Miguel currently lives in Lincoln Park, NJ with his wife Elena and his daughter Victoria.