Skip to content

Events with Cosmos DB Example

Demonstrates event-driven architecture patterns using Azure Functions, Service Bus, and Cosmos DB with change feed processing.

📋 Overview

This example shows how to:

  • Build event-driven systems with Azure Service Bus
  • Store and process events in Cosmos DB
  • Use Azure Functions for event subscription handling
  • Leverage Cosmos DB change feed for event propagation

🏗️ Architecture

flowchart TB
    subgraph AppHost["Aspire AppHost"]
        API["Sample.Api<br/>(Events)"] --> ServiceBus["Service Bus<br/>(Topics/Queues)"]
        ServiceBus --> Subs["Sample.Subscriptions"]

        API --> Cosmos
        Subs --> Cosmos

        subgraph Cosmos["Cosmos DB"]
            NewUser["NewUserEvents"]
            Policy["PolicyEvents"]
        end
    end

🧩 Components

Project Description
Sample API for creating and publishing events
Sample.Subscriptions Azure Functions processing Service Bus messages
Sample.Data Cosmos DB models and Entity Framework context
Sample.AppHost Aspire orchestration with resource wiring
Sample.Data.Seed Database seeding utility
Sample.ServiceBus.Seed Service Bus message seeding utility

🔑 Key Features

  • Event Publishing: API endpoints for publishing domain events
  • Service Bus Integration: Topics and subscriptions for event distribution
  • Cosmos DB Storage: Event storage with partitioning by event type
  • Azure Functions Triggers: Service Bus triggers for event processing
  • Change Feed: Cosmos DB change feed for event propagation

📊 Event Types

Event Container Description
NewUserEvent NewUserEvents User registration and profile events
PolicyEvent PolicyEvents Insurance policy lifecycle events

📂 Project Structure

foundry/dotnet/events-with-cosmos/
├── Sample.sln
├── Sample.AppHost/
│   ├── AppHost.cs                    # Aspire orchestration
│   └── ResourceBuilders/             # Generated resource builders
├── Sample/
│   └── Endpoints/
│       └── EventEndpoints.cs         # Event publishing API
├── Sample.Subscriptions/
│   ├── PolicyEventSubTrigger.cs      # Policy event handler
│   └── NewUserSubTrigger.cs          # User event handler
├── Sample.Data/
│   ├── Models.cs                     # Event models
│   └── SampleContext.cs              # EF Core context
├── Sample.Data.Seed/                 # DB seeding
└── Sample.ServiceBus.Seed/           # Message seeding

🚀 Getting Started

Prerequisites

  • .NET 10.0 SDK
  • Docker Desktop (for Cosmos DB emulator)
  • Azure Service Bus namespace (or emulator)

Running the Example

cd foundry/dotnet/events-with-cosmos
dotnet run --project Sample.AppHost

📡 API Endpoints

Endpoint Method Description
/events/user POST Publish a new user event
/events/policy POST Publish a policy event
/events GET List recent events

💡 Use Cases

  • Audit Logging: Immutable event log for compliance
  • Event Sourcing: Rebuild state from event history
  • CQRS: Separate read/write models with events
  • Integration Events: Cross-service communication

📍 Source Code

Location: foundry/dotnet/events-with-cosmos/