How to call the new Azure Marketplace Bing Search API using C#

It took me more than 2 minutes (too long!) to figure out how to call the new Bing search API. The Bing API to Azure Marketplace migration doc is a great resource for learning how to call the new Azure Marketplace.

Here’s a quick code-post to get you going.

1. Get a Bing API Azure Marketplace Key

a) Go to Bing API Azure Marketplace page
b) Sign up for a plan. There is a free option if you have less than 5,000 transactions a month

c) Go back to the Bing API Azure Marketplace Page and click “Explore this DataSet”.

d) Click the “Show” link next to Primary Account Key in the header.

e) Copy your key for later use

2. Download the C# proxy file

It will be awesome once this is in NuGet, but for now you just have to get the proxy file manually.
a) Go back to the Bing API Azure Marketplace Page

b) Click the “.NET C# Class Library” link you’ll find right below the plan options:

c) That will prompt you to download the proxy file. Save it to a location on your computer.

3. Add the proxy file to your project

a) In Visual Studio, right click on your project and select Add –> Existing Item and add the c# file you just downloaded.

4. Reference System.Data.Services.Client dll

The proxy file uses System.Data.Services.Client, so you need to reference that.

5. Use snippets of the following code to get started and modify as needed.

Replace [your key here] with the key you got in Step 1 above

using System;
using System.Net;
using Bing;

namespace BingSearchAzureAPI
{
    class Program
    {
        static void Main(string[] args)
        {
            const string bingKey = "[your key here]";
            var bing = new BingSearchContainer(
                new Uri("https://api.datamarket.azure.com/Bing/Search/")) 
                { Credentials = new NetworkCredential(bingKey, bingKey) };

            var query = bing.Web("Jon Gallant blog", null, null, null, null, null, null, null);
            var results = query.Execute();

            foreach(var result in results)
            {
                Console.WriteLine(result.Url);
            }

            Console.ReadKey();
        }
    }
}

Hope this saves you some time and get you on the new Bing API quickly.

Jon