Connect to Meilisearch
Цей контент ще не доступний вашою мовою.
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.
Connection properties
Section titled “Connection properties”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 Name | Description |
|---|---|
Host | The hostname or IP address of the Meilisearch server |
Port | The port number the Meilisearch server is listening on (default: 7700) |
Url | The full base URL of the Meilisearch server, with the format http://{Host}:{Port} |
MasterKey | The master key used to authenticate with the Meilisearch server |
Example connection values:
Url: http://localhost:7700MasterKey: p%40ssw0rd1Connect 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 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 client integration
Section titled “Install the client integration”Install the 📦 CommunityToolkit.Aspire.Meilisearch NuGet package in the client-consuming project:
dotnet add package CommunityToolkit.Aspire.Meilisearch#:package CommunityToolkit.Aspire.Meilisearch@*<PackageReference Include="CommunityToolkit.Aspire.Meilisearch" Version="*" />Add the Meilisearch client
Section titled “Add the Meilisearch client”In Program.cs, call AddMeilisearchClient on your IHostApplicationBuilder to register a MeilisearchClient:
builder.AddMeilisearchClient(connectionName: "meilisearch");Resolve the client through dependency injection:
public class ExampleService(MeilisearchClient client){ // Use client...}Add keyed Meilisearch clients
Section titled “Add keyed Meilisearch clients”To register multiple MeilisearchClient instances with different connection names, use AddKeyedMeilisearchClient:
builder.AddKeyedMeilisearchClient(name: "products");builder.AddKeyedMeilisearchClient(name: "orders");Then resolve each instance by key:
public class ExampleService( [FromKeyedServices("products")] MeilisearchClient productsClient, [FromKeyedServices("orders")] MeilisearchClient ordersClient){ // Use clients...}For more information, see .NET dependency injection: Keyed services.
Configuration
Section titled “Configuration”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:
builder.AddMeilisearchClient("meilisearch");The connection string is resolved from the ConnectionStrings section:
{ "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:
{ "Aspire": { "Meilisearch": { "Client": { "Endpoint": "http://localhost:7700/", "MasterKey": "your-master-key" } } }}Client integration health checks
Section titled “Client integration health checks”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.
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 properties from the environment and construct a MeilisearchClient directly using the 📦 Meilisearch.NET NuGet package:
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...Install the official Meilisearch Go client:
go get github.com/meilisearch/meilisearch-goRead the injected environment variables and connect:
package main
import ( "os" meilisearch "github.com/meilisearch/meilisearch-go")
func main() { // Read the Aspire-injected connection properties client := meilisearch.New( os.Getenv("MEILISEARCH_URL"), meilisearch.WithAPIKey(os.Getenv("MEILISEARCH_MASTERKEY")), )
// Use client to interact with Meilisearch...}Install the official Meilisearch Python SDK:
pip install meilisearchRead the injected environment variables and connect:
import osimport meilisearch
# Read the Aspire-injected connection propertiesclient = meilisearch.Client( os.getenv("MEILISEARCH_URL"), os.getenv("MEILISEARCH_MASTERKEY"),)
# Use client to interact with Meilisearch...Install the official Meilisearch JavaScript/TypeScript client:
npm install meilisearchRead the injected environment variables and connect:
import { MeiliSearch } from 'meilisearch';
// Read Aspire-injected connection propertiesconst client = new MeiliSearch({ host: process.env.MEILISEARCH_URL!, apiKey: process.env.MEILISEARCH_MASTERKEY,});
// Use client to interact with Meilisearch...