|
| 1 | +--- |
| 2 | +title: .NET Aspire Community Toolkit RavenDB integration |
| 3 | +author: shiranshalom |
| 4 | +description: Learn how to use the .NET Aspire RavenDB hosting and client integrations to run a RavenDB container and access it via the RavenDB client. |
| 5 | +ms.date: 2/10/2025 |
| 6 | +--- |
| 7 | + |
| 8 | +# .NET Aspire Community Toolkit RavenDB integration |
| 9 | + |
| 10 | +[!INCLUDE [includes-hosting-and-client](../includes/includes-hosting-and-client.md)] |
| 11 | + |
| 12 | +[!INCLUDE [banner](includes/banner.md)] |
| 13 | + |
| 14 | +[RavenDB](https://ravendb.net/) is a high-performance, open-source NoSQL database designed for fast, efficient, and scalable data storage. It supports advanced features like ACID transactions, distributed data replication, and time-series data management, making it an excellent choice for modern application development. The .NET Aspire RavenDB integration enables you to connect to existing RavenDB instances or create new instances from .NET using the [docker.io/library/ravendb container image](https://hub.docker.com/r/ravendb/ravendb). |
| 15 | + |
| 16 | +## Hosting integration |
| 17 | + |
| 18 | +The RavenDB hosting integration models the server as the `RavenDBServerResource` type and the database as the `RavenDBDatabaseResource` type. To access these types and APIs, add the [📦 CommunityToolkit.Aspire.Hosting.RavenDB](https://nuget.org/packages/CommunityToolkit.Aspire.Hosting.RavenDB) NuGet package in the [app host](xref:dotnet/aspire/app-host) project. |
| 19 | + |
| 20 | +### [.NET CLI](#tab/dotnet-cli) |
| 21 | + |
| 22 | +```dotnetcli |
| 23 | +dotnet add package CommunityToolkit.Aspire.Hosting.RavenDB |
| 24 | +``` |
| 25 | + |
| 26 | +### [PackageReference](#tab/package-reference) |
| 27 | + |
| 28 | +```xml |
| 29 | +<PackageReference Include="CommunityToolkit.Aspire.Hosting.RavenDB" |
| 30 | + Version="*" /> |
| 31 | +``` |
| 32 | + |
| 33 | +--- |
| 34 | + |
| 35 | +For more information, see [dotnet add package](/dotnet/core/tools/dotnet-add-package) or [Manage package dependencies in .NET applications](/dotnet/core/tools/dependencies). |
| 36 | + |
| 37 | +### Add RavenDB server resource and database resource |
| 38 | + |
| 39 | +To set up RavenDB in your app host project, call one of the `AddRavenDB` extension methods on the `builder` instance to add a RavenDB server resource, then call `AddDatabase` on the server resource to add a database. Here’s an example: |
| 40 | + |
| 41 | +```csharp |
| 42 | +var builder = DistributedApplication.CreateBuilder(args); |
| 43 | + |
| 44 | +var ravenServer = builder.AddRavenDB("ravenServer"); |
| 45 | +var ravendb = ravenServer.AddDatabase("ravendb"); |
| 46 | + |
| 47 | +builder.AddProject<Projects.ExampleProject>() |
| 48 | + .WithReference(ravendb) |
| 49 | + .WaitFor(ravendb); |
| 50 | + |
| 51 | +// After adding all resources, build and run the app... |
| 52 | +``` |
| 53 | +> [!IMPORTANT] |
| 54 | +> A valid RavenDB license is required. If you don’t have one yet, you can request a free Community license [here](https://ravendb.net/license/request/community). |
| 55 | +
|
| 56 | +When .NET 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. A reference to your RavenDB database resource (the `ravendb` variable) is added to the `ExampleProject`. |
| 57 | + |
| 58 | +For more information, see [Container resource lifecycle](../fundamentals/app-host-overview.md#container-resource-lifecycle). |
| 59 | + |
| 60 | +### Add RavenDB server resource with data volume |
| 61 | + |
| 62 | +To add a data volume to the RavenDB server resource, call the `Aspire.Hosting.RavenDBBuilderExtensions.WithDataVolume` method on the RavenDB server resource: |
| 63 | + |
| 64 | +```csharp |
| 65 | +var builder = DistributedApplication.CreateBuilder(args); |
| 66 | + |
| 67 | +var ravenServer = builder.AddRavenDB("ravenServer") |
| 68 | + .WithDataVolume(); |
| 69 | + |
| 70 | +builder.AddProject<Projects.ExampleProject>() |
| 71 | + .WithReference(ravenServer) |
| 72 | + .WaitFor(ravenServer); |
| 73 | +``` |
| 74 | + |
| 75 | +The data volume remains available after the container's lifecycle ends, preserving RavenDB data. The data volume is mounted at the `/var/lib/ravendb/data` path in the RavenDB container and 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](#add-meilisearch-resource-with-data-bind-mount), see [Docker docs: Volumes](https://docs.docker.com/engine/storage/volumes). |
| 76 | + |
| 77 | +### Add RavenDB server resource with data bind mount |
| 78 | + |
| 79 | +To add a data bind mount to the RavenDB server resource, call the `Aspire.Hosting.RavenDBBuilderExtensions.WithDataBindMount` method: |
| 80 | + |
| 81 | +```csharp |
| 82 | +var builder = DistributedApplication.CreateBuilder(args); |
| 83 | + |
| 84 | +var ravenServer = builder.AddRavenDB("ravenServer") |
| 85 | + .WithDataBindMount(source: @"C:\RavenDb\Data"); |
| 86 | + |
| 87 | +builder.AddProject<Projects.ExampleProject>() |
| 88 | + .WithReference(ravenServer) |
| 89 | + .WaitFor(ravenServer); |
| 90 | +``` |
| 91 | + |
| 92 | +[!INCLUDE [data-bind-mount-vs-volumes](../includes/data-bind-mount-vs-volumes.md)] |
| 93 | + |
| 94 | +Data bind mounts rely on the host machine's filesystem to persist the RavenDB data across container restarts. The data bind mount is mounted at the `C:\RavenDb\Data` on Windows (or `/RavenDB/Data` on Unix) path on the host machine in the RavenDB container. For more information on data bind mounts, see [Docker docs: Bind mounts](https://docs.docker.com/engine/storage/bind-mounts). |
| 95 | + |
| 96 | +### Add secured RavenDB server resource |
| 97 | + |
| 98 | +To create a new secured RavenDB instance using settings from a pre-configured _settings.json_ file or a self-signed certificate, use the `RavenDBServerSettings.Secured` method or `RavenDBServerSettings.SecuredWithLetsEncrypt` for Let’s Encrypt configurations. These methods allow you to specify the domain URL, certificate details, and additional server settings. |
| 99 | +Here’s an example of how to add a secured RavenDB server resource using Let's Encrypt: |
| 100 | + |
| 101 | +```csharp |
| 102 | +var builder = DistributedApplication.CreateBuilder(args); |
| 103 | + |
| 104 | +var serverSettings = RavenDBServerSettings.SecuredWithLetsEncrypt( |
| 105 | + domainUrl: "https://mycontainer.development.run", |
| 106 | + certificatePath: "/etc/ravendb/security/cluster.server.certificate.mycontainer.pfx"); |
| 107 | + |
| 108 | +var ravendb = builder.AddRavenDB("ravenSecuredServer", serverSettings) |
| 109 | + .WithBindMount("C:/RavenDB/Server/Security", "/etc/ravendb/security", false) |
| 110 | + .AddDatabase("ravendbSecured"); |
| 111 | + |
| 112 | +builder.AddProject<Projects.ExampleProject>() |
| 113 | + .WithReference(ravendb) |
| 114 | + .WaitFor(ravendb); |
| 115 | +``` |
| 116 | +> [!IMPORTANT] |
| 117 | +> Ensure the certificate path is accessible to the container by bind-mounting it to `/etc/ravendb/security`. |
| 118 | +
|
| 119 | +### Hosting integration health checks |
| 120 | + |
| 121 | +The RavenDB hosting integration automatically adds a health check for the RavenDB server resource, verifying that the server is running and reachable. |
| 122 | + |
| 123 | +The hosting integration relies on the [📦 AspNetCore.HealthChecks.RavenDB](https://www.nuget.org/packages/AspNetCore.HealthChecks.RavenDB) NuGet package. |
| 124 | + |
| 125 | +## Client integration |
| 126 | + |
| 127 | +To get started with the .NET Aspire RavenDB client integration, install the [📦 CommunityToolkit.Aspire.RavenDB.Client](https://nuget.org/packages/CommunityToolkit.Aspire.RavenDB.Client) NuGet package in the client-consuming project, that is, the project for the application that uses the RavenDB client. |
| 128 | +The RavenDB client integration registers an [IDocumentStore](https://ravendb.net/docs/article-page/6.2/csharp/client-api/what-is-a-document-store) instance, which serves as the entry point for interacting with the RavenDB server resource or an existing RavenDB instance. If your app host includes RavenDB database resources, the associated [IDocumentSession](https://ravendb.net/docs/article-page/6.2/csharp/client-api/session/what-is-a-session-and-how-does-it-work) and [IAsyncDocumentSession](https://ravendb.net/docs/article-page/6.2/csharp/client-api/session/what-is-a-session-and-how-does-it-work) instances are also registered for dependency injection. |
| 129 | + |
| 130 | +### [.NET CLI](#tab/dotnet-cli) |
| 131 | + |
| 132 | +```dotnetcli |
| 133 | +dotnet add package CommunityToolkit.Aspire.RavenDB.Client |
| 134 | +``` |
| 135 | + |
| 136 | +### [PackageReference](#tab/package-reference) |
| 137 | + |
| 138 | +```xml |
| 139 | +<PackageReference Include="CommunityToolkit.Aspire.RavenDB.Client" |
| 140 | + Version="*" /> |
| 141 | +``` |
| 142 | + |
| 143 | +--- |
| 144 | + |
| 145 | +### Add RavenDB client |
| 146 | + |
| 147 | +In the _:::no-loc text="Program.cs":::_ file of your client-consuming project, call the `Microsoft.Extensions.Hosting.RavenDBClientExtension.AddRavenDBClient` extension method on any <xref:Microsoft.Extensions.Hosting.IHostApplicationBuilder> to register a `IDocumentStore` for use via the dependency injection container. The method takes a connection name parameter. |
| 148 | + |
| 149 | +```csharp |
| 150 | +builder.AddRavenDBClient(connectionName: "ravendb"); |
| 151 | +``` |
| 152 | + |
| 153 | +> [!TIP] |
| 154 | +> The `connectionName` parameter must match the name used when adding the RavenDB server resource (or the database resource, if provided) in the app host project. In other words, when you call `AddDatabase` and provide a name of `ravendb`, that same name should be used when calling `AddRavenDBClient`. For more information, see [Add RavenDB server resource and database resource](#add-ravendb-server-resource-and-database-resource). |
| 155 | +
|
| 156 | +You can then retrieve the `IDocumentStore` instance using dependency injection. For example, to retrieve the client from an example service: |
| 157 | + |
| 158 | +```csharp |
| 159 | +public class ExampleService(IDocumentStore client) |
| 160 | +{ |
| 161 | + // Use client... |
| 162 | +} |
| 163 | +``` |
| 164 | + |
| 165 | +#### Add RavenDB client using `RavenDBClientSettings` |
| 166 | + |
| 167 | +The `AddRavenDBClient` method provides overloads that accept a `RavenDBClientSettings` object. This allows you to use the client integration independently of the hosting integration. |
| 168 | +The `RavenDBClientSettings` class contains the parameters needed to establish a connection. More details about the available configuration options can be found in the [Configuration options](#configuration-options) section below. |
| 169 | + |
| 170 | +Here’s an example: |
| 171 | + |
| 172 | +```csharp |
| 173 | +var settings = new RavenDBClientSettings |
| 174 | +{ |
| 175 | + Urls = new[] { serverUrl }, |
| 176 | + DatabaseName = myDatabaseName, |
| 177 | + Certificate = myCertificate |
| 178 | +}; |
| 179 | + |
| 180 | +builder.AddRavenDBClient(settings: settings); |
| 181 | +``` |
| 182 | + |
| 183 | +> [!Note] |
| 184 | +> These methods are ideal for connecting to an existing RavenDB instance without relying on the hosting integration. This is particularly useful if you already have a standalone instance running (e.g., in the cloud) and want to connect to it using its specific details. |
| 185 | +
|
| 186 | +After registration, you can retrieve the `IDocumentStore` instance and its associated `IDocumentSession` and `IAsyncDocumentSession` instances as follows: |
| 187 | + |
| 188 | +```csharp |
| 189 | +var documentStore = host.Services.GetRequiredService<IDocumentStore>(); |
| 190 | +var session = host.Services.GetRequiredService<IDocumentSession>(); |
| 191 | +var asyncSession = host.Services.GetRequiredService<IAsyncDocumentSession>(); |
| 192 | +``` |
| 193 | + |
| 194 | +### Add keyed RavenDB client |
| 195 | + |
| 196 | +If your application requires multiple `IDocumentStore` instances with different connection configurations, you can register keyed RavenDB clients using the `Microsoft.Extensions.Hosting.RavenDBClientExtension.AddKeyedRavenDBClient` extension method: |
| 197 | + |
| 198 | +```csharp |
| 199 | +builder.AddKeyedRavenDBClient(serviceKey: "production", connectionName: "production"); |
| 200 | +builder.AddKeyedRavenDBClient(serviceKey: "testing", connectionName: "testing"); |
| 201 | +``` |
| 202 | + |
| 203 | +Then you can retrieve the `IDocumentStore` instances using dependency injection. For example, to retrieve a connection from an example service: |
| 204 | + |
| 205 | +```csharp |
| 206 | +public class ExampleService( |
| 207 | + [FromKeyedServices("production")] IDocumentStore production, |
| 208 | + [FromKeyedServices("testing")] IDocumentStore testing) |
| 209 | +{ |
| 210 | + // Use databases... |
| 211 | +} |
| 212 | +``` |
| 213 | + |
| 214 | +For more information on keyed services, see [.NET dependency injection: Keyed services](/dotnet/core/extensions/dependency-injection#keyed-services). |
| 215 | + |
| 216 | +### Configuration |
| 217 | + |
| 218 | +The .NET Aspire RavenDB Client integration provides multiple configuration approaches and options to meet the requirements and conventions of your project. |
| 219 | + |
| 220 | +#### Use a connection string |
| 221 | + |
| 222 | +When using a connection string from the `ConnectionStrings` configuration section, provide the name of the connection string when calling `builder.AddRavenDBClient`: |
| 223 | + |
| 224 | +```csharp |
| 225 | +builder.AddRavenDBClient("ravendb"); |
| 226 | +``` |
| 227 | + |
| 228 | +The connection string will be retrieved from the `ConnectionStrings` configuration section: |
| 229 | + |
| 230 | +```json |
| 231 | +{ |
| 232 | + "ConnectionStrings": { |
| 233 | + "ravendb": "Url=http://localhost:8080/;Database=ravendb" |
| 234 | + } |
| 235 | +} |
| 236 | +``` |
| 237 | + |
| 238 | +#### Use configuration providers |
| 239 | + |
| 240 | +The .NET Aspire RavenDB integration supports <xref:Microsoft.Extensions.Configuration>. It loads the `RavenDBClientSettings` from the configuration using the `Aspire:RavenDB:Client` key. Consider the following example _appsettings.json_ that configures some of the options: |
| 241 | + |
| 242 | +```json |
| 243 | +{ |
| 244 | + "Aspire": { |
| 245 | + "RavenDB": { |
| 246 | + "Client": { |
| 247 | + "ConnectionString": "URL=http://localhost:8080;Database=ravendb", |
| 248 | + "DisableHealthChecks": false, |
| 249 | + "HealthCheckTimeout": 10000, |
| 250 | + "DisableTracing": false |
| 251 | + } |
| 252 | + } |
| 253 | + } |
| 254 | +} |
| 255 | +``` |
| 256 | + |
| 257 | +#### Use inline configurations |
| 258 | + |
| 259 | +You can also pass the `Action<RavenDBClientSettings>` delegate to set up some or all the options inline: |
| 260 | + |
| 261 | +```csharp |
| 262 | +builder.AddRavenDBClient(connectionName: "ravendb", configureSettings: |
| 263 | + settings => |
| 264 | + { |
| 265 | + settings.CreateDatabase = true; |
| 266 | + settings.Certificate = myCertificate; |
| 267 | + settings.DisableTracing = true; |
| 268 | + } |
| 269 | +``` |
| 270 | + |
| 271 | +### Configuration options |
| 272 | + |
| 273 | +The .NET Aspire RavenDB client integration provides flexible configuration options through the `RavenDBClientSettings` class, enabling you to tailor the connection to your project's requirements. Here are the key properties: |
| 274 | + |
| 275 | +| Name | Description | |
| 276 | +|-----------------------|---------------------------------------------------------------------------------------| |
| 277 | +| `Urls` | The connection URLs (`string[]`) of the RavenDB cluster. | |
| 278 | +| `DatabaseName` | Optional. The name of the RavenDB database to create or connect to. | |
| 279 | +| `CertificatePath` | Optional. The path to the certificate for secured RavenDB instances. | |
| 280 | +| `CertificatePassword` | Optional. The password for the certificate, if required. | |
| 281 | +| `Certificate` | Optional. An `X509Certificate2` instance for secured RavenDB instances. | |
| 282 | +| `CreateDatabase` | A boolean value that indicates whether a new database should be created if it does not already exist. | |
| 283 | +| `ModifyDocumentStore` | Optional. An `Action` to modify the `IDocumentStore` instance. | |
| 284 | +| `DisableHealthChecks` | A boolean value that indicates whether the database health check is disabled or not. | |
| 285 | +| `HealthCheckTimeout` | An `int?` value that indicates the RavenDB health check timeout in milliseconds. | |
| 286 | +| `DisableTracing` | A boolean value that indicates whether the OpenTelemetry tracing is disabled or not. | |
| 287 | + |
| 288 | + |
| 289 | +#### Client integration health checks |
| 290 | + |
| 291 | +The .NET Aspire RavenDB client integration uses the configured client to perform a `IsHealthyAsync`. If the result is `true`, the health check is considered healthy, otherwise it's unhealthy. Likewise, if there's an exception, the health check is considered unhealthy with the error propagating through the health check failure. |
| 292 | + |
| 293 | +## See also |
| 294 | + |
| 295 | +- [RavenDB](https://ravendb.net/) |
| 296 | +- [ Running in a Docker Container](https://ravendb.net/docs/article-page/6.2/csharp/start/installation/running-in-docker-container) |
| 297 | +- [RavenDB Code](https://github.com/ravendb/ravendb) |
| 298 | +- [.NET Aspire Community Toolkit GitHub repo](https://github.com/CommunityToolkit/Aspire) |
| 299 | +
|
0 commit comments