Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Prepare applications for Azure deployment by generating infrastructure code, Dockerfiles, and config files.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/services/app-service/templates/recipes/cosmos/source/dotnet.md
1# Cosmos DB Recipe — C# (.NET) — REFERENCE ONLY23## Cosmos DB SDK Setup45### NuGet Packages67```xml8<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.*" />9<PackageReference Include="Azure.Identity" Version="1.13.*" />10```1112### Program.cs (additions)1314Add these lines — do NOT replace the existing file:1516```csharp17using Azure.Identity;18using Microsoft.Azure.Cosmos;1920builder.Services.AddSingleton(_ =>21{22var endpoint = builder.Configuration["COSMOS_ENDPOINT"];23return new CosmosClient(endpoint, new DefaultAzureCredential());24});25```2627### CRUD Endpoints2829Add to `Program.cs` after `app` is built:3031```csharp32app.MapGet("/api/items", async (CosmosClient cosmos) =>33{34var container = cosmos35.GetDatabase(Environment.GetEnvironmentVariable("COSMOS_DATABASE_NAME"))36.GetContainer(Environment.GetEnvironmentVariable("COSMOS_CONTAINER_NAME"));37var query = container.GetItemQueryIterator<dynamic>("SELECT * FROM c");38var results = new List<dynamic>();39while (query.HasMoreResults)40results.AddRange(await query.ReadNextAsync());41return Results.Ok(results);42});43```4445## Files to Modify4647| File | Action |48|------|--------|49| `Program.cs` | Add CosmosClient registration + endpoints |50| `*.csproj` | Add Microsoft.Azure.Cosmos NuGet package |51