Salta ai contenuti
Docs Try Aspire
Docs Try

Connect to RavenDB

Questi contenuti non sono ancora disponibili nella tua lingua.

⭐ Community Toolkit RavenDB logo

This page describes how consuming apps connect to a RavenDB resource that’s already modeled in your AppHost. For the AppHost API surface — adding a RavenDB server, databases, data volumes, secured instances, and more — see RavenDB Hosting integration.

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

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

The RavenDB server resource exposes the following connection properties:

Property NameDescription
HostThe hostname or IP address of the RavenDB server
PortThe port number the RavenDB server is listening on (8080 for HTTP, 443 for HTTPS)
UriThe server URI, with the format http://{Host}:{Port} or https://{Host}:{Port}

Example connection string:

Uri: http://localhost:8080

The RavenDB database resource inherits all properties from its parent server resource and adds:

Property NameDescription
DatabaseThe name of the RavenDB database

The full connection string for the database resource uses the format URL={server URI};Database={DatabaseName}:

URL=http://localhost:8080;Database=ravendb

Pick the language your consuming app is written in. Each example assumes your AppHost adds a RavenDB server resource named ravenServer and a database resource named ravendb, and references the database from the consuming app.

For C# apps, the recommended approach is the Aspire RavenDB client integration. It registers an IDocumentStore 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.RavenDB.Client NuGet package in the client-consuming project:

.NET CLI — Add CommunityToolkit.Aspire.RavenDB.Client package
dotnet add package CommunityToolkit.Aspire.RavenDB.Client

The RavenDB client integration registers an IDocumentStore instance, which serves as the entry point for interacting with the RavenDB server. If your AppHost includes RavenDB database resources, the associated IDocumentSession and IAsyncDocumentSession instances are also registered for dependency injection.

In Program.cs, call AddRavenDBClient on your IHostApplicationBuilder to register an IDocumentStore:

C# — Program.cs
builder.AddRavenDBClient(connectionName: "ravendb");

Resolve the document store through dependency injection:

C# — ExampleService.cs
public class ExampleService(IDocumentStore store)
{
// Use store...
}

The AddRavenDBClient method provides overloads that accept a RavenDBClientSettings object for connecting to an existing RavenDB instance without relying on the hosting integration:

C# — Program.cs
var settings = new RavenDBClientSettings
{
Urls = new[] { serverUrl },
DatabaseName = myDatabaseName,
Certificate = myCertificate
};
builder.AddRavenDBClient(settings: settings);

To register multiple IDocumentStore instances with different connection configurations, use AddKeyedRavenDBClient:

C# — Program.cs
builder.AddKeyedRavenDBClient(serviceKey: "production", connectionName: "production");
builder.AddKeyedRavenDBClient(serviceKey: "testing", connectionName: "testing");

Then resolve each instance by key:

C# — ExampleService.cs
public class ExampleService(
[FromKeyedServices("production")] IDocumentStore production,
[FromKeyedServices("testing")] IDocumentStore testing)
{
// Use stores...
}

The Aspire RavenDB 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 AddRavenDBClient:

C# — Program.cs
builder.AddRavenDBClient("ravendb");

The connection string is resolved from the ConnectionStrings section:

JSON — appsettings.json
{
"ConnectionStrings": {
"ravendb": "URL=http://localhost:8080;Database=ravendb"
}
}

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

JSON — appsettings.json
{
"Aspire": {
"RavenDB": {
"Client": {
"ConnectionString": "URL=http://localhost:8080;Database=ravendb",
"DisableHealthChecks": false,
"HealthCheckTimeout": 10000,
"DisableTracing": false
}
}
}
}

Inline delegates. Pass an Action<RavenDBClientSettings> to configure settings inline, for example to disable health checks:

C# — Program.cs
builder.AddRavenDBClient(
connectionName: "ravendb",
configureSettings: settings =>
{
settings.CreateDatabase = true;
settings.Certificate = myCertificate;
settings.DisableTracing = true;
});

Aspire client integrations enable health checks by default. The RavenDB client integration adds a health check that verifies the RavenDB server or database 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.

If you prefer not to use the Aspire client integration, you can read the Aspire-injected URI from the environment and connect directly using the RavenDB.Client NuGet package:

C# — Program.cs
using Raven.Client.Documents;
var uri = Environment.GetEnvironmentVariable("RAVENDB_URI");
var database = Environment.GetEnvironmentVariable("RAVENDB_DATABASE");
using var store = new DocumentStore
{
Urls = [uri!],
Database = database
}.Initialize();
// Use store to interact with RavenDB...