Pular para o conteúdo
Docs Try Aspire
Docs Try

Connect to SurrealDB

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

⭐ Community Toolkit SurrealDB logo

This page describes how consuming apps connect to a SurrealDB resource that’s already modeled in your AppHost. For the AppHost API surface — adding a SurrealDB server, namespace, database, Surrealist, data bind mounts, and more — see SurrealDB Hosting integration.

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

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

The SurrealDB database resource exposes the following connection properties:

Property NameDescription
HostThe hostname or IP address of the SurrealDB server
PortThe HTTP/WebSocket port the SurrealDB server is listening on (default: 8000)
UsernameThe username for authentication
PasswordThe password for authentication
NamespaceThe SurrealDB namespace name
DatabaseThe database name
EndpointThe full WebSocket endpoint URL, with the format ws://{Host}:{Port}/
ConnectionStringThe full connection string, with the format Endpoint=ws://{Host}:{Port}/;Namespace={Namespace};Database={Database};Username={Username};Password={Password}

Example connection string:

ConnectionString: Endpoint=ws://localhost:8000/;Namespace=ns;Database=db;Username=root;Password=p%40ssw0rd1

Pick the language your consuming app is written in. Each example assumes your AppHost adds a SurrealDB server, namespace, and database resource with the database named db and references it from the consuming app.

For C# apps, the recommended approach is the Aspire SurrealDB client integration. It registers a SurrealDbClient 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 📦 CommunityToolkit.Aspire.SurrealDb NuGet package in the client-consuming project:

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

In Program.cs, call AddSurrealClient on your IHostApplicationBuilder to register a SurrealDbClient:

C# — Program.cs
builder.AddSurrealClient(connectionName: "db");

Resolve the client through dependency injection:

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

To register multiple SurrealDbClient instances with different connection names, use AddKeyedSurrealClient:

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

Then resolve each instance by key:

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

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

The Aspire SurrealDB 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 AddSurrealClient:

C# — Program.cs
builder.AddSurrealClient("db");

The connection string is resolved from the ConnectionStrings section:

JSON — appsettings.json
{
"ConnectionStrings": {
"db": "Endpoint=ws://localhost:8000/;Namespace=ns;Database=db;Username=root;Password=s3cr3t"
}
}

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

JSON — appsettings.json
{
"Aspire": {
"Surreal": {
"Client": {
"Endpoint": "ws://localhost:8000/",
"Namespace": "ns",
"Database": "db",
"Username": "root",
"Password": "s3cr3t"
}
}
}
}

Inline delegates. Pass an action to configure settings inline, for example to disable health checks:

C# — Program.cs
builder.AddSurrealClient(
"db",
static settings => settings.DisableHealthChecks = true);

Aspire client integrations enable health checks by default. The SurrealDB client integration adds a health check that verifies the SurrealDB instance is reachable and can execute queries. 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 string from the environment and use the 📦 SurrealDB.Net NuGet package to construct a client directly:

C# — Program.cs
using SurrealDb.Net;
var connectionString = Environment.GetEnvironmentVariable("DB_CONNECTIONSTRING");
var client = new SurrealDbClient(connectionString!);
await client.Connect();
// Use client to query the database...