# Milvus Client integration reference

<Image
  src={milvusIcon}
  alt="Milvus logo"
  width={100}
  height={100}
  class:list={'float-inline-left icon'}
  data-zoom-off
/>

To get started with the Aspire Milvus integrations, follow the [Get started with Milvus integrations](../milvus-get-started/) 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.

## Installation

<Aside type="caution">The client integration is currently in preview.</Aside>

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](https://www.nuget.org/packages/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.

<InstallDotNetPackage packageName="Aspire.Milvus.Client" />

## Add 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.

```csharp title="C# — Program.cs"
builder.AddMilvusClient(connectionName: "milvusdb");
```

<Aside type="tip">
The `connectionName` parameter must match the name used when adding the Milvus database resource in the AppHost project. For more information, see [Add Milvus server and database resources](../milvus-host/#add-milvus-server-and-database-resources).
</Aside>

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

```csharp title="C# — ExampleService.cs"
public class ExampleService(MilvusClient client)
{
    // Use the Milvus Client...
}
```

For more information on dependency injection, see [.NET dependency injection](https://learn.microsoft.com/dotnet/core/extensions/dependency-injection).

## Add keyed Milvus client

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:

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

Then retrieve the instances using dependency injection:

```csharp title="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](https://learn.microsoft.com/dotnet/core/extensions/dependency-injection#keyed-services).

## Properties of the Milvus resources

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`.

### Milvus server

The Milvus server resource exposes the following connection properties:

| Property Name | Description                                                   |
| ------------- | ------------------------------------------------------------- |
| `Host`        | The hostname or IP address of the Milvus server               |
| `Port`        | The gRPC port exposed by the Milvus server                    |
| `Token`       | The authentication token, with the format `root:{ApiKey}`     |
| `Uri`         | The gRPC endpoint URI, with the format `http://{Host}:{Port}` |

**Example connection strings:**

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

### Milvus database

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

| Property Name  | Description              |
| -------------- | ------------------------ |
| `DatabaseName` | The Milvus database name |

## Configuration

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

### Use a connection string

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

```csharp title="C# — Program.cs"
builder.AddMilvusClient("milvus");
```

The connection string is retrieved from the `ConnectionStrings` configuration section:

```json title="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](https://milvus.io/).

### Use configuration providers

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 title="JSON — appsettings.json"
{
  "Aspire": {
    "Milvus": {
      "Client": {
        "Endpoint": "http://localhost:19530/",
        "Database": "milvusdb",
        "Key": "root:Non-default-P@ssw0rd",
        "DisableHealthChecks": false
      }
    }
  }
}
```

### Use inline delegates

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:

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

### Configuration options

Here are the available configurable options:

| Name | Description |
|--|--|
| `Endpoint` | The endpoint URI of the Milvus server to connect to. |
| `Database` | The name of the Milvus database. |
| `Key` | The authentication key used for authentication (format: `root:{password}`). |
| `DisableHealthChecks` | A boolean value that indicates whether the health check is disabled or not. |

## Client integration health checks

By default, Aspire integrations enable [health checks](/fundamentals/health-checks/) for all services. For more information, see [Aspire integrations overview](/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.

## Observability and telemetry

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](#configuration) section.

### Logging

The Aspire Milvus client integration uses the following log categories:

- `Milvus.Client`

### Tracing and Metrics

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

## See also

- [Milvus](https://milvus.io/)
- [Milvus Documentation](https://milvus.io/docs)
- [Aspire integrations](/integrations/overview/)
- [Aspire GitHub repo](https://github.com/dotnet/aspire)