Connect to Qdrant
Este conteúdo não está disponível em sua língua ainda.
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.
Connection properties
Section titled “Connection properties”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 Name | Description |
|---|---|
GrpcHost | The hostname of the Qdrant gRPC endpoint |
GrpcPort | The port number the Qdrant gRPC endpoint is listening on (default: 6334) |
HttpHost | The hostname of the Qdrant REST endpoint |
HttpPort | The port number the Qdrant REST endpoint is listening on (default: 6333) |
ApiKey | The API key for authentication |
Uri | The gRPC connection URI, with the format http://{GrpcHost}:{GrpcPort} |
HttpUri | The REST connection URI, with the format http://{HttpHost}:{HttpPort} |
Example connection strings:
Uri: http://localhost:6334HttpUri: http://localhost:6333Connect from your app
Section titled “Connect from your app”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 client integration
Section titled “Install the client integration”Install the 📦 Aspire.Qdrant.Client NuGet package in the client-consuming project:
dotnet add package Aspire.Qdrant.Client#:package Aspire.Qdrant.Client@*<PackageReference Include="Aspire.Qdrant.Client" Version="*" />Add the Qdrant client
Section titled “Add the Qdrant client”In Program.cs, call AddQdrantClient on your IHostApplicationBuilder to register a QdrantClient:
builder.AddQdrantClient(connectionName: "qdrant");Resolve the client through dependency injection:
public class ExampleService(QdrantClient client){ // Use client...}Add keyed Qdrant clients
Section titled “Add keyed Qdrant clients”To register multiple QdrantClient instances with different connection names, use AddKeyedQdrantClient:
builder.AddKeyedQdrantClient(name: "mainQdrant");builder.AddKeyedQdrantClient(name: "loggingQdrant");Then resolve each instance by key:
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.
Configuration
Section titled “Configuration”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:
builder.AddQdrantClient("qdrant");The connection string is resolved from the ConnectionStrings section. By default, the QdrantClient uses the gRPC endpoint:
{ "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:
{ "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:
builder.AddQdrantClient( "qdrant", settings => settings.Key = "your-api-key");Client integration health checks
Section titled “Client integration health checks”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.
Observability and telemetry
Section titled “Observability and telemetry”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.
Read environment variables in C#
Section titled “Read environment variables in C#”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:
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...Install the official Go client for Qdrant:
go get github.com/qdrant/go-clientRead the injected environment variables and connect using gRPC:
package main
import ( "context" "os" "strconv"
"github.com/qdrant/go-client/qdrant")
func main() { // Read Aspire-injected connection properties grpcHost := os.Getenv("QDRANT_GRPCHOST") grpcPort, _ := strconv.Atoi(os.Getenv("QDRANT_GRPCPORT")) apiKey := os.Getenv("QDRANT_APIKEY")
client, err := qdrant.NewClient(&qdrant.Config{ Host: grpcHost, Port: grpcPort, APIKey: apiKey, }) if err != nil { panic(err) } defer client.Close()
// Use client to interact with Qdrant... _ = context.Background()}Install the official Python client for Qdrant:
pip install qdrant-clientRead the injected environment variables and connect using gRPC (recommended for performance):
import osfrom qdrant_client import QdrantClient
# Read Aspire-injected connection propertiesgrpc_host = os.getenv("QDRANT_GRPCHOST")grpc_port = int(os.getenv("QDRANT_GRPCPORT"))api_key = os.getenv("QDRANT_APIKEY")
client = QdrantClient( host=grpc_host, port=grpc_port, api_key=api_key, prefer_grpc=True,)
# Use client to interact with Qdrant...Or connect using the REST endpoint:
import osfrom qdrant_client import QdrantClient
http_uri = os.getenv("QDRANT_HTTPURI")api_key = os.getenv("QDRANT_APIKEY")
client = QdrantClient(url=http_uri, api_key=api_key)Install the official TypeScript/JavaScript REST client for Qdrant:
npm install @qdrant/js-client-restRead the injected environment variables and connect:
import { QdrantClient } from '@qdrant/js-client-rest';
// Read Aspire-injected connection propertiesconst client = new QdrantClient({ url: process.env.QDRANT_HTTPURI, apiKey: process.env.QDRANT_APIKEY,});
// Use client to interact with Qdrant...Or connect using individual host and port properties:
import { QdrantClient } from '@qdrant/js-client-rest';
const client = new QdrantClient({ host: process.env.QDRANT_HTTPHOST, port: Number(process.env.QDRANT_HTTPPORT), apiKey: process.env.QDRANT_APIKEY,});