Pular para o conteúdo
Docs Try Aspire
Docs Try

Connect to Meilisearch

Este conteúdo não está disponível em sua língua ainda.

⭐ Community Toolkit Meilisearch logo

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

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

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

The Meilisearch resource exposes the following connection properties:

Property NameDescription
HostThe hostname or IP address of the Meilisearch server
PortThe port number the Meilisearch server is listening on (default: 7700)
UrlThe full base URL of the Meilisearch server, with the format http://{Host}:{Port}
MasterKeyThe master key used to authenticate with the Meilisearch server

Example connection values:

Url: http://localhost:7700
MasterKey: p%40ssw0rd1

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

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

Install the 📦 CommunityToolkit.Aspire.Meilisearch NuGet package in the client-consuming project:

.NET CLI — Add CommunityToolkit.Aspire.Meilisearch package
dotnet add package CommunityToolkit.Aspire.Meilisearch

In Program.cs, call AddMeilisearchClient on your IHostApplicationBuilder to register a MeilisearchClient:

C# — Program.cs
builder.AddMeilisearchClient(connectionName: "meilisearch");

Resolve the client through dependency injection:

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

To register multiple MeilisearchClient instances with different connection names, use AddKeyedMeilisearchClient:

C# — Program.cs
builder.AddKeyedMeilisearchClient(name: "products");
builder.AddKeyedMeilisearchClient(name: "orders");

Then resolve each instance by key:

C# — ExampleService.cs
public class ExampleService(
[FromKeyedServices("products")] MeilisearchClient productsClient,
[FromKeyedServices("orders")] MeilisearchClient ordersClient)
{
// Use clients...
}

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

The Aspire Meilisearch 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 AddMeilisearchClient:

C# — Program.cs
builder.AddMeilisearchClient("meilisearch");

The connection string is resolved from the ConnectionStrings section:

JSON — appsettings.json
{
"ConnectionStrings": {
"meilisearch": "Endpoint=http://localhost:7700/;MasterKey=your-master-key"
}
}

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

JSON — appsettings.json
{
"Aspire": {
"Meilisearch": {
"Client": {
"Endpoint": "http://localhost:7700/",
"MasterKey": "your-master-key"
}
}
}
}

Aspire client integrations enable health checks by default. The Meilisearch client integration adds a health check that verifies the Meilisearch instance is reachable and can respond to requests. 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.

If you prefer not to use the Aspire client integration, you can read the Aspire-injected connection properties from the environment and construct a MeilisearchClient directly using the 📦 Meilisearch.NET NuGet package:

C# — Program.cs
using Meilisearch;
var url = Environment.GetEnvironmentVariable("MEILISEARCH_URL");
var masterKey = Environment.GetEnvironmentVariable("MEILISEARCH_MASTERKEY");
var client = new MeilisearchClient(url!, masterKey);
// Use client to interact with Meilisearch...