콘텐츠로 이동

Get started with the Azure Cosmos DB integrations

이 콘텐츠는 아직 번역되지 않았습니다.

Azure Cosmos DB logo

Azure Cosmos DB is a fully managed NoSQL database service for modern app development. The Aspire Azure Cosmos DB integration enables you to connect to existing Cosmos DB instances or create new instances with the Azure Cosmos DB emulator.

In this introduction, you’ll see how to install and use the Aspire Azure Cosmos DB integrations in a simple configuration. If you already have this knowledge, see Azure Cosmos DB Hosting integration and Azure Cosmos DB Client integration for full reference details.

To begin, install the Aspire Azure Cosmos DB Hosting integration in your Aspire AppHost project. This integration allows you to create and manage Azure Cosmos DB resources from your Aspire hosting projects:

Aspire CLI — Aspire.Hosting.Azure.CosmosDB 패키지 추가
aspire add azure-cosmosdb

Aspire CLI는 대화형입니다. 프롬프트 시 알맞은 검색 결과 선택:

Aspire CLI — 출력 예시
Select an integration to add:
> azure-cosmosdb (Aspire.Hosting.Azure.CosmosDB)
> Other results listed as selectable options...

Next, in the AppHost project, create an Azure Cosmos DB resource and pass it to the consuming client projects:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var cosmos = builder.AddAzureCosmosDB("cosmos-db");
var db = cosmos.AddCosmosDatabase("db");
builder.AddProject<Projects.ExampleProject>("api")
.WithReference(db);
// After adding all resources, run the app...
builder.Build().Run();

The preceding code adds an Azure Cosmos DB resource named cosmos-db with a database named db. The database is then referenced by the api project.

To get started with the Aspire Microsoft Entity Framework Core Cosmos DB client integration, install the client integration package in the client-consuming project:

.NET CLI — Add Aspire.Microsoft.EntityFrameworkCore.Cosmos package
dotnet add package Aspire.Microsoft.EntityFrameworkCore.Cosmos

In the Program.cs file of your client-consuming project, call the AddCosmosDbContext extension method to register a DbContext for use via the dependency injection container:

C# — Program.cs
builder.AddCosmosDbContext<MyDbContext>("cosmos-db");

In the AppHost, when you used the WithReference method to pass an Azure Cosmos DB resource to a consuming client project, Aspire injects several configuration properties that you can use in the consuming project.

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

Use the GetValue() method to obtain these environment variables in consuming projects:

C# — Obtain configuration properties
string cosmosUri = builder.Configuration.GetValue<string>("DB_URI");
string databaseName = builder.Configuration.GetValue<string>("DB_DATABASENAME");

Use Azure Cosmos DB resources in client code

Section titled “Use Azure Cosmos DB resources in client code”

You can then retrieve the DbContext instance using dependency injection. For example, to retrieve the client from a service:

public class ExampleService(MyDbContext context)
{
// Use context...
}

For more information on how the Azure Cosmos DB integrations work, see: