Перейти к содержимому
ДокументацияПопробовать Aspire
ДокументацияПопробовать

Set up RavenDB in the AppHost

Это содержимое пока не доступно на вашем языке.

⭐ Community Toolkit RavenDB logo

This article is the reference for the Aspire RavenDB Hosting integration. It enumerates the AppHost APIs that you use to model RavenDB server and database resources in your AppHost project.

If you’re new to the RavenDB integration, start with the Get started with RavenDB integrations guide. For how consuming apps read the connection information this page exposes, see Connect to RavenDB.

The Aspire RavenDB hosting integration models the RavenDB server as the RavenDBServerResource type and the database as the RavenDBDatabaseResource type. To start building an Aspire app that uses RavenDB, install the 📦 CommunityToolkit.Aspire.Hosting.RavenDB NuGet package:

Terminal
aspire add CommunityToolkit.Aspire.Hosting.RavenDB

Learn more about aspire add in the command reference.

This updates your aspire.config.json with the RavenDB hosting integration package:

aspire.config.json
{
"packages": {
"CommunityToolkit.Aspire.Hosting.RavenDB": "13.4.0"
}
}

Add RavenDB server resource and database resource

Section titled “Add RavenDB server resource and database resource”

Once you’ve installed the hosting integration in your AppHost project, you can add a RavenDB server resource and then add a database resource:

apphost.mts
import {
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
} from './.aspire/modules/aspire.mjs';
const
const builder: IDistributedApplicationBuilder
builder
= await
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
();
const
const ravenServer: RavenDBServerResource
ravenServer
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addRavenDB(name: string): RavenDBServerResource

Adds a RavenDB server resource to the application model. A container is used for local development. This overload simplifies the configuration by creating an unsecured RavenDB server resource with default settings.

addRavenDB
("ravenServer");
const
const ravendb: RavenDBDatabaseResource
ravendb
= await
const ravenServer: RavenDBServerResource
ravenServer
.
RavenDBServerResource.addDatabase(name: string, options?: {
databaseName?: string;
ensureCreated?: boolean;
}): RavenDBDatabaseResource (+1 overload)

Adds a database resource to an existing RavenDB server resource.

addDatabase
("ravendb");
const
const apiService: ProjectResource
apiService
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addProject(name: string, projectPath: string, options?: {
launchProfileOrOptions?: ProjectResourceOptions;
}): ProjectResource (+1 overload)

Adds a .NET project resource

addProject
("apiservice", "../ExampleProject/ExampleProject.csproj");
await
const apiService: ProjectResource
apiService
.
ProjectResource.withReference(source: EndpointReference | string | uri, options?: {
connectionName?: string;
optional?: boolean;
name?: string;
} | undefined): ProjectResource (+1 overload)

Adds a reference to another resource

withReference
(
const ravendb: RavenDBDatabaseResource
ravendb
);
await
const apiService: ProjectResource
apiService
.
ProjectResource.waitFor(dependency: IResource | IResourceWithConnectionString, waitBehavior?: WaitBehavior): ProjectResource

Waits for another resource to be ready

waitFor
(
const ravendb: RavenDBDatabaseResource
ravendb
);
await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.build(): DistributedApplication

Builds the distributed application

build
().
DistributedApplication.run(cancellationToken?: cancellationToken): void

Runs the distributed application

run
();
  1. When Aspire adds a container image to the app host, as shown in the preceding example with the docker.io/ravendb/ravendb image, it creates a new RavenDB instance on your local machine.

  2. The AddDatabase call registers a database sub-resource named ravendb on the server. By default, the database name matches the resource name. You can pass a different database name as the second argument: ravenServer.AddDatabase("ravendb", databaseName: "mydb").

  3. The AppHost reference call configures a connection in the consuming project named after the referenced database resource, such as ravendb in the preceding example.

Add RavenDB database resource with auto-create

Section titled “Add RavenDB database resource with auto-create”

By default, AddDatabase registers the database in the Aspire model but does not create it in RavenDB automatically. Pass ensureCreated: true to have Aspire create the database on startup if it does not already exist:

apphost.mts
import {
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
} from './.aspire/modules/aspire.mjs';
const
const builder: IDistributedApplicationBuilder
builder
= await
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
();
const
const ravenServer: RavenDBServerResource
ravenServer
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addRavenDB(name: string): RavenDBServerResource

Adds a RavenDB server resource to the application model. A container is used for local development. This overload simplifies the configuration by creating an unsecured RavenDB server resource with default settings.

addRavenDB
("ravenServer");
const
const ravendb: RavenDBDatabaseResource
ravendb
= await
const ravenServer: RavenDBServerResource
ravenServer
.
RavenDBServerResource.addDatabase(name: string, options?: {
databaseName?: string;
ensureCreated?: boolean;
}): RavenDBDatabaseResource (+1 overload)

Adds a database resource to an existing RavenDB server resource.

addDatabase
("ravendb", {
ensureCreated?: boolean | undefined
ensureCreated
: true });
const
const apiService: ProjectResource
apiService
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addProject(name: string, projectPath: string, options?: {
launchProfileOrOptions?: ProjectResourceOptions;
}): ProjectResource (+1 overload)

Adds a .NET project resource

addProject
("apiservice", "../ExampleProject/ExampleProject.csproj");
await
const apiService: ProjectResource
apiService
.
ProjectResource.withReference(source: EndpointReference | string | uri, options?: {
connectionName?: string;
optional?: boolean;
name?: string;
} | undefined): ProjectResource (+1 overload)

Adds a reference to another resource

withReference
(
const ravendb: RavenDBDatabaseResource
ravendb
);
await
const apiService: ProjectResource
apiService
.
ProjectResource.waitFor(dependency: IResource | IResourceWithConnectionString, waitBehavior?: WaitBehavior): ProjectResource

Waits for another resource to be ready

waitFor
(
const ravendb: RavenDBDatabaseResource
ravendb
);
await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.build(): DistributedApplication

Builds the distributed application

build
().
DistributedApplication.run(cancellationToken?: cancellationToken): void

Runs the distributed application

run
();

Add RavenDB server resource with data volume

Section titled “Add RavenDB server resource with data volume”

Add a data volume to the RavenDB server resource to persist data outside the lifecycle of its container:

apphost.mts
import {
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
} from './.aspire/modules/aspire.mjs';
const
const builder: IDistributedApplicationBuilder
builder
= await
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
();
const
const ravenServer: RavenDBServerResource
ravenServer
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addRavenDB(name: string): RavenDBServerResource

Adds a RavenDB server resource to the application model. A container is used for local development. This overload simplifies the configuration by creating an unsecured RavenDB server resource with default settings.

addRavenDB
("ravenServer");
await
const ravenServer: RavenDBServerResource
ravenServer
.
RavenDBServerResource.withDataVolume(options?: {
name?: string;
isReadOnly?: boolean;
}): RavenDBServerResource (+1 overload)

Adds a named volume for the data folder to a RavenDB container resource.

withDataVolume
();
const
const ravendb: RavenDBDatabaseResource
ravendb
= await
const ravenServer: RavenDBServerResource
ravenServer
.
RavenDBServerResource.addDatabase(name: string, options?: {
databaseName?: string;
ensureCreated?: boolean;
}): RavenDBDatabaseResource (+1 overload)

Adds a database resource to an existing RavenDB server resource.

addDatabase
("ravendb");
const
const apiService: ProjectResource
apiService
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addProject(name: string, projectPath: string, options?: {
launchProfileOrOptions?: ProjectResourceOptions;
}): ProjectResource (+1 overload)

Adds a .NET project resource

addProject
("apiservice", "../ExampleProject/ExampleProject.csproj");
await
const apiService: ProjectResource
apiService
.
ProjectResource.withReference(source: EndpointReference | string | uri, options?: {
connectionName?: string;
optional?: boolean;
name?: string;
} | undefined): ProjectResource (+1 overload)

Adds a reference to another resource

withReference
(
const ravendb: RavenDBDatabaseResource
ravendb
);
await
const apiService: ProjectResource
apiService
.
ProjectResource.waitFor(dependency: IResource | IResourceWithConnectionString, waitBehavior?: WaitBehavior): ProjectResource

Waits for another resource to be ready

waitFor
(
const ravendb: RavenDBDatabaseResource
ravendb
);
await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.build(): DistributedApplication

Builds the distributed application

build
().
DistributedApplication.run(cancellationToken?: cancellationToken): void

Runs the distributed application

run
();

The data volume is mounted at the /var/lib/ravendb/data path in the RavenDB container. When a name parameter isn’t provided, the name is generated at random. For more information on data volumes and details on why they’re preferred over bind mounts, see Docker docs: Volumes.

Add RavenDB server resource with data bind mount

Section titled “Add RavenDB server resource with data bind mount”

Add a data bind mount to the RavenDB server resource as an alternative to a named volume:

apphost.mts
import {
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
} from './.aspire/modules/aspire.mjs';
const
const builder: IDistributedApplicationBuilder
builder
= await
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
();
const
const ravenServer: RavenDBServerResource
ravenServer
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addRavenDB(name: string): RavenDBServerResource

Adds a RavenDB server resource to the application model. A container is used for local development. This overload simplifies the configuration by creating an unsecured RavenDB server resource with default settings.

addRavenDB
("ravenServer");
await
const ravenServer: RavenDBServerResource
ravenServer
.
RavenDBServerResource.withDataBindMount(source: string, options?: {
isReadOnly?: boolean;
}): RavenDBServerResource (+1 overload)

Adds a bind mount for the data folder to a RavenDB container resource.

withDataBindMount
("C:\\RavenDb\\Data");
const
const ravendb: RavenDBDatabaseResource
ravendb
= await
const ravenServer: RavenDBServerResource
ravenServer
.
RavenDBServerResource.addDatabase(name: string, options?: {
databaseName?: string;
ensureCreated?: boolean;
}): RavenDBDatabaseResource (+1 overload)

Adds a database resource to an existing RavenDB server resource.

addDatabase
("ravendb");
const
const apiService: ProjectResource
apiService
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addProject(name: string, projectPath: string, options?: {
launchProfileOrOptions?: ProjectResourceOptions;
}): ProjectResource (+1 overload)

Adds a .NET project resource

addProject
("apiservice", "../ExampleProject/ExampleProject.csproj");
await
const apiService: ProjectResource
apiService
.
ProjectResource.withReference(source: EndpointReference | string | uri, options?: {
connectionName?: string;
optional?: boolean;
name?: string;
} | undefined): ProjectResource (+1 overload)

Adds a reference to another resource

withReference
(
const ravendb: RavenDBDatabaseResource
ravendb
);
await
const apiService: ProjectResource
apiService
.
ProjectResource.waitFor(dependency: IResource | IResourceWithConnectionString, waitBehavior?: WaitBehavior): ProjectResource

Waits for another resource to be ready

waitFor
(
const ravendb: RavenDBDatabaseResource
ravendb
);
await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.build(): DistributedApplication

Builds the distributed application

build
().
DistributedApplication.run(cancellationToken?: cancellationToken): void

Runs the distributed application

run
();

Data bind mounts rely on the host machine’s filesystem to persist the RavenDB data across container restarts. The bind mount maps the host path C:\RavenDb\Data (on Windows, or the equivalent Unix path) to the /var/lib/ravendb/data directory inside the RavenDB container. For more information on data bind mounts, see Docker docs: Bind mounts.

Add RavenDB server resource with log volume

Section titled “Add RavenDB server resource with log volume”

Add a named volume for the RavenDB logs directory:

apphost.mts
import {
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
} from './.aspire/modules/aspire.mjs';
const
const builder: IDistributedApplicationBuilder
builder
= await
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
();
const
const ravenServer: RavenDBServerResource
ravenServer
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addRavenDB(name: string): RavenDBServerResource

Adds a RavenDB server resource to the application model. A container is used for local development. This overload simplifies the configuration by creating an unsecured RavenDB server resource with default settings.

addRavenDB
("ravenServer");
await
const ravenServer: RavenDBServerResource
ravenServer
.
RavenDBServerResource.withDataVolume(options?: {
name?: string;
isReadOnly?: boolean;
}): RavenDBServerResource (+1 overload)

Adds a named volume for the data folder to a RavenDB container resource.

withDataVolume
();
await
const ravenServer: RavenDBServerResource
ravenServer
.
RavenDBServerResource.withLogVolume(options?: {
name?: string;
isReadOnly?: boolean;
}): RavenDBServerResource (+1 overload)

Adds a named volume for the logs folder to a RavenDB container resource.

withLogVolume
();
const
const ravendb: RavenDBDatabaseResource
ravendb
= await
const ravenServer: RavenDBServerResource
ravenServer
.
RavenDBServerResource.addDatabase(name: string, options?: {
databaseName?: string;
ensureCreated?: boolean;
}): RavenDBDatabaseResource (+1 overload)

Adds a database resource to an existing RavenDB server resource.

addDatabase
("ravendb");
const
const apiService: ProjectResource
apiService
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addProject(name: string, projectPath: string, options?: {
launchProfileOrOptions?: ProjectResourceOptions;
}): ProjectResource (+1 overload)

Adds a .NET project resource

addProject
("apiservice", "../ExampleProject/ExampleProject.csproj");
await
const apiService: ProjectResource
apiService
.
ProjectResource.withReference(source: EndpointReference | string | uri, options?: {
connectionName?: string;
optional?: boolean;
name?: string;
} | undefined): ProjectResource (+1 overload)

Adds a reference to another resource

withReference
(
const ravendb: RavenDBDatabaseResource
ravendb
);
await
const apiService: ProjectResource
apiService
.
ProjectResource.waitFor(dependency: IResource | IResourceWithConnectionString, waitBehavior?: WaitBehavior): ProjectResource

Waits for another resource to be ready

waitFor
(
const ravendb: RavenDBDatabaseResource
ravendb
);
await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.build(): DistributedApplication

Builds the distributed application

build
().
DistributedApplication.run(cancellationToken?: cancellationToken): void

Runs the distributed application

run
();

The log volume is mounted at the /var/log/ravendb/logs path in the RavenDB container. You can also use WithLogBindMount(source: @"C:\RavenDb\Logs") to bind-mount a host directory for logs.

To create a secured RavenDB instance using a pre-configured settings.json file or a self-signed certificate, use RavenDBServerSettings.Secured or RavenDBServerSettings.SecuredWithLetsEncrypt:

AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var serverSettings = RavenDBServerSettings.SecuredWithLetsEncrypt(
domainUrl: "https://mycontainer.development.run",
certificatePath: "/etc/ravendb/security/cluster.server.certificate.mycontainer.pfx");
var ravenServer = builder.AddRavenDB("ravenSecuredServer", serverSettings)
.WithBindMount("C:/RavenDB/Server/Security", "/etc/ravendb/security", isReadOnly: false);
var ravendb = ravenServer.AddDatabase("ravendbSecured");
builder.AddProject<Projects.ExampleProject>("apiservice")
.WithReference(ravendb)
.WaitFor(ravendb);
builder.Build().Run();

For the full reference of RavenDB connection properties — and how consuming apps in C#, TypeScript, Python, and Go read them — see Connect to RavenDB.

The RavenDB hosting integration automatically adds a health check for both the server resource and each database resource, verifying that the instance is running and reachable. The hosting integration relies on the 📦 AspNetCore.HealthChecks.RavenDB NuGet package.