Skip to content

Milvus integration

Milvus logo

Milvus is an open-source vector database system that efficiently stores, indexes, and searches large-scale vector data. It’s commonly used in machine learning, artificial intelligence, and data science applications.

Vector data encodes information as mathematical vectors, which are arrays of numbers or coordinates. Machine learning and AI systems often use vectors to represent unstructured objects like images, text, audio, or video.

The Milvus database hosting integration models the server as the MilvusServerResource type and the database as the MilvusDatabaseResource type. To access these types and APIs, add the 📦 Aspire.Hosting.Milvus NuGet package in your AppHost project:

Aspire CLI — Add Aspire.Hosting.Milvus package
aspire add milvus

The Aspire CLI is interactive, be sure to select the appropriate search result when prompted:

Aspire CLI — Example output prompt
Select an integration to add:
> milvus (Aspire.Hosting.Milvus)
> Other results listed as selectable options...

In your AppHost project, call AddMilvus to add and return a Milvus resource builder. Chain a call to the returned resource builder to AddDatabase, to add a Milvus database resource:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var milvus = builder.AddMilvus("milvus")
.WithLifetime(ContainerLifetime.Persistent);
var milvusdb = milvus.AddDatabase("milvusdb");
var myService = builder.AddProject<Projects.ExampleProject>()
.WithReference(milvusdb)
.WaitFor(milvusdb);

The WithReference method configures a connection in the ExampleProject named milvusdb.

The Milvus resource includes default credentials with a username of root and the password Milvus. To change the default password in the container, pass an apiKey parameter:

var apiKey = builder.AddParameter("apiKey", secret: true);
var milvus = builder.AddMilvus("milvus", apiKey);
var myService = builder.AddProject<Projects.ExampleProject>()
.WithReference(milvus);

For more information, see External parameters.

To add a data volume to the Milvus service resource, call the WithDataVolume method:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var milvus = builder.AddMilvus("milvus")
.WithDataVolume();
var milvusdb = milvus.AddDatabase("milvusdb");
var myService = builder.AddProject<Projects.ExampleProject>()
.WithReference(milvusdb)
.WaitFor(milvusdb);

The data volume is used to persist the Milvus data outside the lifecycle of its container. The data volume is mounted at the /var/lib/milvus path.

Add a Milvus resource with a data bind mount

Section titled “Add a Milvus resource with a data bind mount”

To add a data bind mount to the Milvus resource, call the WithDataBindMount method:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var milvus = builder.AddMilvus("milvus")
.WithDataBindMount(source: @"C:\Milvus\Data");
var milvusdb = milvus.AddDatabase("milvusdb");
var myService = builder.AddProject<Projects.ExampleProject>()
.WithReference(milvusdb)
.WaitFor(milvusdb);

Attu is a graphical user interface (GUI) and management tool designed to interact with Milvus and its databases. To use Attu, call the WithAttu extension method:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var milvus = builder.AddMilvus("milvus")
.WithAttu()
.WithLifetime(ContainerLifetime.Persistent);
var milvusdb = milvus.AddDatabase("milvusdb");
var myService = builder.AddProject<Projects.ExampleProject>()
.WithReference(milvusdb)
.WaitFor(milvusdb);

When you debug the Aspire solution, you’ll see an Attu container listed in the resources. Select the resource’s endpoint to open the GUI.

When you reference a Milvus resource using WithReference, the following connection properties are made available to the consuming project:

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 combines the server properties above and adds the following connection property:

Property NameDescription
DatabaseThe Milvus database name

To get started with the Aspire Milvus client integration, 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 the Program.cs file, call the AddMilvusClient extension method to register a MilvusClient:

builder.AddMilvusClient("milvusdb");

You can then retrieve the MilvusClient instance using dependency injection:

public class ExampleService(MilvusClient client)
{
// Use the Milvus Client...
}

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

builder.AddKeyedMilvusClient(name: "mainDb");
builder.AddKeyedMilvusClient(name: "loggingDb");

Then retrieve the instances:

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

When using a connection string from the ConnectionStrings configuration section:

builder.AddMilvusClient("milvus");

Then the connection string will be retrieved:

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

The Milvus client integration supports Microsoft.Extensions.Configuration. Example appsettings.json:

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

You can pass the delegate to set up options inline:

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

By default, Aspire integrations enable health checks. The Milvus database integration adds a health check when DisableHealthChecks is false, which attempts to connect to the Milvus server.

The Milvus integration uses the Milvus.Client log category.

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

FAQCollaborateCommunityDiscussWatch