Szerkesztés

Megosztás a következőn keresztül:


.NET Aspire Qdrant component

In this article, you learn how to use the .NET Aspire Qdrant component. Use this component to register a QdrantClient in the DI container for connecting to a Qdrant server.

Get started

To get started with the .NET Aspire Qdrant component, install the Aspire.Qdrant.Client NuGet package.

dotnet add package Aspire.Qdrant.Client

For more information, see dotnet add package or Manage package dependencies in .NET applications.

Example usage

In the Program.cs file of your component-consuming project, call the AddQdrantClient extension method to register a QdrantClient for use via the dependency injection container. The method takes a connection name parameter.

builder.AddQdrantClient("qdrant");

To retrieve your QdrantClient object consider the following example service:

public class ExampleService(QdrantClient client)
{
    // Use client...
}

App host usage

To model the Qdrant server resource in the app host, install the Aspire.Hosting.Qdrant NuGet package.

dotnet add package Aspire.Hosting.Qdrant

In your app host project, register a Qdrant server and consume the connection using the following methods:

var builder = DistributedApplication.CreateBuilder(args);

var qdrant = builder.AddQdrant("qdrant");

var myService = builder.AddProject<Projects.MyService>()
                       .WithReference(qdrant);

When you want to explicitly provide the API key, you can provide it as a parameter. Consider the following alternative example:

var apiKey = builder.AddParameter("apikey", secret: true);

var qdrant = builder.AddQdrant("qdrant", apiKey);

var myService = builder.AddProject<Projects.MyService>()
                       .WithReference(qdrant);

For more information, see External parameters.

Configuration

The .NET Aspire Qdrant Client component provides multiple options to configure the server connection based on the requirements and conventions of your project.

Use a connection string

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

builder.AddQdrantClient("qdrant");

And then the connection string will be retrieved from the ConnectionStrings configuration section:

{
  "ConnectionStrings": {
    "qdrant": "Endpoint=http://localhost:6334;Key=123456!@#$%"
  }
}

By default the QdrantClient uses the gRPC API endpoint.

Use configuration providers

The .NET Aspire Qdrant Client component supports Microsoft.Extensions.Configuration. It loads the QdrantClientSettings from configuration by using the Aspire:Qdrant:Client key. Example appsettings.json that configures some of the options:

{
  "Aspire": {
    "Qdrant": {
      "Client": {
        "Key": "123456!@#$%"
      }
    }
  }
}

Use inline delegates

You can also pass the Action<QdrantClientSettings> configureSettings delegate to set up some or all the options inline, for example to set the API key from code:

builder.AddQdrantClient("qdrant", settings => settings.ApiKey = "12345!@#$%");

Observability and telemetry

.NET Aspire components automatically set up Logging, Tracing, and Metrics configurations, which are sometimes known as the pillars of observability. For more information about component observability and telemetry, see .NET Aspire components overview. Depending on the backing service, some components may only support some of these features. For example, some components support logging and tracing, but not metrics. Telemetry features can also be disabled using the techniques presented in the Configuration section.

Logging

The .NET Aspire Qdrant component uses the following logging categories:

  • "Qdrant.Client"

See also