.NET / ASP.NET Core¶
Scope: back-end services, web APIs, web apps and worker services on modern .NET.
Entries are sorted by score from 1 to 10 (criteria): 9โ10 start here or strongly recommended, 6โ8 recommended, 1โ5 further reading or conditional.
Adopted references¶
- 9 .NET Architecture Guides (Microsoft): the baseline. A hub of free e-books, including:
- 9 Architect Modern Web Applications with ASP.NET Core and Azure: monolithic and Clean Architecture web apps.
- 8 .NET Microservices: Architecture for Containerized .NET Applications: DDD, CQRS and microservices in .NET.
- 6 Architecting Cloud Native .NET Applications for Azure.
- 9 ASP.NET Core best practices (Microsoft): performance and reliability rules.
- 8 eShop reference application (Microsoft): a current reference architecture.
- 8 ASP.NET Core diagnostic scenarios: async guidance (David Fowler).
- 7 Framework Design Guidelines (Microsoft): for designing reusable libraries and public APIs.
- 7 C# coding conventions (Microsoft).
Key rules¶
- 9
DOTNET-1Load secrets through configuration providers: user secrets (the Secret Manager) in development, and Azure Key Vault or another vault in production, neverappsettings.jsonor source code. This is how .NET apps followSEC-2in general/security.md. (Microsoft, Safe storage of app secrets) - 9
DOTNET-2Go async all the way and never block on async code with.Resultor.Wait(). (Microsoft, ASP.NET Core best practices, Fowler, Async guidance) - 8
DOTNET-3Use the built-in DI container and follow its lifetime guidelines. For example, don't resolve scoped services from singletons. (Microsoft, Dependency injection in ASP.NET Core, Dependency injection guidelines) - 8
DOTNET-4AddAddStandardResilienceHandlerfromMicrosoft.Extensions.Http.Resilienceto outgoingHttpClients. It applies timeouts, retries with backoff and a circuit breaker, asOPS-3andOPS-5in general/operations-reliability.md ask. (Microsoft, Build resilient HTTP apps) - 7
DOTNET-5When the domain has real business logic, structure the solution with Clean Architecture: an application core that defines interfaces, and infrastructure projects that implement them and that the core never references. This is the .NET form ofSTYLE-6in general/architecture-styles.md, which also says when the layers aren't worth it. (Microsoft, Common web application architectures) - 7
DOTNET-6RegisterAddProblemDetailsandUseExceptionHandler, so that errors are returned as ProblemDetails and exceptions are handled in one place. The general rule isAPI-7in general/api-design.md. (Microsoft, Handle errors in ASP.NET Core) - 7
DOTNET-7Map separate liveness and readiness endpoints withMapHealthChecks, selecting the checks for each by tag. The general rule isOPS-10in general/operations-reliability.md; on Kubernetes, seeK8S-2. (Microsoft, Health checks) - 7
DOTNET-8Instrument with the OpenTelemetry .NET SDK, or with the .NET Aspire service defaults, which configure it for you. The general rule isOPS-11in general/operations-reliability.md. (Microsoft, .NET observability with OpenTelemetry) - 7
DOTNET-9Enable nullable reference types in all projects. (Microsoft, Nullable reference types) - 7
DOTNET-10Test the HTTP pipeline with in-memory integration tests (WebApplicationFactory), and enforce layering with architecture tests. (Microsoft, Integration tests, ArchUnitNET) - 6
DOTNET-11Bind configuration to strongly typed classes with the options pattern. (Microsoft, Options pattern) - 6
DOTNET-12Follow the EF Core performance guidance for queries and change tracking. (Microsoft, EF Core performance)
How to enforce¶
Tools that check the key rules above automatically, in CI, the IDE or a commit hook. "Partly" means the tool covers only some of what the rule asks.
- 7 Built-in DI scope validation: in the Development environment, the default service provider fails when scoped services are resolved from the root provider or injected into singletons. Integration tests run in Development by default, so they catch it. Checks
DOTNET-3. - 7 Nullable warnings as errors:
<WarningsAsErrors>nullable</WarningsAsErrors>makes nullability warnings fail the build. Set it for every project in Directory.Build.props. ChecksDOTNET-9. - 7 CA1849: flags blocking calls inside async methods when an async version exists. It's off by default; enable it, and build with
-warnaserrorto fail on it. Partly checksDOTNET-2. - 7 GitHub secret scanning push protection: blocks pushes that contain credentials. See general/security.md for more secret scanners. Checks
DOTNET-1. - 6 ArchUnitNET: architecture tests, for example that the core namespace doesn't depend on infrastructure, or that modules are free of cycles. Checks
DOTNET-5andDOTNET-10. - 6 Microsoft.VisualStudio.Threading.Analyzers, VSTHRD002 and VSTHRD103: warn about synchronous waits on tasks, such as
.Wait(),.Resultand.GetAwaiter().GetResult(). Enable only these rules; others in the package assume Visual Studio extensions. ChecksDOTNET-2.
Tutorials and further reading¶
- 7 Aspire: tooling for building, running and observing distributed .NET apps locally and in the cloud.
- 6 Clean Architecture template (Ardalis) and Clean Architecture template (Jason Taylor): community starting points.
- 6 Testing in .NET (Microsoft).