A robust .NET 8 Windows service that monitors a directory for incoming JSON order files, processes them with validation and business rules, and persists results to a SQLite database.
- Event-driven file monitoring using FileSystemWatcher for real-time processing
- High-performance concurrent processing with configurable throttling
- Robust error handling with exponential backoff retry logic
- Idempotency guarantees preventing duplicate order processing
- Comprehensive validation with business rule enforcement
- SQLite persistence with Entity Framework Core
- Structured logging using Serilog for observability
- Windows Service support with easy installation scripts
- Extensive testing with unit and integration test coverage
The system consists of three main components:
- OrderProcessor.Service - The main Windows service that monitors files and processes orders
- OrderProcessor.Generator - A command-line tool for generating test order files
- OrderProcessor.Tests - Comprehensive unit and integration tests
- Event-driven file monitoring using
FileSystemWatcherinstead of polling for better performance - Concurrent processing with semaphore-based throttling to handle high-volume bursts
- Idempotency implemented through database constraints and duplicate checking
- Resilient error handling with exponential backoff retry logic
- Structured logging using Serilog for comprehensive observability
- Entity Framework Core with SQLite for reliable data persistence
- .NET 8.0 SDK or later
- Windows 10/11 or Windows Server 2019+
- Administrator privileges (for Windows Service installation)
git clone https://github.com/NoCap01/high-performance-order-processor.git
cd high-performance-order-processor
dotnet build OrderProcessor.sln --configuration Releasecd OrderProcessor.Service
dotnet runThe service will:
- Create an
IncomingOrdersdirectory if it doesn't exist - Start monitoring for
.jsonfiles - Create a SQLite database (
orders.db) in the current directory - Begin processing any existing files
Open a new terminal and run:
cd OrderProcessor.Generator
dotnet run -- --count 100 --delay 50 --invalid-percentage 0.1 --corrupted-percentage 0.05This generates 100 files with 10% invalid orders and 5% corrupted JSON files.
- Build in Release mode:
dotnet publish OrderProcessor.Service -c Release -o C:\OrderProcessor- Install the service:
sc create "OrderProcessorService" binPath="C:\OrderProcessor\OrderProcessor.Service.exe" start=auto
sc description "OrderProcessorService" "High-Performance Order Processing Service"- Start the service:
sc start "OrderProcessorService"sc stop "OrderProcessorService"
sc delete "OrderProcessorService"The generator supports various scenarios for comprehensive testing:
dotnet run -- --count 50dotnet run -- --count 1000 --burst --output-dir "IncomingOrders"dotnet run -- --count 200 --delay 25 --invalid-percentage 0.15 --corrupted-percentage 0.1 --include-high-value true| Option | Description | Default |
|---|---|---|
--count |
Number of files to generate | 10 |
--delay |
Delay between files (ms) | 100 |
--invalid-percentage |
Percentage of invalid orders (0.0-1.0) | 0.1 |
--corrupted-percentage |
Percentage of corrupted JSON (0.0-1.0) | 0.05 |
--include-high-value |
Include high-value orders (>$1000) | true |
--output-dir |
Output directory | "IncomingOrders" |
--burst |
Generate all files at once | false |
Id(Primary Key)OrderId(Unique, Required)CustomerName(Required)OrderDateTotalAmount(Decimal)IsHighValue(Boolean)ItemsJson(Serialized order items)ProcessedAt(UTC timestamp)FileName(Unique, for idempotency)
Id(Primary Key)FileName(Unique)RawContent(Original file content)FailureReason(Validation error message)ProcessedAt(UTC timestamp)
TotalAmountmust be >= 0CustomerNamemust not be empty or nullOrderIdmust not be empty or null
- Orders with
TotalAmount > $1000are marked as "HighValue" - All order items are serialized as JSON in the
ItemsJsonfield
The system ensures each file is processed exactly once through:
- Unique filename constraints in both ValidOrders and InvalidOrders tables
- Pre-processing checks that query the database before processing
- Database-level uniqueness preventing duplicate insertions
dotnet test OrderProcessor.Tests-
Valid Order Processing
- Standard orders are correctly parsed and saved
- High-value orders are properly flagged
- Order items are serialized correctly
-
Invalid Order Handling
- Negative amounts are rejected
- Empty customer names are rejected
- Missing order IDs are rejected
-
Corrupted JSON Handling
- Malformed JSON is caught and logged
- Corrupted files are saved to InvalidOrders table
-
Idempotency Testing
- Duplicate file processing is prevented
- Database constraints prevent duplicate records
-
High-Volume Testing
- Generate 1000+ files and verify processing
- Monitor memory usage and performance
- Verify no files are lost or duplicated
# Generate 1000 files at once
cd OrderProcessor.Generator
dotnet run -- --count 1000 --burst
# Monitor service logs for processing completion# Generate a file and immediately open it in a text editor
# Verify the service retries and eventually processes it# Generate files with various invalid conditions
dotnet run -- --count 100 --invalid-percentage 0.3 --corrupted-percentage 0.2# Generate files, stop service mid-processing, restart
# Verify idempotency prevents duplicate processingThe service uses structured logging with Serilog:
- Console output for development
- Rolling file logs in
logs/directory - Structured JSON format for easy parsing
- File detection and queuing
- Processing start/completion
- Validation failures
- Database operations
- Error conditions and retries
Information: Normal operationsWarning: Validation failures, file locksError: Processing failures, database errorsCritical: Service startup/shutdown issues
- Concurrent processing: Up to
Environment.ProcessorCount * 2files simultaneously - Memory efficient: Streaming file processing, no large in-memory collections
- Resilient: Exponential backoff retry for locked files
- Scalable: Event-driven architecture handles burst loads effectively
Modify appsettings.json to change the database location:
{
"ConnectionStrings": {
"DefaultConnection": "Data Source=C:\\Data\\orders.db"
}
}Adjust log levels and outputs in appsettings.json:
{
"Serilog": {
"MinimumLevel": {
"Default": "Information"
}
}
}- Files not processing: Check directory permissions and file locks
- Database errors: Verify SQLite file permissions and disk space
- High memory usage: Monitor concurrent processing limits
- Service won't start: Check Windows Event Log for detailed errors
Monitor these indicators:
- Log file growth and error rates
- Database file size growth
- IncomingOrders directory file count
- Windows Service status
With more time, I would implement:
- Health Check Endpoints: HTTP endpoints for monitoring service health and metrics
- Configuration Hot-Reload: Dynamic configuration updates without service restart
- Distributed Processing: Support for multiple service instances with coordination
- Advanced Retry Policies: Configurable retry strategies with dead letter queues
- Metrics and Telemetry: Integration with Application Insights or Prometheus
- File Archival: Automatic cleanup/archival of processed files
- Database Migrations: Proper EF Core migrations for schema evolution
- Performance Counters: Windows Performance Counters for system monitoring
The SampleOrders/ directory contains example files demonstrating:
- Valid standard orders
- High-value orders (>$1000)
- Invalid orders (negative amounts, empty customer names)
- Corrupted JSON files
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
Project Link: https://github.com/NoCap01/high-performance-order-processor