Skip to main content
net8.0

A client for Azure Search that integrates with Aspire, including logging and telemetry.

Published
Mar 12, 2024
2y ago
Total Downloads
475.4K
475,444

Deprecated Package Version

Reason: Other, Legacy

This version is out of support and is no longer maintained. Please upgrade to the latest version. See our support policy for details: https://aka.ms/dotent/aspire/support

Install

dotnet add package Aspire.Azure.Search.Documents --version 8.0.0-preview.4.24156.9
ProductVersions
.NET
net8.0

README

Aspire.Azure.Search.Documents library

Registers SearchIndexClient as a singleton in the DI container for connecting to Azure Search. Enables corresponding logging and telemetry.

Getting started

Prerequisites

Install the package

Install the .NET Aspire Azure Search library with NuGet:

dotnet add package Aspire.Azure.Search.Documents

Usage example

In the Program.cs file of your project, call the AddAzureSearch extension method to register an SearchIndexClient for use via the dependency injection container. The method takes a connection name parameter.

builder.AddAzureSearch("searchConnectionName");

You can then retrieve the SearchIndexClient instance using dependency injection. For example, to retrieve the client from a Web API controller:

private readonly SearchIndexClient _indexClient;

public SearchController(SearchIndexClient indexClient)
{
    _indexClient = indexClient;
}

You can also retrieve a SearchClient which can be used for querying, by calling GetSearchClient(string indexName) method on the SearchIndexClient instance as follows:

private readonly SearchIndexClient _indexClient;

public SearchController(SearchIndexClient indexClient)
{
    _indexClient = indexClient;
}

public async Task<long> GetDocumentCountAsync(string indexName, CancellationToken cancellationToken)
{
    var searchClient = _indexClient.GetSearchClient(indexName);
    var documentCountResponse = await searchClient.GetDocumentCountAsync(cancellationToken);
    return documentCountResponse.Value;
}

See the Azure Search Service Getting Started for examples on using the SearchIndexClient.

Configuration

The .NET Aspire Azure Azure Search library provides multiple options to configure the Azure Search Service based on the requirements and conventions of your project. Note that either an Endpoint or a ConnectionString is required to be supplied.

Use a connection string

A connection can be constructed from the Keys and Endpoint tab with the format Endpoint={endpoint};Key={key};. You can provide the name of the connection string when calling builder.AddAzureSearch():

builder.AddAzureSearch("searchConnectionName");

And then the connection string will be retrieved from the ConnectionStrings configuration section. Two connection formats are supported:

Account Endpoint

The recommended approach is to use an Endpoint, which works with the AzureSearchSettings.Credential property to establish a connection. If no credential is configured, the DefaultAzureCredential is used.

{
  "ConnectionStrings": {
    "searchConnectionName": "https://{search_service}.search.windows.net/"
  }
}
Connection string

Alternatively, a custom connection string can be used.

{
  "ConnectionStrings": {
    "searchConnectionName": "Endpoint=https://{search_service}.search.windows.net/;Key={account_key};"
  }
}

Use configuration providers

The .NET Aspire Azure Search library supports Microsoft.Extensions.Configuration. It loads the AzureSearchSettings and SearchClientOptions from configuration by using the Aspire:Azure:Search:Documents key. Example appsettings.json that configures some of the options:

{
  "Aspire": {
    "Azure": {
      "Search": {
        "Documents": {
          "Tracing": true,
        }
      }
    }
  }
}

Use inline delegates

You can also pass the Action<AzureSearchSettings> configureSettings delegate to set up some or all the options inline, for example to disable tracing from code:

    builder.AddAzureSearch("searchConnectionName", settings => settings.Tracing = false);

You can also setup the SearchClientOptions using the optional Action<IAzureClientBuilder<SearchIndexClient, SearchClientOptions>> configureClientBuilder parameter of the AddAzureSearch method. For example, to set the client ID for this client:

    builder.AddAzureSearch("searchConnectionName", configureClientBuilder: builder => builder.ConfigureOptions(options => options.Diagnostics.ApplicationId = "CLIENT_ID"));

AppHost extensions

In your AppHost project, install the Aspire Azure Hosting library with NuGet:

dotnet add package Aspire.Hosting.Azure

Then, in the Program.cs file of AppHost, add an Azure Search service and consume the connection using the following methods:

var search = builder.AddAzureSearch("search");

var myService = builder.AddProject<Projects.MyService>()
                       .WithReference(search);

The AddAzureSearch method will read connection information from the AppHost's configuration (for example, from "user secrets") under the ConnectionStrings:search config key. The WithReference method passes that connection information into a connection string named search in the MyService project. In the Program.cs file of MyService, the connection can be consumed using:

builder.AddAzureSearch("search");

Additional documentation

Feedback & contributing

https://github.com/dotnet/aspire

Adoption Assessment

A verified publisher signal is available. Assess fit from the package's supported frameworks, license and dependency graph rather than popularity alone.

This version is marked prerelease. Validate its API and upgrade path before production use.NuGet marks this package as deprecated. Review the publisher's migration guidance before adopting it.Compatibility metadata lists 1 target framework; confirm the exact target used by your application.

Related Guides & Workflows

Learn how to evaluate, integrate, or audit this package in your .NET project.