Three tiers: a Blazor Server UI (BlazorApp), a REST API (WebApi) and MongoDB.
The UI only talks to the API over HTTP, and the API alone reads and writes the database.
Layered architecture across small single-purpose projects in src/, with Domain at the center and nothing referencing outward.
Common.System, depending on nothing: cross-cutting primitives (IHasId,IHasIdAndOwnerId,PagedRequest,PagedResult<T>,TitleNormalizer,ListSort,RefuelCostCheck).Common.Hosting, depending on nothing of the solution: the OpenTelemetry setup thatWebApiandBlazorAppboth call.Domain, depending onCommon.System: business models (Models/*Model), repository interfaces (Repositories/I*Repository) and pure services (Services/), with no persistence or web concerns.Infrastructure.MongoDb, depending onDomain: BSONEntities/,Repositories/,Mappers/.WebApi.Contracts, depending onCommon.System: the public REST DTOs (Dto/), shared withBlazorAppso the client deserializes without duplicate classes.WebApi, depending onCommon.Hosting,Infrastructure.MongoDb,DomainandWebApi.Contracts: controllers, DTO mappers, DI, JWT auth, OpenAPI/Scalar.BlazorApp, depending onCommon.Hosting,Common.SystemandWebApi.Contracts: the Blazor Server UI over HTTP, never referencingDomainorInfrastructure.MongoDb.
Controllers live in WebApi/Controllers/, with any real computation in Domain/Services/.
WebApi/Import/ and WebApi/ReferenceData/ use an older feature-folder shape that new code does not extend (see backlog).
The data conventions shared by every layer are in Data model.
DataCrudControllerBase<TDto, TModel> implements the whole CRUD surface once.
It reads the caller's user_id claim through ControllerBaseExtensions.GetUserId() to scope every query and stamp OwnerId, and every controller uses that extension rather than re-reading the claim.
A per-type controller is a one-line class extending it, and CRUD logic is never duplicated per controller.
Read-only cross-entity aggregations (WatchNextController, WishlistController, StatsController, SystemStatusController) are plain ControllerBase classes.
A record update never changes a reference link (DataCrudControllerBase.PreserveServerOwnedFieldsAsync, over IReferenceLinkedModel).
A PUT is a full replace, and a detail page opened before background resolution holds a copy with an empty ReferenceId, so its first edit would erase the link.
The link and its rating fields are restored from storage on every update, the same way OwnerId is protected.
Resolution, the detail page's check and an admin unlink write the link through a repository, never through the CRUD controller.
ApiExceptionFilterAttribute logs every unhandled exception, then converts it: ArgumentException to 400, a failed provider call to 502, anything else to 500.
- A provider that timed out, exhausted its retries or tripped its circuit breaker is a 502, never a 500 (
TimeoutRejectedException,BrokenCircuitException,HttpRequestException, logged as a warning). A 500 would blame this API for a provider outage. - The 502's
{ error }says what the provider did (DescribeUpstreamFailure: its status, unreachable, timed out or circuit open). BlazorApp/Components/Shared/ApiResponseExtensions(EnsureSuccessOrThrowAsync,ReadJsonOrThrowAsync) is used wherever a failure is shown to a user, neverEnsureSuccessStatusCodeorGetFromJsonAsync, which discard the body and leave the user with "502 Bad Gateway". They throwApiRequestException, whoseIsUpstreamProviderFailuretells a provider outage from a defect.InlineReferenceLinkerthen names the provider, quotes the detail, and points at the provider picker when the domain has more than one.
A missing item is a 404 at every layer, never the error page: see Blazor UI.
Every third-party client chains .AddStandardResilienceHandler() on its AddHttpClient<...>() registration, a new one included, never a hand-rolled retry (ExternalProviderResilienceTest).
Provider specifics are in Providers.
Long-running work runs as a background job, never a blocking request: buffer the input, start the work on a fresh IServiceScopeFactory.CreateScope(), return a job id, poll its status.
JobStore<TStage, TResult> (WebApi/Jobs/) is backed by MongoDB (background_job, TTL 7 days), since the replica answering a poll is not necessarily the one running the job.
The owner id is checked in the repository query on every read, and a background task resolves its own JobStore from its own scope.
A detached job runs on IHostApplicationLifetime.ApplicationStopping, never an unbounded token.
On shutdown it would otherwise keep working against disposed singletons (Mongo client, HTTP clients, IGDB's rate limiter), every step would throw ObjectDisposedException, and the job would read "Running" forever.
For the same reason, per-item catches that keep one failing document from aborting a run (ReferenceSyncService, ExploreCatalogueRefreshService) exclude OperationCanceledException.
HostOptions.BackgroundServiceExceptionBehavior = Ignore in Program.cs is a backstop, since by default an exception escaping any BackgroundService stops the entire host.
A background service still catches what it can anticipate.
A request timeout leaves no trace in the WebApi logs, since "Microsoft": "Warning" hides request start and finish.
CancellationToken passing is incomplete: see backlog.
- Firebase authenticates: a cookie in
BlazorApp, a JWT bearer validated against Firebase inWebApi, andAuthenticationTokenHandlerattaches the bearer to outgoing API calls. - Authorization is policy based:
AdminOnlyisRequireClaim("role", "admin")andMemberOnlyisRequireClaim("role", "member", "admin"), registered withAddAuthorizationBuilder()in bothProgram.csfiles. Firebase sends a plainroleclaim, whichBlazorApp'sAuthenticationControllercopies into the cookie principal at sign-in (FirebaseClaimsBuilder). - Gotcha:
AddJwtBearersetsMapInboundClaims = false, otherwise the handler renames short JWT claim names to legacyClaimTypes.*URIs andRequireClaim("role", ...)never matches. A new custom claim is checked against this, and it is also what keeps theemailclaim readable for collection sharing.
An account with no role claim gets movies and TV shows only, capped at Features:FreeTierItemLimit per collection (default 20, AppConfiguration.GetFreeTierItemLimit).
Episodes are capped at 100 times that (EpisodeController.FreeTierLimitFactor), only to stop a raw API caller flooding the database.
Enforcement is API side: [Authorize(Policy = "MemberOnly")] on every restricted controller, plus the creation quota in FreeTierQuota.CheckAsync,
called by DataCrudControllerBase.Post and by every other path that creates an item (403 with { error }).
NavMenu.razor hiding sections is UX, never security, and FreeTierTest carries a reflection guard asserting each controller's expected policy.
Runtime-changeable global settings live in one app_setting document (_id: "global", one field per setting) through IAppSettingRepository, written with a targeted $set.
A new setting is a new field there, never a new collection.
Deploy-time values stay in AppConfiguration and environment variables, since those cannot change without a redeploy.
Scaling is designed in, never assumed, since the app may sit behind a Cloudflare tunnel with no sticky sessions.
DataProtection:MongoDb:*persists the key ring (DataProtection/MongoDbXmlRepository) so cookies decrypt on every replica, the only reasonBlazorApp.csprojreferencesMongoDB.Driver.Features:IsWebSocketsOnlyEnabled(defaulttrue) pins a circuit to its pod, and is setfalseonly behind a proxy without WebSockets, with a single replica.- Background jobs are stored in MongoDB, and the reference sync takes a lease (see Reference data).