Quickstart: Use the Bing Entity Search .NET client library
Use this quickstart to begin searching for entities with the Bing Entity Search client library for C#. While Bing Entity Search has a REST API compatible with most programming languages, the client library provides an easy way to integrate the service into your applications. The source code for this sample can be found on GitHub.
Prerequisites
Any edition of Visual Studio 2017 or later.
The Json.NET framework, available as a NuGet package.
If you are using Linux/MacOS, this application can be run using Mono.
The Bing News Search SDK NuGet package. Installing this package also installs the following:
- Microsoft.Rest.ClientRuntime
- Microsoft.Rest.ClientRuntime.Azure
- Newtonsoft.Json
To add the Bing Entity Search client library to your Visual Studio project, use the Manage NuGet Packages option from Solution Explorer, and add the Microsoft.Azure.CognitiveServices.Search.EntitySearch
package.
Create and initialize an application
Create a new C# console solution in Visual Studio. Then add the following into the main code file:
using System; using System.Linq; using System.Text; using Microsoft.Azure.CognitiveServices.Search.EntitySearch; using Microsoft.Azure.CognitiveServices.Search.EntitySearch.Models; using Newtonsoft.Json;
Create a client and send a search request
Create a new search client. Add your subscription key by creating a new
ApiKeyServiceClientCredentials
.var client = new EntitySearchClient(new ApiKeyServiceClientCredentials("YOUR-ACCESS-KEY"));
Use the client's
Entities.Search()
function to search for your query:var entityData = client.Entities.Search(query: "Satya Nadella");
Get and print an entity description
If the API returned search results, get the main entity from
entityData
.var mainEntity = entityData.Entities.Value.Where(thing => thing.EntityPresentationInfo.EntityScenario == EntityScenario.DominantEntity).FirstOrDefault();
Print the description of the main entity.
Console.WriteLine(mainEntity.Description);