Milvonion Template
dotnet new milvonion generates a full API solution. This page summarizes the README that ships inside the generated solution. In the examples, Milvonion is replaced by your project name, api by your route prefix and 5000 by your HTTP port.
Tech stack
- .NET
- PostgreSQL
- Redis (cache, token revocation stamps, feature flags, rate limits)
- Serilog (Console, Seq, OpenTelemetry)
- OpenTelemetry (metrics and traces over OTLP)
- Docker
Requirements
| Requirement | Needed for | Notes |
|---|---|---|
| .NET 11 SDK | build, run, tests | global.json selects Microsoft.Testing.Platform for dotnet test. |
| Visual Studio 2022 17.13+ / Rider | IDE | The solution is a .slnx file. |
| PostgreSQL (17 recommended) | the application database | Any reachable server; docker-compose.yml runs one locally. |
| Redis (7) | required at runtime | Shared state of every instance; docker-compose.yml runs one locally. |
| Seq | logs when MILVA_ENV is test or prod | Optional locally; docker-compose.yml runs one. |
| OpenTelemetry collector | logs, metrics, traces when MILVA_ENV=prod | Optional; nothing is exported while OpenTelemetry:CollectorUrl is empty. |
| Docker | local dependencies, integration tests, images | The integration tests start PostgreSQL and Redis with Testcontainers. |
First run
1. Start local dependencies
Redis is required. docker-compose.yml runs Redis and, optionally, PostgreSQL and Seq for local development:
docker compose up -d
The Redis connection defaults to localhost:6379. If Redis is down the application still starts: rate limits fail open, the revocation check and feature flags fall back to the database, and the readiness probe reports 503.
2. Configure
| Key | Environment variable | Description |
|---|---|---|
ConnectionStrings:DefaultConnectionString | ConnectionStrings__DefaultConnectionString | PostgreSQL connection string. Empty in appsettings.json. |
Milvasoft:Caching:Redis:ConnectionString | Milvasoft__Caching__Redis__ConnectionString | Redis endpoint as host:port (default localhost:6379). |
CacheKeys:Prefix | CacheKeys__Prefix | Prefix of every Redis key (default: the project name in lower case). Applications sharing one Redis need different prefixes. |
SeqUri | SeqUri | Seq server URL. Empty disables Seq. |
OpenTelemetry:ServiceName | OpenTelemetry__ServiceName | Service name of the logs, metrics and traces. |
OpenTelemetry:CollectorUrl | OpenTelemetry__CollectorUrl | OTLP gRPC endpoint of the collector. Empty disables OpenTelemetry. |
Milvasoft:Identity:Token:SymmetricPublicKey | Milvasoft__Identity__Token__SymmetricPublicKey | Secret. HMAC-SHA256 signing key of the access tokens (at least 32 characters). |
Milvasoft:FileOperations:Json:EncryptionKey | Milvasoft__FileOperations__Json__EncryptionKey | Secret. AES key for Milvasoft's encrypted JSON file operations (16, 24 or 32 characters); only read once a project adds Milvasoft.FileOperations. |
| - | MILVA_ENV | dev, test or prod. |
Secrets. The appsettings files carry no secrets. Use user secrets on a developer machine and environment variables everywhere else. Set the signing key once, in the solution root:
dotnet user-secrets set "Milvasoft:Identity:Token:SymmetricPublicKey" "$(openssl rand -hex 32)" --project src/Milvonion.Api
dotnet user-secrets list --project src/Milvonion.Api
Outside Development, a missing or too short signing key stops the application at startup. In Development a random key is generated for the run and a warning is written to the console; tokens issued with it stop working when the application restarts.
3. Create the first migration
The template ships without migrations. Create the initial one before the first run (the migrations assembly is the Api project):
dotnet tool install --global dotnet-ef --version 11.0.0-rc.3.*
dotnet ef migrations add InitialCreate --project src/Milvonion.Api
MigrationHostedService applies pending migrations on every startup. Add a migration for every model change.
4. Run
dotnet run --project src/Milvonion.Api
The API documentation (Scalar) is at http://localhost:5000/api/documentation.
5. Seed initial data
POST /api/v1.0/developer/database/init seeds the root user, the super admin role, permissions, languages, UI data and the seed protection triggers, and returns the generated root password. database/seed seeds the same with development data. Developer endpoints require a super admin token and are disabled when MILVA_ENV=prod.
To initialize a new production database: deploy (or run the API once) against the empty database so the migrations are applied, then run the API on a developer machine with MILVA_ENV=dev pointed at that database and call database/init once. Store the returned root password; it is shown only once.
Environments (MILVA_ENV)
| Value | Logs | Notes |
|---|---|---|
dev | Console | Local development. |
test | Seq | Console when SeqUri is empty. |
prod | OpenTelemetry collector + Seq | Developer endpoints and the API documentation are disabled. Set by the production Dockerfile. |
| not set / other | Console | Treated as non-production; always set MILVA_ENV=prod in production. |
Docker images
Both images are built from the solution root:
docker build -t milvonion-api . # production: chiseled runtime, MILVA_ENV=prod, port 8080
docker build -t milvonion-api:dev -f Dockerfile-dev . # development / test: runtime with a shell
Pass configuration and secrets as environment variables when you run the container.
Conventions
The generated code follows a set of conventions, among them:
- Authorization is deny-by-default: every endpoint needs an authenticated user unless marked
[AllowAnonymous]. - Access tokens carry a revocation stamp. Password changes, role changes, locks and deletions rotate it, so the user's tokens stop working at once. Refresh tokens are single use.
- Handlers read the caller from
ICurrentUser, never from an ID in the request. [RateLimit]protects expensive or anonymous endpoints;[Idempotent]makes create endpoints safe to retry with anIdempotency-Keyheader;[FeatureFlag]lets an admin switch a feature off.[Log]method logging is off by default because arguments and results can contain personal data. Mark sensitive request properties with[LogMasked]before turning it on.- Configuration is read only in the composition root and bound to options classes.
- All times are UTC.
- New CRUD features can be generated with the MediatR CRUD Template.
Tests
Docker must be running, because the integration tests start PostgreSQL and Redis with Testcontainers.
dotnet build -c Debug
dotnet test --solution Milvonion.slnx --no-build
Architecture tests in tests/Milvonion.UnitTests/ArchitectureTests enforce layer dependencies, the CQRS feature layout, controller conventions, data access rules, localization, code style and more on every run.
The build scripts run the unit and integration tests and write an HTML report per test assembly to ./artifacts/test-results:
build\build.bat # Windows
bash build/build.sh # Linux
Health checks
GET /api/v1.0/healthcheck: liveness.GET /api/v1.0/healthcheck/ready: readiness; 200 when the database and Redis answer, 503 otherwise.
Project structure
The layers follow Onion Architecture, and the .csproj dependencies follow the same rules:
- Core
- Domain: the core business logic and entities.
- Application: use cases; orchestrates the domain and infrastructure layers.
- Infrastructure
- Infrastructure: external dependencies, technical logic and persistence.
- Presentation
- Api: the executable project with the endpoints and configuration.
- Tests
- UnitTests and IntegrationTests.
src
├── Milvonion.Api
├── Milvonion.Infrastructure
├── Milvonion.Application
└── Milvonion.Domain
tests
├── Milvonion.IntegrationTests
└── Milvonion.UnitTests
docs
build
Design patterns used: CQRS, Mediator, Factory and Singleton.