콘텐츠로 이동
Docs Try Aspire
Docs Try

Connect to Azure AI Search

이 콘텐츠는 아직 번역되지 않았습니다.

Azure AI Search logo

This page describes how consuming apps connect to an Azure AI Search resource that’s already modeled in your AppHost. For the AppHost API surface — adding a search resource, connecting to existing services, customizing provisioning, and more — see Azure AI Search Hosting integration.

When you reference an Azure AI Search resource from your AppHost, Aspire injects the connection information into the consuming app as environment variables. Your app can either read those environment variables directly — the pattern works the same from any language — or, in C#, use the Aspire Azure AI Search client integration for automatic dependency injection, health checks, and telemetry.

Aspire exposes each property as an environment variable named [RESOURCE]_[PROPERTY]. For instance, the Endpoint property of a resource called search becomes SEARCH_ENDPOINT.

The Azure AI Search resource exposes the following connection properties:

Property NameDescription
EndpointThe HTTPS endpoint of the Azure AI Search service: https://{name}.search.windows.net
ApiKeyThe admin API key (only available when using key-based connection strings, not role-based auth)

Example connection string (role-based access, default):

Endpoint=https://mysearch.search.windows.net

Example connection string (key-based access):

Endpoint=https://mysearch.search.windows.net;Key={account_key}

Pick the language your consuming app is written in. Each example assumes your AppHost adds an Azure AI Search resource named search and references it from the consuming app.

For C# apps, the recommended approach is the Aspire Azure AI Search client integration. It registers a SearchIndexClient through dependency injection and adds health checks and telemetry automatically. If you’d rather read environment variables directly, see the Read environment variables section at the end of this tab.

Install the 📦 Aspire.Azure.Search.Documents NuGet package in the client-consuming project:

.NET CLI — Add Aspire.Azure.Search.Documents package
dotnet add package Aspire.Azure.Search.Documents

In Program.cs, call AddAzureSearchClient on your IHostApplicationBuilder to register a SearchIndexClient:

C# — Program.cs
builder.AddAzureSearchClient(connectionName: "search");

Resolve the client through dependency injection:

C# — ExampleService.cs
public class ExampleService(SearchIndexClient indexClient)
{
// Use indexClient...
}

You can also retrieve a SearchClient for querying by calling GetSearchClient on the injected SearchIndexClient:

C# — ExampleService.cs
public class ExampleService(SearchIndexClient indexClient)
{
public async Task<long> GetDocumentCountAsync(
string indexName,
CancellationToken cancellationToken)
{
var searchClient = indexClient.GetSearchClient(indexName);
var documentCountResponse = await searchClient.GetDocumentCountAsync(
cancellationToken);
return documentCountResponse.Value;
}
}

To register multiple SearchIndexClient instances with different connection names, use AddKeyedAzureSearchClient:

C# — Program.cs
builder.AddKeyedAzureSearchClient(name: "catalog");
builder.AddKeyedAzureSearchClient(name: "documents");

Then resolve each instance by key:

C# — ExampleService.cs
public class ExampleService(
[FromKeyedServices("catalog")] SearchIndexClient catalogClient,
[FromKeyedServices("documents")] SearchIndexClient documentsClient)
{
// Use clients...
}

For more information, see .NET dependency injection: Keyed services.

The Aspire Azure AI Search client integration offers multiple ways to provide configuration.

Connection strings. When using a connection string from the ConnectionStrings configuration section, pass the connection name to AddAzureSearchClient.

For role-based access (recommended), provide just the endpoint URL:

JSON — appsettings.json
{
"ConnectionStrings": {
"search": "https://mysearch.search.windows.net/"
}
}

For key-based access, provide the full connection string:

JSON — appsettings.json
{
"ConnectionStrings": {
"search": "Endpoint=https://mysearch.search.windows.net/;Key={account_key};"
}
}

Configuration providers. The client integration supports Microsoft.Extensions.Configuration. It loads AzureSearchSettings from appsettings.json (or any other configuration source) by using the Aspire:Azure:Search:Documents key:

JSON — appsettings.json
{
"Aspire": {
"Azure": {
"Search": {
"Documents": {
"DisableTracing": false
}
}
}
}
}

For the complete Azure AI Search Documents client integration JSON schema, see Aspire.Azure.Search.Documents/ConfigurationSchema.json.

Named configuration. To configure multiple instances, use the connection name as a key:

JSON — appsettings.json
{
"Aspire": {
"Azure": {
"Search": {
"Documents": {
"search1": {
"Endpoint": "https://mysearch1.search.windows.net/",
"DisableTracing": false
},
"search2": {
"Endpoint": "https://mysearch2.search.windows.net/",
"DisableTracing": true
}
}
}
}
}
}

Named configuration takes precedence over top-level configuration when both are provided.

Inline delegates. Pass an Action<AzureSearchSettings> to configure settings inline:

C# — Program.cs
builder.AddAzureSearchClient(
"search",
static settings => settings.DisableTracing = true);

You can also configure the SearchClientOptions using the configureClientBuilder parameter:

C# — Program.cs
builder.AddAzureSearchClient(
"search",
configureClientBuilder: builder => builder.ConfigureOptions(
static options => options.Diagnostics.ApplicationId = "CLIENT_ID"));

The Azure AI Search client integration adds a health check that calls GetServiceStatisticsAsync on the SearchIndexClient to verify that the service is available. The health check is wired into the /health HTTP endpoint, where all registered health checks must pass before the app is considered ready to accept traffic.

The Aspire Azure AI Search client integration automatically configures logging, tracing, and metrics through OpenTelemetry.

Logging categories:

  • Azure
  • Azure.Core
  • Azure.Identity

Tracing activities:

  • The integration emits tracing activities using OpenTelemetry when interacting with the search service.

Any of these telemetry features can be disabled through the configuration options above.

If you prefer not to use the Aspire client integration, you can read the Aspire-injected endpoint from the environment and use the 📦 Azure.Search.Documents package directly with DefaultAzureCredential:

C# — Program.cs
using Azure;
using Azure.Identity;
using Azure.Search.Documents.Indexes;
var endpoint = new Uri(Environment.GetEnvironmentVariable("SEARCH_ENDPOINT")!);
// Use DefaultAzureCredential for role-based access (recommended)
var indexClient = new SearchIndexClient(endpoint, new DefaultAzureCredential());
// Use AzureKeyCredential for key-based access
// var apiKey = Environment.GetEnvironmentVariable("SEARCH_APIKEY");
// var indexClient = new SearchIndexClient(endpoint, new AzureKeyCredential(apiKey!));