Ir al contenido

Milvus Client integration reference

Esta página aún no está disponible en tu idioma.

Milvus logo

To get started with the Aspire Milvus integrations, follow the Get started with Milvus integrations guide.

This article includes full details about the Aspire Milvus Client integration, which allows you to connect to and interact with Milvus vector databases from your Aspire consuming projects.

You need a Milvus server and connection information for accessing the server. To get started with the Aspire Milvus client integration, install the 📦 Aspire.Milvus.Client NuGet package in the client-consuming project, that is, the project for the application that uses the Milvus client. The Milvus client integration registers a MilvusClient instance that you can use to interact with Milvus.

.NET CLI — Add Aspire.Milvus.Client package
dotnet add package Aspire.Milvus.Client

In the Program.cs file of your client-consuming project, call the AddMilvusClient extension method on any IHostApplicationBuilder to register a MilvusClient for use via the dependency injection container. The method takes a connection name parameter.

C# — Program.cs
builder.AddMilvusClient(connectionName: "milvusdb");

You can then retrieve the MilvusClient instance using dependency injection. For example, to retrieve the connection from an example service:

C# — ExampleService.cs
public class ExampleService(MilvusClient client)
{
// Use the Milvus Client...
}

For more information on dependency injection, see .NET dependency injection.

There might be situations where you want to register multiple MilvusClient instances with different connection strings. To register keyed Milvus clients, call the AddKeyedMilvusClient method:

C# — Program.cs
builder.AddKeyedMilvusClient(name: "mainDb");
builder.AddKeyedMilvusClient(name: "loggingDb");

Then retrieve the instances using dependency injection:

C# — ExampleService.cs
public class ExampleService(
[FromKeyedServices("mainDb")] MilvusClient mainDbClient,
[FromKeyedServices("loggingDb")] MilvusClient loggingDbClient)
{
// Use clients...
}

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

When you use the WithReference method to pass a Milvus server or database resource from the AppHost project to a consuming client project, several properties are available to use in the consuming project.

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

The Milvus server resource exposes the following connection properties:

Property NameDescription
HostThe hostname or IP address of the Milvus server
PortThe gRPC port exposed by the Milvus server
TokenThe authentication token, with the format root:{ApiKey}
UriThe gRPC endpoint URI, with the format http://{Host}:{Port}

Example connection strings:

Uri: http://localhost:19530
Token: root:Milvus

The Milvus database resource inherits all properties from its parent MilvusServerResource and adds:

Property NameDescription
DatabaseNameThe Milvus database name

The Aspire Milvus client integration provides multiple configuration approaches and options to meet the requirements and conventions of your project.

When using a connection string from the ConnectionStrings configuration section, you provide the name of the connection string when calling builder.AddMilvusClient():

C# — Program.cs
builder.AddMilvusClient("milvus");

The connection string is retrieved from the ConnectionStrings configuration section:

JSON — appsettings.json
{
"ConnectionStrings": {
"milvus": "Endpoint=http://localhost:19530/;Key=root:Non-default-P@ssw0rd"
}
}

For more information about the Milvus connection string format, see the Milvus documentation.

The Aspire Milvus client integration supports Microsoft.Extensions.Configuration from configuration files such as appsettings.json by using the Aspire:Milvus:Client key. If you have set up your configurations in the Aspire:Milvus:Client section you can just call the method without passing any parameter.

The following is an example of an appsettings.json that configures some of the available options:

JSON — appsettings.json
{
"Aspire": {
"Milvus": {
"Client": {
"Endpoint": "http://localhost:19530/",
"Database": "milvusdb",
"Key": "root:Non-default-P@ssw0rd",
"DisableHealthChecks": false
}
}
}
}

You can also pass the Action<MilvusSettings> delegate to set up some or all the options inline, for example to set the API key from code:

C# — Program.cs
builder.AddMilvusClient(
"milvus",
static settings => settings.Key = "root:Non-default-P@ssw0rd");

Here are the available configurable options:

NameDescription
EndpointThe endpoint URI of the Milvus server to connect to.
DatabaseThe name of the Milvus database.
KeyThe authentication key used for authentication (format: root:{password}).
DisableHealthChecksA boolean value that indicates whether the health check is disabled or not.

By default, Aspire integrations enable health checks for all services. For more information, see Aspire integrations overview.

By default, the Aspire Milvus client integration handles the following:

  • Checks if the MilvusSettings.DisableHealthChecks is true.
  • If not disabled, adds a health check that verifies the Milvus server is reachable and a connection can be established.

Aspire integrations automatically set up Logging, Tracing, and Metrics configurations, which are sometimes known as the pillars of observability. Depending on the backing service, some integrations may only support some of these features. For example, some integrations support logging and tracing, but not metrics. Telemetry features can also be disabled using the techniques presented in the Configuration section.

The Aspire Milvus client integration uses the following log categories:

  • Milvus.Client

The Milvus integration doesn’t currently emit tracing activities or metrics because they are not supported by the Milvus.Client library.