The .NET Aspire Azure Cosmos DB hosting integration provides extension methods for adding Azure Cosmos DB resources to your distributed application.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/dotnet/aspire/llms.txt
Use this file to discover all available pages before exploring further.
Installation
Install the Azure Cosmos DB hosting integration package in your AppHost project:Prerequisites
- Azure subscription - create one for free
- Azure CLI installed and authenticated
- Owner access to the target subscription for role assignments
Configuration
Configure Azure provisioning settings in user secrets:Usage
Add Cosmos DB Account and Database
In your AppHost project (AppHost.cs):
Consume in Service
In your service project:Program.cs:
API Reference
AddAzureCosmosDB
Adds an Azure Cosmos DB account resource to the application model.Parameters
name- The name of the resource. This name will be used as the connection string name when referenced in a dependency.
Returns
A reference to theIResourceBuilder<AzureCosmosDBResource>.
AddDatabase
Adds a Cosmos DB database to the Cosmos DB account.Parameters
name- The name of the resource. This name will be used as the connection string name when referenced in a dependency.databaseName- The name of the database. If not provided, defaults to the same value asname.
Configuration Examples
Using the Emulator
For local development, use the Azure Cosmos DB emulator:Multiple Databases
Adding Containers
Add specific containers to your database:Access Key Authentication
By default, Azure Cosmos DB uses managed identity. To use access keys:Connection Properties
When you reference Azure Cosmos DB resources usingWithReference, the following connection properties are made available to the consuming project.
Cosmos DB Account
| Property Name | Description |
|---|---|
Uri | The account endpoint URI for the Cosmos DB account, with the format https://mycosmosaccount.documents.azure.com:443/ |
AccountKey | The account key for the Cosmos DB account (only available for emulator and access key authentication) |
ConnectionString | Emulator or access key authentication only. A full connection string (includes account key for emulator; access key secret when access key auth is enabled). |
Cosmos DB Database
The Cosmos DB database resource inherits all properties from its parent Cosmos DB account and adds:| Property Name | Description |
|---|---|
DatabaseName | The name of the database |
Cosmos DB Container
The Cosmos DB container resource inherits all properties from its parent Cosmos DB database and adds:| Property Name | Description |
|---|---|
ContainerName | The name of the container |
Environment Variables
Aspire exposes each property as an environment variable named[RESOURCE]_[PROPERTY]. For instance:
- The
Uriproperty of a resource calledcosmosbecomesCOSMOS_URI - The
DatabaseNameproperty becomesCOSMOS_DATABASENAME
Deployment
Local Development with Emulator
Development with Azure Resources
Production Deployment
Use Azure Developer CLI to deploy:Common Patterns
Environment-Specific Configuration
Multi-Container Database
Emulator Requirements
The Azure Cosmos DB emulator requires:- Docker Desktop (on Windows, Linux, or macOS)
- At least 2 GB of memory allocated to Docker
- Sufficient disk space for the emulator image
On macOS and Linux, the emulator runs in a container. On Windows, you can use either the container or the native Windows emulator.