New C# topic rules for .NET MAUI and Blazor, both centered on performance and cross-platform reuse. MAUI covers compilation strategy per platform (ReadyToRun, NativeAOT), trim and AOT safety, lean startup, CollectionView and flat layouts, and per-platform conventions. Blazor covers render mode cost, WebAssembly payload size and AOT trade-offs, prerender state, rendering cost, and Razor class libraries shared across web, WebAssembly, and Hybrid hosts. The C# index and global routing now name both. The existing C# rules gain the net11.0 default of C# 15, async void only for event handlers, explicit StringComparison, invariant culture for machine data, UTC or DateTimeOffset for stored times, ProblemDetails, and ValidateOnStart. Duplicate authorization and raw SQL guidance in the architecture rule is merged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
15 lines
1.8 KiB
Markdown
15 lines
1.8 KiB
Markdown
# C# Safety and Error Handling
|
|
|
|
Apply these rules when handling external input, failures, logs, or sensitive information.
|
|
|
|
* Treat external data as untrusted and validate it at system boundaries. Do not rely on static types to validate runtime input.
|
|
* Validate size, range, format, and allowed values before expensive parsing or allocation. Keep validation consistent with downstream business and security constraints.
|
|
* Fail fast for invalid arguments or impossible state. Do not use exceptions for routine control flow or swallow unexpected exceptions.
|
|
* Preserve useful exception context without exposing secrets, personal data, or internal details in logs, APIs, or user-facing errors.
|
|
* Use structured logging with message templates and named placeholders, not string interpolation (CA2254); use `[LoggerMessage]` source-generated methods in hot paths. Never log credentials, tokens, or sensitive payloads.
|
|
* Dispose owned resources with `using` or `await using` (for `IAsyncDisposable`), including on failure paths. Do not dispose dependencies injected by the container.
|
|
* In ASP.NET Core, return errors as `ProblemDetails` (`AddProblemDetails`) without internal details, and validate options at startup with `ValidateDataAnnotations` or a validator plus `ValidateOnStart()` so invalid configuration fails before the first request.
|
|
* Use narrow exception handling around the operation that can fail. Do not catch broadly merely to retry, continue with partial state, or convert failures into success.
|
|
* Avoid returning different error detail to unauthenticated callers when it would disclose whether protected accounts, records, or resources exist.
|
|
* Treat resource limits as part of safe error handling: cap request bodies, page sizes, query complexity, decompressed data, collection sizes, parser depth, and other attacker-controlled work.
|