Suwbackkit.Server 0.4.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Suwbackkit.Server --version 0.4.0
                    
NuGet\Install-Package Suwbackkit.Server -Version 0.4.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Suwbackkit.Server" Version="0.4.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Suwbackkit.Server" Version="0.4.0" />
                    
Directory.Packages.props
<PackageReference Include="Suwbackkit.Server" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Suwbackkit.Server --version 0.4.0
                    
#r "nuget: Suwbackkit.Server, 0.4.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Suwbackkit.Server@0.4.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Suwbackkit.Server&version=0.4.0
                    
Install as a Cake Addin
#tool nuget:?package=Suwbackkit.Server&version=0.4.0
                    
Install as a Cake Tool

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.AspNetCore at Warning. These are the lowest-priority configuration values, so appsettings.json or environment variables override any of them (Logging__Console__FormatterName=simple).

  • ProblemDetails: every error is application/problem+json with a traceId (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 throw BadHttpRequestException instead of answering 400 themselves.

  • OpenAPI: AddOpenApi with 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 one Microsoft.Extensions.ApiDescription.Server writes at build time: the build ignores OpenApiOptions, so the package also adds --openapi-version OpenApi3_1 to OpenApiGenerateDocumentsOptions, unless the app's project already names a version there. An app adds its own options with another AddOpenApi(...) call.

  • Health checks: /health runs every check (503 when one fails), /alive only 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 with AddHealthChecks(), as Suwbackkit.Postgres does.

  • 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 | null where the server never leaves a value out: a property the server always writes and never as null (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; and null is no value of an enum (ASP.NET Core adds it to an enum used only as a nullable property, whose property already says null).

  • /api/health in the contract: MapHealthStatus() on a route group maps GET <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" }]
        );
  • Refuse refuses 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 JsonStringEnumMemberName for dotted names), so .ProducesRefusal<TCode>() lists them as the enum of code and of mistakes[].code, and a new code is a new enum value in the contract. A string works 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 to Refuse and 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) with record CheckMistake(CheckCode Code, …) : Mistake<CheckCode>(…), declared as .ProducesRefusal<NoteCode, CheckCode, CheckMistake>(status). The contract then lists NoteCode as the enum of code and CheckCode as the enum of mistakes[].code.
  • In the contract the shape is Refusal<TCode, TMistake>: code, title, detail, status, type and traceId required, mistakes optional, and each mistake's code, message and fix required.

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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.5.2 80 11/22/2026
0.5.1 78 11/22/2026
0.5.0 77 11/21/2026
0.4.0 101 11/21/2026
0.3.0 66 11/21/2026
0.2.2 56 11/21/2026
0.2.1 49 11/21/2026
0.2.0 46 11/20/2026
0.1.0 128 11/20/2026