Salta ai contenuti
Docs Try Aspire
Docs Try

Connect to Qdrant

Questi contenuti non sono ancora disponibili nella tua lingua.

Qdrant logo

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

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

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

Qdrant exposes both a REST API and a gRPC API. The Qdrant resource exposes the following connection properties:

Property NameDescription
GrpcHostThe hostname of the Qdrant gRPC endpoint
GrpcPortThe port number the Qdrant gRPC endpoint is listening on (default: 6334)
HttpHostThe hostname of the Qdrant REST endpoint
HttpPortThe port number the Qdrant REST endpoint is listening on (default: 6333)
ApiKeyThe API key for authentication
UriThe gRPC connection URI, with the format http://{GrpcHost}:{GrpcPort}
HttpUriThe REST connection URI, with the format http://{HttpHost}:{HttpPort}

Example connection strings:

Uri: http://localhost:6334
HttpUri: http://localhost:6333

Pick the language your consuming app is written in. Each example assumes your AppHost adds a Qdrant resource named qdrant and references it from the consuming app.

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

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

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

In Program.cs, call AddQdrantClient on your IHostApplicationBuilder to register a QdrantClient:

C# — Program.cs
builder.AddQdrantClient(connectionName: "qdrant");

Resolve the client through dependency injection:

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

To register multiple QdrantClient instances with different connection names, use AddKeyedQdrantClient:

C# — Program.cs
builder.AddKeyedQdrantClient(name: "mainQdrant");
builder.AddKeyedQdrantClient(name: "loggingQdrant");

Then resolve each instance by key:

C# — ExampleService.cs
public class ExampleService(
[FromKeyedServices("mainQdrant")] QdrantClient mainClient,
[FromKeyedServices("loggingQdrant")] QdrantClient loggingClient)
{
// Use clients...
}

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

The Aspire Qdrant 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 AddQdrantClient:

C# — Program.cs
builder.AddQdrantClient("qdrant");

The connection string is resolved from the ConnectionStrings section. By default, the QdrantClient uses the gRPC endpoint:

JSON — appsettings.json
{
"ConnectionStrings": {
"qdrant": "Endpoint=http://localhost:6334;Key=your-api-key"
}
}

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

JSON — appsettings.json
{
"Aspire": {
"Qdrant": {
"Client": {
"Endpoint": "http://localhost:6334/",
"Key": "your-api-key",
"DisableHealthChecks": false,
"DisableTracing": false
}
}
}
}

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

C# — Program.cs
builder.AddQdrantClient(
"qdrant",
settings => settings.Key = "your-api-key");

Aspire client integrations enable health checks by default. The Qdrant client integration adds a health check that verifies the Qdrant server is reachable. 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 Qdrant client integration automatically configures logging through OpenTelemetry.

Logging categories:

  • Qdrant.Client

The Qdrant integration doesn’t currently emit tracing activities or metrics because they aren’t supported by the Qdrant.Client library.

If you prefer not to use the Aspire client integration, you can read the Aspire-injected connection URI and API key from the environment and create a QdrantClient directly using the 📦 Qdrant.Client NuGet package:

C# — Program.cs
using Qdrant.Client;
var endpoint = Environment.GetEnvironmentVariable("QDRANT_URI");
var apiKey = Environment.GetEnvironmentVariable("QDRANT_APIKEY");
var client = new QdrantClient(new Uri(endpoint!), apiKey: apiKey);
// Use client to interact with Qdrant...