Skip to content

.NET 8 Notifications microservice with CQRS + Outbox Pattern (Email/Push), RabbitMQ integration, background workers, and SQL Server stored procedures (TVP).

Notifications You must be signed in to change notification settings

Dev-Ahmed-Ashraf/OnlineCourseSystem.Notifications

Repository files navigation

OnlineCourseSystem.Notifications

.NET C# SQL Server RabbitMQ Firebase Status

Enterprise-grade notifications microservice for the Codeway Online Courses Platform. Handles multi-channel delivery (in-app, email, push) with event-driven architecture, outbox pattern, and comprehensive user preference management.


🎯 Overview

Centralized notification hub that orchestrates multi-channel communication across the Codeway platform. Supports REST APIs and event-driven integration via RabbitMQ.

Core Capabilities:

  • Multi-channel delivery (In-app, Email, Push)
  • User preference management per notification type
  • Event-driven architecture (MassTransit + RabbitMQ)
  • Outbox pattern for reliable delivery
  • Scheduled notifications
  • Direct messaging between users
  • Device management for push notifications

πŸ—οΈ Architecture

External Services β†’ REST API / RabbitMQ Events
         ↓
Controllers β†’ CQRS (MediatR) β†’ Services β†’ Repositories β†’ SQL Server
         ↓
Background Workers (EmailOutbox, PushOutbox, Scheduled)

Data Flow:

  1. Request/Event β†’ Validation β†’ User Verification
  2. sp_CreateNotification (stored procedure) creates notification + outbox items atomically
  3. Background workers process outbox items asynchronously
  4. Delivery tracking across all channels

πŸ› οΈ Tech Stack

  • .NET 8.0 + ASP.NET Core Web API
  • EF Core 8.0 + SQL Server (stored procedures with TVPs)
  • MassTransit 8.2 + RabbitMQ
  • MediatR 14.0 (CQRS)
  • FluentValidation 12.1
  • Firebase Admin SDK (FCM push notifications)

πŸš€ Quick Start

Prerequisites

  • .NET 8.0 SDK
  • SQL Server 2022+
  • RabbitMQ (for events)
  • Firebase project (for push)

Setup

  1. Restore & Configure

    dotnet restore OnlineCourseSystem.sln

    Update appsettings.json with connection strings and credentials.

  2. Database Setup

    dotnet ef database update --project OnlineCourseSystem.Notifications

    Execute Infrastructure/Database/SP_CreateNotification.sql

  3. Run

    dotnet run --project OnlineCourseSystem.Notifications

    Swagger UI: https://localhost:5001/swagger


βš™οΈ Configuration

Setting Description
ConnectionStrings:DefaultConnection SQL Server connection string
RabbitMq:Host/Username/Password RabbitMQ connection
EmailSettings:* SMTP configuration
Firebase:CredentialsPath Firebase service account JSON path
WorkerSettings:* Batch sizes and polling intervals

Override via environment variables: ConnectionStrings__DefaultConnection=...


πŸ“š API Examples

Create Notification

POST /api/notifications
{
  "notificationType": "Course",
  "title": "New lesson available!",
  "content": "Lesson 5 is now available.",
  "courseId": "5f2c8c2c-18d8-46b0-b4e9-f0e4dc8e6a61",
  "userIds": ["c8c49232-4c8a-4f8c-8de1-4a70c7ad1140"]
}

Get User Notifications

GET /api/notifications?userId={userId}&isRead=false&pageNumber=1&pageSize=20

Update Preferences

POST /api/notificationpreferences/{userId}
{
  "notificationType": "Reminder",
  "email": true,
  "push": false,
  "inApp": true
}

Schedule Notification

POST /api/schedulednotifications
{
  "notificationType": "Reminder",
  "title": "Assignment due soon",
  "content": "Your assignment is due in 2 days",
  "userIds": ["user-id-1"],
  "scheduledFor": "2024-12-25T10:00:00Z"
}

πŸ”„ Background Workers

  • EmailOutboxWorker: Processes pending emails (batch size: 10, interval: 30s)
  • PushOutboxWorker: Sends push notifications via FCM (batch size: 10, interval: 30s)
  • ScheduledNotificationWorker: Processes scheduled notifications (batch size: 10, interval: 60s)

All workers include retry logic with exponential backoff.


πŸ“‘ Event-Driven Integration

Consumes events via RabbitMQ:

  • NotificationRequestedEvent: Creates notifications for specified users
  • UserEnrolledEvent: Auto-creates welcome notifications
  • PaymentSucceededEvent: Creates payment confirmation notifications

Events are automatically consumed via MassTransit consumers registered in Program.cs.


πŸ—„οΈ Database

Core Tables: Notifications, UserNotifications, NotificationPreferences, UserReferences, UserNotificationDeliveries

Outbox Tables: EmailOutbox, PushOutbox, ScheduledNotifications

Stored Procedures:

  • sp_CreateNotification: Atomic notification creation with TVP
  • sp_GetUserNotifications: Paginated user notification retrieval

βœ… Validation & Error Handling

  • FluentValidation: Automatic validation via MediatR pipeline behaviors
  • ExceptionMiddleware: Global exception handler with structured logging
  • Custom Exceptions: BadRequestException, NotFoundException
  • Standardized Responses: Via GlobalResponse.Shared

πŸ§ͺ Development

# Run tests
dotnet test OnlineCourseSystem.sln

# Format code
dotnet format OnlineCourseSystem.Notifications

# Create migration
dotnet ef migrations add <Name> --project OnlineCourseSystem.Notifications

# Apply migration
dotnet ef database update --project OnlineCourseSystem.Notifications

🚒 Deployment

dotnet publish OnlineCourseSystem.Notifications -c Release -o ./publish/notifications

Checklist:

  • Production connection strings configured
  • RabbitMQ cluster setup (if using events)
  • Firebase credentials configured
  • SMTP credentials configured
  • Database migrations applied
  • Stored procedures executed
  • Logging/monitoring configured
  • Worker settings tuned

πŸ“ Project Structure

OnlineCourseSystem.Notifications/
β”œβ”€β”€ Controllers/          # REST endpoints
β”œβ”€β”€ Consumers/            # MassTransit event consumers
β”œβ”€β”€ Features/            # CQRS feature modules (Commands/Queries/Handlers)
β”œβ”€β”€ Infrastructure/
β”‚   β”œβ”€β”€ Database/        # Stored procedures
β”‚   β”œβ”€β”€ Repositories/     # Data access (Unit of Work)
β”‚   └── Services/         # Domain services
β”œβ”€β”€ Workers/              # Background services
β”œβ”€β”€ Models/               # Entities, DbContext, Enums
└── Validators/           # FluentValidation rules

🀝 Contributing

  1. Fork & create feature branch
  2. Follow coding standards (nullable ref types, async/await, logging)
  3. Add tests for new functionality
  4. Run dotnet format and dotnet test
  5. Submit PR with detailed description

Built with ❀️ for the Codeway Online Courses Platform

About

.NET 8 Notifications microservice with CQRS + Outbox Pattern (Email/Push), RabbitMQ integration, background workers, and SQL Server stored procedures (TVP).

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors