Pular para o conteúdo
Docs Try Aspire
Docs Try

Connect to Milvus

Este conteúdo não está disponível em sua língua ainda.

Milvus logo

This page describes how consuming apps connect to a Milvus resource that’s already modeled in your AppHost. For the AppHost API surface — adding a Milvus server, databases, Attu GUI, data volumes, API key parameters, and more — see Milvus Hosting integration.

When you reference a Milvus 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 Milvus client integration for automatic dependency injection and health checks.

Aspire exposes each property as an environment variable named [RESOURCE]_[PROPERTY]. For instance, the Uri property of a resource called milvusdb becomes MILVUSDB_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 server resource and adds:

Property NameDescription
DatabaseNameThe Milvus database name

Pick the language your consuming app is written in. Each example assumes your AppHost adds a Milvus server named milvus, a database named milvusdb, and references the database from the consuming app.

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

Install the 📦 Aspire.Milvus.Client NuGet package in the client-consuming project:

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

In Program.cs, call AddMilvusClient on your IHostApplicationBuilder to register a MilvusClient:

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

Resolve the client through dependency injection:

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

To register multiple MilvusClient instances with different connection names, use AddKeyedMilvusClient:

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

Then resolve each instance by key:

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.

The Aspire Milvus 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 AddMilvusClient:

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

The connection string is resolved from the ConnectionStrings section:

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

Configuration providers. The client integration supports Microsoft.Extensions.Configuration. It loads MilvusSettings from appsettings.json (or any other configuration source) by using the Aspire:Milvus:Client key:

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

Inline delegates. Pass an Action<MilvusSettings> to configure settings inline, for example to set the API key from code:

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

The following options are available:

NameDescription
EndpointThe endpoint URI of the Milvus server to connect to
DatabaseThe name of the Milvus database
KeyThe authentication key (format: root:{password})
DisableHealthChecksWhether the health check is disabled

Aspire client integrations enable health checks by default. The Milvus client integration adds a health check that verifies the Milvus server is reachable and a connection can be established. 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 Milvus client integration configures the following logging categories:

  • Milvus.Client

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

If you prefer not to use the Aspire client integration, you can read the Aspire-injected connection properties from the environment and create a MilvusClient directly using the 📦 Milvus.Client NuGet package:

C# — Program.cs
using Milvus.Client;
var host = Environment.GetEnvironmentVariable("MILVUSDB_HOST")!;
var port = int.Parse(Environment.GetEnvironmentVariable("MILVUSDB_PORT")!);
var token = Environment.GetEnvironmentVariable("MILVUSDB_TOKEN")!;
var database = Environment.GetEnvironmentVariable("MILVUSDB_DATABASENAME")!;
var client = new MilvusClient(host, port, database: database, apiKey: token);