Suwbackkit.Server
0.5.0
See the version list below for details.
dotnet add package Suwbackkit.Server --version 0.5.0
NuGet\Install-Package Suwbackkit.Server -Version 0.5.0
<PackageReference Include="Suwbackkit.Server" Version="0.5.0" />
<PackageVersion Include="Suwbackkit.Server" Version="0.5.0" />
<PackageReference Include="Suwbackkit.Server" />
paket add Suwbackkit.Server --version 0.5.0
#r "nuget: Suwbackkit.Server, 0.5.0"
#:package Suwbackkit.Server@0.5.0
#addin nuget:?package=Suwbackkit.Server&version=0.5.0
#tool nuget:?package=Suwbackkit.Server&version=0.5.0
Suwbackkit.Server
Server defaults for the ASP.NET Core backends of Manomenu's apps, in two calls, the way .NET Aspire's ServiceDefaults does it:
Logging: JSON on the console, with scopes and UTC timestamps;
Microsoft.AspNetCoreatWarning. These are the lowest-priority configuration values, soappsettings.jsonor environment variables override any of them (Logging__Console__FormatterName=simple).ProblemDetails: every error is
application/problem+jsonwith atraceId(the W3C trace id the logs carry in their scope), from unhandled exceptions (UseExceptionHandler) and from empty error responses such as 404 (UseStatusCodePages). A request body the endpoint cannot read stays a 400, also in Development, where minimal APIs throwBadHttpRequestExceptioninstead of answering 400 themselves.OpenAPI:
AddOpenApiwith the document pinned to OpenAPI 3.1, so a newer .NET does not change the contract on its own. The pin holds for the document served at run time and for the oneMicrosoft.Extensions.ApiDescription.Serverwrites at build time: the build ignoresOpenApiOptions, so the package also adds--openapi-version OpenApi3_1toOpenApiGenerateDocumentsOptions, unless the app's project already names a version there. An app adds its own options with anotherAddOpenApi(...)call.Health checks:
/healthruns every check (503 when one fails),/aliveonly the liveness ones (the process answers); both stay out of the OpenAPI document and answer without credentials (AllowAnonymous), also in an app whose other endpoints ask for them. Other packages add their checks withAddHealthChecks(), asSuwbackkit.Postgresdoes.Contract schemas (on by default;
AddServerDefaults(contractSchemas: false)opts out): every schema says what the JSON always holds, so the types an app generates from the document (openapi-typescript) need no?or| nullwhere the server never leaves a value out: a property the server always writes and never asnull(a get-only property of a non-null reference type, which ASP.NET Core leaves optional and nullable) is required and not nullable; a union's discriminator ([JsonPolymorphic]) is required in each of its variants; andnullis no value of an enum (ASP.NET Core adds it to an enum used only as a nullable property, whose property already saysnull)./api/healthin the contract:MapHealthStatus()on a route group mapsGET <group>/health, answering{"status":"ok"}, or 503 ProblemDetails naming the failed checks; it answers without credentials too.Refusals: one shape for "the server refused, here is what is wrong and how to fix it" (below), from
Refusals.Refuse(...), with its schema in the contract from.ProducesRefusal<TCode>(status).
Usage
using Suwbackkit.Server;
var builder = WebApplication.CreateBuilder(args);
builder.AddServerDefaults();
var app = builder.Build();
app.MapServerDefaults();
app.MapGroup("/api").MapHealthStatus();
app.Run();
The app's own OpenAPI options go in another call, beside the defaults:
builder.Services.AddOpenApi(options => options.AddDocumentTransformer(...));
An app that is not ready for the stricter schemas yet keeps ASP.NET Core's with builder.AddServerDefaults(contractSchemas: false);
options.AddContractSchemas() stays public for an OpenApiOptions of a second document.
Call MapServerDefaults() right after Build(): it puts the exception handler first in the pipeline.
ServerDefaults.WritingOpenApiDocument is true while the build-time OpenAPI generator (GetDocument.Insider) runs the app only to read its
routes: skip anything that needs the outside world then, such as migrations or background workers.
builder.Services.AddDatabase(builder.Configuration, migrateOnStart: !ServerDefaults.WritingOpenApiDocument);
Refusals
A refusal is the server saying no because of what the caller sent (422, 409, 404, 403). Every server on this kit sends it in one
shape, so a web client (suwfrontkit's ApiError), a CLI and an agent read refusals of any of our apps the same way. It is an
RFC 9457 ProblemDetails (type, title, status, detail, traceId) with two extension members:
| Member | Type | Meaning |
|---|---|---|
code |
string, always on a refusal | why the request was refused, a stable dotted word (resource.stale); callers branch on it, never on the text. In the app's contract it is the app's enum of codes |
mistakes |
array, optional | one item per thing that is wrong |
mistakes[].code |
string | the kind of this mistake, an app enum like code |
mistakes[].message |
string | what is wrong, in a sentence a person or an agent reads |
mistakes[].fix |
string | what to do about it, in a sentence, never empty |
mistakes[].at |
string, optional | where it applies, in the app's own terms: a field path, a step id, a resource id |
| other members of a mistake | any | the app's own detail (a stage, a resource, related items), kept as sent |
With mistakes, detail says how many there are and to fix each as its fix says; without them, detail itself says what to do. A
failure that is not a refusal (an unhandled exception, a 503, a 404 of an unknown route) keeps the plain ProblemDetails, with no code.
ASP.NET Core's errors of a model-binding failure stays as the framework sends it.
[JsonConverter(typeof(JsonStringEnumConverter<NoteCode>))]
internal enum NoteCode
{
[JsonStringEnumMemberName("note.invalid")] Invalid,
[JsonStringEnumMemberName("note.stale")] Stale,
[JsonStringEnumMemberName("note.titleMissing")] TitleMissing,
}
routes.MapPut("/notes/{id}", Save)
.ProducesRefusal<NoteCode>(StatusCodes.Status409Conflict)
.ProducesRefusal<NoteCode>(StatusCodes.Status422UnprocessableEntity);
static Results<Ok<Note>, ProblemHttpResult> Save(...) =>
stale
? Refusals.Refuse(StatusCodes.Status409Conflict, "The note changed", NoteCode.Stale, "Read it again and save once more.")
: Refusals.Refuse(
StatusCodes.Status422UnprocessableEntity,
"The note cannot be saved",
NoteCode.Invalid,
[new Mistake<NoteCode>(NoteCode.TitleMissing, "The note has no title.", "Give it a title.") { At = "title" }]
);
Refuserefuses to build a refusal without a title, a detail or a code, or a mistake without a code, a message or a fix (ArgumentException): a refusal that cannot say what to fix is a bug of the server.- The codes are the app's own enum (with
JsonStringEnumMemberNamefor dotted names), so.ProducesRefusal<TCode>()lists them as the enum ofcodeand ofmistakes[].code, and a new code is a new enum value in the contract. Astringworks too, without the enum. - A mistake with more to say derives from
Mistake<TCode>(record NoteMistake(NoteCode Code, string Message, string Fix, Stage Stage) : Mistake<NoteCode>(Code, Message, Fix)); pass a list of it toRefuseand declare.ProducesRefusal<NoteCode, NoteMistake>(status), and its members travel and are documented next to the shared ones. - Mistakes may have codes of their own, a second enum beside the refusal's:
Refuse(status, title, NoteCode.Invalid, checkMistakes)withrecord CheckMistake(CheckCode Code, …) : Mistake<CheckCode>(…), declared as.ProducesRefusal<NoteCode, CheckCode, CheckMistake>(status). The contract then listsNoteCodeas the enum ofcodeandCheckCodeas the enum ofmistakes[].code. - In the contract the shape is
Refusal<TCode, TMistake>:code,title,detail,status,typeandtraceIdrequired,mistakesoptional, and each mistake'scode,messageandfixrequired.
ServerKit.Version is the version of this package, for an app that logs which kit it runs.
Source, license and changes: https://github.com/Manomenu/suwbackkit
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- Microsoft.AspNetCore.OpenApi (>= 10.0.12)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.