/shiny:shiny-httpserverfrom shiny
Generate code using Shiny.Net.HttpServer — a dependency-light, AOT/trim-clean HTTP/1.1, HTTP/2 & HTTP/3 server that runs anywhere .NET runs, including .NET MAUI and native tvOS, where ASP.NET Core cannot. Covers routing, middleware, source-generated typed endpoints, results and JSON, content negotiation with XML/MessagePack/protobuf formatters in both directions, static files and Blazor WASM, uploads/downloads, WebSockets, SSE, SignalR-style switchboards (real-time two-way calls over SSE with groups, an operator, client results, resumable lines, and a generated typed .NET client), sessions, OpenAPI, authentication (Basic/API key/cookie/JWT), authorization, CORS, rate limiting, IP filtering, TLS and self-signed certificates, tunnelling (relay, SSH, quick tunnels, Azure Relay, and supervised cloudflared/ngrok/tailscale agents), serving a directory over WebDAV, serving gRPC and gRPC-Web, hosting an MCP server with RFC 9728 OAuth discovery, receiving the OAuth/OIDC loopback redirect so a desktop app or CLI can sign in through the system browser (RFC 8252), health checks, OpenTelemetry-shaped metrics and tracing, W3C access logs, request timeouts, output caching and conditional requests, request decompression, antiforgery and browser security headers, a reverse proxy with destination clusters, load balancing, health checks, session affinity, transforms, WebSocket forwarding and IConfiguration-driven routes, mDNS/Bonjour advertising and discovery, MAUI lifecycle (background/foreground, Android foreground service, network rebinding), host filtering against DNS rebinding, request localization, webhook signature verification (GitHub/Stripe/Slack/Standard Webhooks), idempotency keys, RFC 9530 content digests, 103 Early Hints, the PROXY protocol, RFC 6902 JSON Patch, API versioning with per-version OpenAPI, a Scalar API reference page, tus resumable uploads, CalDAV/CardDAV, a private NuGet feed (V3 protocol with push), a private npm registry (publish/install/dist-tags/deprecate/unpublish with npmjs.org fall-through), Dropbox-style two-way folder sync with content-defined chunking and a Shiny.Net.Http background-transfer client, automatic HTTPS via ACME (Let's Encrypt/ZeroSSL), and an in-memory test harness.
Trending skills
7-day visitors to each skill page
Ships in shiny. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
You are an expert in Shiny.Net.HttpServer, a dependency-light HTTP server for .NET.
Invoke this skill when the user wants to:
Shiny.Net.HttpServer appdotnet nuget push and dotnet restore talk tonpm publish / npm install (and pnpm, Yarn) talk togh auth login do) without HttpListenerDo not use this skill for ASP.NET Core / Kestrel / minimal APIs. Those are a different library with similar-looking names.
Documentation: https://shinylib.net/httpserver
Only Microsoft.Extensions.* abstractions are taken as dependencies. Everything else — JSON, crypto,
JWT, OpenAPI, HPACK, QPACK — is in the box. Everything targets net10.0 and net11.0 with the trim,
AOT and single-file analyzers on; Shiny.Net.HttpServer.Mobile adds -android, -ios and
-maccatalyst for both. On net11.0-android the floor is API 24 (the .NET 11 Android SDK's minimum);
net10.0-android stays at 21.
The single hard rule: nothing is discovered by reflection. Routes and binders are generated at
compile time; JSON goes through JsonTypeInfo from a JsonSerializerContext. Any code you generate
must hold that line, or it fails on a trimmed device build.
dotnet add package Shiny.Net.HttpServer # the server + the typed-endpoint generator
dotnet add package Shiny.Net.HttpServer.Jwt # JWT auth
dotnet add package Shiny.Net.HttpServer.Proxy # reverse proxy: clusters, LB, health, transforms
dotnet add package Shiny.Net.HttpServer.Ssh # SSH + quick tunnels
dotnet add package Shiny.Net.HttpServer.AzureRelay # Azure Relay tunnel (NOT AOT-clean)
dotnet add package Shiny.Net.HttpServer.Mcp # Model Context Protocol transport
dotnet add package Shiny.Net.HttpServer.Mediator # Shiny.Mediator handlers as endpoints
dotnet add package Shiny.Net.HttpServer.DocumentDb # Shiny.DocumentDb types as REST resources
dotnet add package Shiny.Net.HttpServer.WebDav # a directory as a WebDAV mount (RFC 4918)
dotnet add package Shiny.Net.HttpServer.Grpc # gRPC + gRPC-Web services
dotnet add package Shiny.Net.HttpServer.Discovery # mDNS/Bonjour advertise + browse
dotnet add package Shiny.Net.HttpServer.Mobile # mobile lifecycle (multi-targets android/ios/maccatalyst; not tvOS)
dotnet add package Shiny.Net.HttpServer.Testing # in-memory HttpClient for tests
dotnet add package Shiny.Net.HttpServer.Tunnels # cloudflared / ngrok / tailscale agents (desktop + CLI only)
dotnet add package Shiny.Net.HttpServer.OAuthLoopback # OAuth/OIDC loopback redirect receiver (desktop + CLI only)
dotnet add package Shiny.Net.HttpServer.Tus # tus 1.0.0 resumable uploads
dotnet add package Shiny.Net.HttpServer.NuGet # a private NuGet V3 feed (push, restore, search)
dotnet add package Shiny.Net.HttpServer.Npm # a private npm registry (publish, install, npmjs fall-through)
dotnet add package Shiny.Net.HttpServer.FileSync # Dropbox-style sync server (chunk store + change feed)
dotnet add package Shiny.Net.HttpServer.FileSync.Client # its client: two-way folder sync over Shiny.Net.Http
dotnet add package Shiny.Net.HttpServer.CalDav # calendars & contacts over CalDAV/CardDAV
dotnet add package Shiny.Net.HttpServer.Acme # automatic HTTPS via Let's Encrypt/ZeroSSL (public hosts, not phones)
dotnet tool install -g Shiny.Net.HttpServer.CommandLine # `shinyhttpserver`, not a library reference
Shiny.Net.HttpServer.CommandLine is a .NET tool, not something an app references: it serves a
directory over HTTP from a terminal (shinyhttpserver [path] -m read|create|update|delete|all -u user:password). Reach for it when the ask is "serve this folder", not "add a server to my app".
It mounts the directory over WebDAV, so one address is both a browser file manager (browse,
upload, rename, delete — whatever -m allows) and a drive Finder, Explorer or a Linux file manager
can mount. Scripting it is WebDAV: GET/PUT/DELETE as usual, MKCOL for a directory, MOVE
for a rename, and PROPFIND with Depth: 1 for a machine-readable listing — a GET on a directory
returns the manager's HTML, not JSON. -m is enforced before the handler runs, across MKCOL,
COPY and MOVE as well as PUT; a MOVE is judged by where it lands, and renaming needs
update and delete.
It listens on every interface by default and ends its banner with a scannable QR code of the LAN
address plus the URL in full, so "get this folder onto my phone" is the tool answer, not code —
-a localhost keeps it to the machine, --no-qr drops the code. Basic auth (-u) over plain HTTP
refuses to start on a non-loopback address, which the default now is: pair it with --https.
--tunnel is the answer to "share this folder with someone not on my network": it opens a
QuickTunnel to pinggy.io and the QR code carries the public HTTPS address instead of the LAN one.
Because the tunnel feeds HttpServer.ServeAsync directly, --tunnel -a localhost binds nothing on
the LAN and is reachable only through the tunnel — and a tunnelled connection counts as encrypted
transport, so -u works over it without --https or --allow-insecure-auth. Anonymous tunnels stop
after 60 minutes; --tunnel-token <token> lifts that and implies --tunnel. Always say that the
address is public when you suggest it.
In a terminal, shinyhttpserver opens a full-screen dashboard rather than printing the banner:
Overview (addresses, tunnel, warnings, QR code), Requests (every request, newest first, including
401s and tunneled clients; Enter for headers, Ctrl+L clears), Transfers (uploads and downloads
in flight with progress, speed and time left), Settings (every flag as a form, applied live with
Ctrl+S), and Log. So "watch who is hitting this folder" or "see how far the upload got" is the tool
answer too. Settings changes to the tunnel, its token, the QR code or the log level apply in place;
anything else rebuilds the server, and the dashboard asks first when that would cut off a transfer
and rolls back if the new settings will not start. --no-tui keeps the plain banner, and a
redirected run (a script, a pipe, a service) gets the banner whatever the flags say, so suggest
--no-tui only when a user wants plain output in an interactive terminal. The dashboard is part of
the tool, not a library API, so it sits outside the four tiers below. An app that wants the same
request log writes a middleware (tier 3) that wraps Request.Body and Response.BodyControl, the
way the tool does.
Every new API belongs to one of these. Say which when you introduce one. They compose in one app.
| Tier | What it is | Use when |
|---|---|---|
| 0 | OnRequest(ctx => …) — one delegate, no routing | A single handler, a test fixture, a fallback |
| 1 | MapGet/MapPost/… — raw handlers behind a route template | A handful of routes, no binding wanted |
| 2 | Use(...) / IHttpMiddleware — the pipeline | Cross-cutting work |
| 3 | [Route] classes + the source generator | Anything real: typed parameters, DI, OpenAPI |
Default to tier 3 when the user has more than a couple of endpoints or wants typed parameters. Default to tier 1 for small, script-like servers. Never suggest reflection-based alternatives.
Choose the host shape from who owns the container:
// (a) No container — smallest possible
var server = new HttpServer(new HttpServerOptions { Port = 8080 });
server.MapGet("/ping", ctx => ctx.Response.WriteAsync("pong"));
await server.RunAsync();
// (b) The builder — a console app or service that wants DI
var builder = HttpServer.CreateBuilder();
builder.Options.Port = 8080;
builder.AddAuthentication().AddJwtBearer(o => { o.Issuer = "app"; o.Audience = "app"; o.SigningKey = key; }); // server features: on the builder
builder.Services.AddSingleton<IWidgetStore, WidgetStore>(); // your own: on Services
var app = builder.Build();
app.MapMyAppEndpoints();
await app.RunAsync();
// (c) An existing container — MAUI, generic host. Same builder, same calls.
services.AddShinyHttpServer(
http =>
{
http.Options.Address = IPAddress.Any;
http.Options.Port = 0;
http.AddAuthentication().AddBasic<CredentialStore>(o => o.Realm = "Device");
http.Configure(server => server.MapMyAppEndpoints());
},
autoStart: false // for an app with a "share" toggle
);
Every registration in this library is an extension on ShinyHttpServerBuilder, not on
IServiceCollection — AddAuthentication, AddCors, AddRateLimiter, AddHealthChecks,
AddOutputCache, AddSessions, the tunnels, discovery, the mobile lifecycle. Generate
builder.AddX(...), never builder.Services.AddX(...), for anything this library owns;
builder.Services is for the app's own registrations. The one exception is
services.AddHttpServerLocator(), which is the client half of mDNS and has no server to hang off.
http.Configure(server => ...) is where routes and middleware go in shape (c) — it runs when the
server is first resolved, so it can pull things out of the container.
autoStart: false + server.StartAsync() from the UI is the right shape for MAUI. Port = 0 lets
the OS pick; read it back from server.ListenUrl.
Address = IPAddress.Any for LAN access — deliberately.Limits.MaxRequestBodySize is 30 MB, the default for every route. For uploads, raise it on the
upload route (.WithRequestSizeLimit(bytes) / [RequestSizeLimit(bytes)]), not server-wide —
see Content. HTTP/3 is always held to the server-wide value.HideExceptionDetails is on; turn it off in development only.Limits.RequestBodyTimeout (30 s) for more bytes (then 408), and a
response write at most Limits.ResponseWriteTimeout (60 s) for the client to read (then the
connection - or, on HTTP/2, just that stream - is aborted and the write throws IOException).
Both measure the gap between bytes, not the whole transfer, and both apply to HTTP/1.1 and HTTP/2.InvalidOperationException. Never copy request input into a header unchecked and expect it to go
out; writing a body on a 204/304 throws too.RequestAborted fires when an HTTP/1.1 client hangs up (once its body is read) and on
ctx.Abort(), before Abort() returns. Abort() ends only that request: HTTP/1.1 closes the
connection, HTTP/2 and HTTP/3 reset just that stream. A handler exception such as FileNotFoundException or an HttpClient
TaskCanceledException is a 500 through the exception-handler chain, not a silent close.Listener configuration, underneath all four tiers — handler code only reads ctx.Connection.
builder.Options.Listen(IPAddress.Any, 8080)
.UseProxyProtocol(p => p.Trust("10.0.0.0/8")); // the BALANCER's addresses, not clients'
// single endpoint: builder.Options.ProxyProtocol = new ProxyProtocolOptions { Mode = ProxyProtocolMode.Required }.Trust("10.0.0.0/8");
ctx.Connection.RemoteIpAddress/RemotePort become the client's, and the IP filter, rate limiter and W3C logs need nothing else.TrustedProxies must not be empty, or the server refuses to start. Only generate .Trust("0.0.0.0/0").Trust("::/0") when the user says the port is firewalled so only the balancer can reach it.Required (the default for UseProxyProtocol) closes connections that have no header. Use Optional only while migrating.ctx.Connection.ProxyProtocol (null if none): ProxyEndPoint, DestinationEndPoint, Alpn, Authority, UniqueId, Ssl, GetTlv(0xEA).UseForwardedHeaders: it can't be spoofed by clients and it works with TLS passthrough. UseForwardedHeaders applies to HTTP/1.1 only and reads X-Forwarded-For/-Proto/-Host from the right, one hop per trusted proxy up to o.ForwardedHeaders.ForwardLimit (1). With o.ForwardedHeaders.KnownProxies/KnownNetworks empty it believes any peer — always generate a KnownNetworks/KnownProxies entry for the proxy's addresses, and never enable it on a server that faces the internet directly without one. For a chain (CDN → LB → app) raise ForwardLimit and list the inner proxies. Don't enable both unless the balancer sets or appends X-Forwarded-For, because that header overrides the PROXY address on HTTP/1.1.Start and stop are runtime operations, not just process startup and shutdown. An app with a toggle flips this switch repeatedly, so the transitions are serialized, idempotent and restartable.
await server.StartAsync(); // binds and begins accepting
await server.StopAsync(); // unbinds, closes idle connections, lets in-flight requests finish
await server.RestartAsync(); // both as one operation, re-reading Options
A stop does not cancel RequestAborted until Options.ShutdownTimeout (5 s) or the stop's token
runs out; then the remaining connections are cancelled and aborted. A long-lived handler (SSE,
WebSocket) therefore holds a stop for the full timeout — lower ShutdownTimeout in an app that stops
often. HTTP/2 follows the same rules: GOAWAY at once, then open streams finish within the timeout.
HTTP/2 limits live on Options.Http2: MaxConcurrentStreams counts a stream until its handler finishes
(a reset doesn't free the slot, so Rapid Reset clients get ENHANCE_YOUR_CALM), and MaxHeaderListSize
(32 KiB, decoded) answers oversized headers with 431 before any handler runs. Raise it rather than
working around a 431 for large cookies or tokens.
StateChanged gives a UI its transitions. StateTransitioned is what to generate whenever the app
needs to diagnose a server that went down — it carries an HttpServerStateChange with the reason
and the exception:
server.StateTransitioned += (_, change) =>
{
// Restarting is the stop half of a rebind — a start is coming, so leave the UI alone.
if (change is { State: HttpServerState.Stopped, Reason: not HttpServerStateReason.Requested and not HttpServerStateReason.Restarting })
logger.LogError(change.Exception, "The server went down: {Reason}", change.Reason);
};
HttpServerStateReason | Meaning |
|---|---|
Requested | The app called StartAsync/StopAsync. The only reason that is never a fault |
Restarting | Part of a RestartAsync — including the stop half, so a subscriber knows a start is coming |
NetworkChanged | A rebind driven by the addresses changing |
BindFailed | The bind was refused and the retries are spent; Exception says why |
ListenerFaulted | The listener stopped accepting while the server believed it was running |
Disposed | The server was disposed |
server.LastStateChange is the same record, for code that was not subscribed at the time — a crash
report, a diagnostics screen, a background task that woke up to find the server down.
Resilience is on by default; do not generate an opt-in for it. A transient accept failure is
retried with backoff; a listener that dies underneath a running server is logged at error, reported
as ListenerFaulted, and rebound; the start half of a restart or a network rebind retries
(StartRetryAttempts, default 5). A plain StartAsync is deliberately not retried — its caller
gets the exception. Tune with RecoverFromListenerFaults, StartRetry* and AcceptRetry* only when
asked.
Handlers on StateChanged, StateTransitioned and NetworkAddressesChanged are isolated: one that
throws is logged and the rest still run. Do not wrap them in defensive try/catch of your own.
[Route("/api/widgets")]
public class WidgetEndpoints(IWidgetStore store, ILogger<WidgetEndpoints> logger)
{
/// <summary>Fetches a widget.</summary> // becomes the OpenAPI summary
[Get("/{id:int}")]
[Produces(200, typeof(Widget))]
[Produces(404)]
public async Task<IActionResult> GetWidget(int id, CancellationToken ct)
=> await store.FindAsync(id, ct) is { } w ? new OkObjectResult(w) : new NotFoundResult();
[Get]
public async Task<IReadOnlyList<Widget>> List(int take = 10, string? search = null,
CancellationToken ct = default) => await store.ListAsync(take, search, ct);
[Post]
public async Task<IActionResult> Create(CreateWidget request, CancellationToken ct)
=> new CreatedResult($"/api/widgets/{(await store.AddAsync(request.Name, ct)).Id}");
}
app.MapWidgetEndpoints(); // one class
app.MapMyAppEndpoints(); // every [Route] class in the assembly (name = assembly name)
Rules to follow when generating endpoint classes:
public or internal, non-static, non-abstract, non-generic (SWS004).[Get], [Post], [Put], [Delete], [Patch], or [HttpMethod("VERB", "/t")].[NonEndpoint] excludes a public method.CancellationToken.In order: ambient types → route token → query → JSON body → container.
HttpContext, HttpRequest, HttpResponse, CancellationToken are handed over directly.IParsable binds from the route.IParsable (plus enums, nullables, and arrays of those) binds from the query.Overrides: [FromRoute], [FromQuery], [FromHeader], [FromBody], [FromServices], each with an
optional Name.
A default value makes a parameter optional. Bind failures are 400s naming the parameter and type, raised before the method is called.
Tier 3 (parameter binding); ReadJsonPatchAsync serves tier 1/2 raw handlers.
A JsonPatchDocument parameter binds from an application/json-patch+json body. Anything else is a
415 with Accept-Patch, and a malformed patch is a 400. No [JsonSerializable] entry is needed for it.
Apply it with the target type's JsonTypeInfo, never by reflection:
[Patch("/{id:int}")]
public IResult Patch(int id, JsonPatchDocument patch)
{
if (notes.Find(id) is not { } note) return Results.NotFound();
try
{
var patched = patch.ApplyTo(note, AppJson.Default.Note); // atomic: works on a copy
notes.Save(patched);
return Results.Ok(patched, AppJson.Default.Note);
}
catch (JsonPatchException ex) { return ex.ToResult(); } // 409 test/path, 422 bad result
}
var patch = await ctx.Request.ReadJsonPatchAsync(ctx.RequestAborted); (throws JsonPatchException)./title, not /Title); /- appends to an array.new JsonPatchDocument().Test("/version", 3).Replace("/title", "x").Add("/tags/-", "y").application/json: the server answers 415.builder.AddApiVersioning(o => // optional; defaults: query "api-version" + URL segment
{
o.AssumeDefaultVersionWhenUnspecified = true; // else no version = 400
o.ReportApiVersions = true; // api-supported-versions / api-deprecated-versions
o.ApiVersionReader = ApiVersionReader.Combine(new UrlSegmentApiVersionReader(), new HeaderApiVersionReader("api-version"));
o.Policies.Sunset(1.0).Effective(new DateTimeOffset(2027, 1, 1, 0, 0, 0, TimeSpan.Zero)).Link("https://example.com/retire");
});
[Route("/api/{version:apiVersion}/users")] // NOT "v{version:apiVersion}" — the constraint accepts "v2"
[ApiVersion("1.0")] [ApiVersion("2.0")] [ApiVersion("0.9", Deprecated = true)]
public class UserEndpoints
{
[Get] public UserV1[] List() => …; // implicit: 0.9, 1.0
[Get] [MapToApiVersion("2.0")] public UserV2[] ListV2() => …; // explicit mapping wins for 2.0
}
app.MapGet("/weather", V1).HasApiVersion(1.0); // raw: applies to the last-mapped route
app.MapGet("/weather", V2).HasApiVersion(2.0); // same template coexists by version
app.MapGroup("/api", api => api.HasApiVersion(2.0).MapGet("/x", X));
app.MapOpenApi("/openapi/{documentName}.json"); // /openapi/v1.json, /openapi/v2.json
UseApiVersioning() with DI: selection runs inside routing. Without a container, use app.UseApiVersioning(o => …).[ApiVersionNeutral] accepts any version or none.UnsupportedApiVersion, AmbiguousApiVersion, InvalidApiVersion, ApiVersionUnspecified). The handler reads its version with ctx.GetRequestedApiVersion().Tier 1 — one route, mapped next to MapOpenApi. Generate it whenever the user asks for Swagger UI,
an API explorer, API docs page, or Scalar.
app.MapOpenApi();
app.MapScalarApiReference(); // → /scalar
// per-version documents: Scalar shows a version picker, newest selected
app.MapOpenApi("/openapi/{documentName}.json");
app.MapScalarApiReference(configure: o =>
{
o.DocumentUrl = "/openapi/{documentName}.json";
o.DefaultHttpClient = ("csharp", "httpclient");
o.Theme = ScalarTheme.Purple;
});
dist/browser/standalone.js as a static file and set o.ScriptUrl = "/scalar.js".DocumentUrl must match the MapOpenApi pattern; it is written into the page as-is (root-relative by default).o.AdditionalConfiguration["key"] = JsonNode. Never put secrets there..RequireAuthorization() right after the call protects the page — protect the document route too.SelfOnlyContentSecurityPolicy) blocks the CDN and inline script.| Return | Response |
|---|---|
void / Task / ValueTask | Nothing — you wrote the response yourself |
IResult / IActionResult | Executed |
string | text/plain |
| Anything else | JSON from compile-time metadata |
All may be wrapped in Task<T>/ValueTask<T>. Anything else is SWS003.
[Get("/health/{component}")]
public class HealthEndpoint(IHealthChecks checks) : IHttpEndpoint
{
public async Task<IActionResult> HandleAsync(string component, CancellationToken ct)
=> await checks.RunAsync(component, ct) ? new OkResult() : new StatusCodeResult(503);
}
Exactly one public Handle/HandleAsync (SWS010) and a verb attribute on the class (SWS011).
public sealed class AdminModule : IEndpointModule
{
public void Map(IEndpointRouteBuilder endpoints)
=> endpoints.MapPost("/admin/reset", ctx => …).RequireAuthorization("admin");
}
app.MapModule(new AdminModule());
app.UnmapModule<AdminModule>();
Generated [Route] classes mount into a group too — the generator emits an IEndpointRouteBuilder
overload beside the HttpServer one, and [Authorize]/[AllowAnonymous] travel with the routes:
app.MapGroup("/v2", api => api.MapWidgetEndpoints()); // or api.MapMyAppEndpoints()
Always declare a JsonSerializerContext covering every type that crosses an endpoint boundary:
[JsonSourceGenerationOptions(PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase)]
[JsonSerializable(typeof(Widget))]
[JsonSerializable(typeof(IReadOnlyList<Widget>))]
[JsonSerializable(typeof(CreateWidget))]
public partial class AppJson : JsonSerializerContext;
JsonTypeInfoRegistry.Register(AppJson.Default);Results.Json(value, options) (the reflection overload) — it is
[RequiresUnreferencedCode]/[RequiresDynamicCode].Reading a body from a raw handler: await ctx.Request.ReadJsonAsync(AppJson.Default.NewNote)
(returns null for absent/malformed — return a 400, do not throw).
Results.X() and the MVC-shaped types are the same objects: Results.NotFound() ≡
new NotFoundResult(). Mix freely; prefer IActionResult types inside endpoint classes and
Results.* in raw handlers.
Common: Ok(), Ok(value), Created(location, value), NoContent(), BadRequest(message),
Unauthorized(), Forbidden(), NotFound(), Conflict(), StatusCode(n), Text, Bytes,
Stream, File, Redirect, Json, Negotiate, Problem, ValidationProblem,
ServerSentEvents.
JSON is the default and is what to generate unless the user asks for something else. When they do,
do not reach for XmlSerializer, DataContractSerializer, or MessagePack-CSharp's default
resolver — all of them build their mapping by reflecting over the type and break a trimmed or AOT
build. Register a formatter instead. Formats plug into content negotiation, which lives at tier 1
(configuration) and applies to all four tiers above it:
builder.AddContentNegotiation(o =>
{
o.NegotiateByDefault = true; // Results.Ok(value) honours Accept; off by default
o.AddXml(); // application/xml, text/xml
o.AddMessagePack(); // application/msgpack, application/x-msgpack
});
Both directions come from that one call, and no endpoint changes:
Results.Negotiate(value) always negotiates; Results.Ok(value) and
new OkObjectResult(value) negotiate only with NegotiateByDefault = true. Results.Json(...)
never does, because the name states a format.Content-Type. Every [FromBody] parameter, mediator contract and
EndpointBinder.TryReadBodyAsync<T> call accepts the registered formats immediately.Both XML and MessagePack read the app's existing JsonSerializerContext metadata, so the DTOs need
no format-specific attributes — do not generate [XmlRoot], [DataMember] or [MessagePackObject].
A type is covered because it is in the JsonTypeInfoRegistry, same as for JSON.
Protobuf needs a schema, so its codecs are supplied, not discovered — one line per message type using
the pair protoc already generated:
o.AddProtobuf(p => p
.Add<Reading>(m => m.ToByteArray(), Reading.Parser.ParseFrom)
);
BinaryCodecRegistry is not protobuf-specific; o.AddBinaryFormat("application/cbor", codecs)
registers any binary encoding the same way. This is also the escape hatch for MessagePack-CSharp's
native codec when the built-in transcoding formatter's JSON type system is not enough.
Reading a body by hand, with the three outcomes kept distinct:
var body = await EndpointBinder.TryReadBodyAsync<Probe>(ctx);
if (!body.Success)
{
// NoBody/Malformed → 400, UnsupportedMediaType → 415
await EndpointBinder.BodyReadFailedAsync(ctx, "probe", body.Status, nameof(Probe));
return;
}
Gotchas to respect when generating:
byte[] goes over MessagePack as base64, and a decimal beyond double precision loses digits —
both because the shared metadata is JSON's.<postalCode>01234</postalCode> into a string member stays
"01234". Do not add a converter to work around a problem that is not there.<tags><item>a</item></tags>), not
repeated siblings.IOutputFormatter for a binary format should return Charset => null.app.Use(async (ctx, next) => { /* before */ await next(ctx); /* after */ });
public sealed class ApiKeyMiddleware(IKeyStore keys) : IHttpMiddleware
{
public async ValueTask InvokeAsync(HttpContext context, RequestDelegate next) { … }
}
builder.Services.AddSingleton<ApiKeyMiddleware>();
app.Use<ApiKeyMiddleware>(); // resolved per request from the request scope
UseAfterRouting(...) runs after endpoint selection, so ctx.Endpoint (and its metadata) is
populated.
app.UseHostFiltering(); // first: a request to the wrong name learns nothing
app.UseTelemetry(); // outermost: everything below it is time the client waited
app.UseW3CLogging(); // same reason — the line should describe the whole exchange
app.UseCors(); // preflights carry no credentials
app.UseRateLimiter(); // before routing: a throttled request should cost nothing
app.UseIpFilter();
app.UseSecurityHeaders(); // applied as the response starts, so it covers static files and errors
app.UseContentDigest(); // before (de)compression: the digest covers the bytes on the wire
app.UseResponseCompression();
app.UseRequestDecompression(); // before anything that reads a body
app.UseWebhookVerification(); // after routing; only for [RequireWebhookSignature]
app.UseAuthentication(); // before routing
app.UseRequestLocalization(); // after auth (a provider may read a claim), before anything that renders text
app.UseAuthorization(); // after routing (registers itself as after-routing)
app.UseIdempotency(); // after authorization: the caller is part of the key
app.UseAntiforgery(); // after authentication — checks a request that would have succeeded
app.UseRequestTimeouts(); // after routing: the timeout is a property of the endpoint
app.UseOutputCache(); // after routing, so a hit still pays for auth above it
app.UseSessions();
app.UseStaticFiles("./wwwroot");
UseSecurityHeaders, UseRequestDecompression, UseTelemetry and UseRateLimiter are ordinary
middleware; UseAuthorization, UseAntiforgery, UseRequestTimeouts and UseOutputCache register
themselves after routing, because each reads endpoint metadata.
Both directions have a seam; use them rather than trying to read a body twice off the wire.
// inbound: read once, hand the handler a rewound copy (also drops any BodyReader handed out)
var buffered = new MemoryStream();
await ctx.Request.Body.CopyToAsync(buffered, ctx.RequestAborted);
buffered.Position = 0;
ctx.Request.Body = buffered;
// outbound: wrap the control the response is bound to, then bind the wrapper
var tee = new TeeBodyControl(ctx.Response.BodyControl, capture); // : IResponseBodyControl
ctx.Response.Bind(tee);
try { await next(ctx); }
finally { await tee.FlushAsync(); } // see gotcha below
A wrapper must forward StartAsync/CompleteAsync to the control it wrapped, and should build its
Writer over its own Stream (not over inner.Writer) so both write paths meet in one place. This
is the same seam UseResponseCompression() inserts itself through. On a device, decide from the
content type whether a body is worth keeping at all and cap what you store.
Critical gotchas:
PipeWriter never reach the wire.RestartAsync does not recompose it. Routes can change at any time.ctx.Response.OnStarting(...), not code after await next(ctx).HttpContext is pooled — never capture it past the end of the handler.[Authorize]/RequireAuthorization (after routing)
does not protect them — and neither does SetFallbackPolicy, which covers routed endpoints only
(not OnRequest, static/embedded files or the 404 for an unknown path). Put an authentication check
middleware in front of UseStaticFiles / UseEmbeddedFiles when the served content is not public.builder.AddAuthentication()
.AddJwtBearer(o => { o.Issuer = "app"; o.Audience = "app"; o.SigningKey = key; });
// or .AddBasic(o => o.AddUser("ada", pw, "admin")) / .AddBasic<UserStore>()
// or .AddApiKey(o => o.AddKey(key, "ci", "deploy"))
// or .AddCookie(o => { o.Protector = new TicketProtector(k); o.LoginPath = "/login"; })
builder.AddAuthorization(o =>
{
o.AddPolicy("admin", p => p.RequireRole("admin"));
// o.SetFallbackPolicy(p => p.RequireAuthenticatedUser()); // deny by default
});
[Authorize] / [AllowAnonymous] on endpoint classes and methods; RequireAuthorization(...) /
AllowAnonymous() on raw routes. Chained onto the server they apply to every route the preceding
call mapped — one for MapGet, all of them for MapGroup, MapModule(s) and a generated
Map{Class}()/Map{Assembly}Endpoints(). So protect a whole group by chaining after it:
app.MapGroup("/admin", g => { … }).RequireAuthorization("admin"). Same for every server-level
convention (RequireRateLimiting, RequireIpFilter, RequireCors, ValidateAntiforgery,
WithRequestTimeout, CacheOutput, …). A custom extension that maps several routes and returns
HttpServer must wrap them in using (server.Router.BeginRegistration()) { … }. A raw route may
instead take the attribute instances as metadata — app.Map("GET", "/x", h, new AuthorizeAttribute("admin"))
— with the same effect.[Authorize(Roles = "admin")] +
method [Authorize(Roles = "viewer")] needs both roles; Roles = "admin,owner" needs either. For
"either role" on one endpoint use one attribute with a comma list.SignInAsync(user, persistent: false) stays a browser-session cookie across sliding
renewals. Use o.OnValidatePrincipal = ctx => { … ctx.ReplacePrincipal(fresh) / ctx.RejectPrincipal() / ctx.ShouldRenew = true; } to refresh claims (a renewal otherwise copies the old ones). One
TicketProtector may serve both AddCookie and AddSessions — tickets are purpose-bound. When
protecting tickets yourself, always pass a purpose (protector.Protect(ticket, "my-feature") /
Unprotect(value, "my-feature")) so they can't be replayed as a sign-in or session cookie.Add… registration composes. AddAuthorization, AddCors, AddRateLimiter and the rest can
be called more than once; each call's configuration applies, in order. When writing an Add… for a
package built on this library, register options with OptionsRegistration.Configure(builder.Services, configure) — never TryAddSingleton(_ => { var o = new T(); configure(o); return o; }), which keeps
the first call and silently drops the rest.[Authorize] adds to the class's; [AllowAnonymous] always wins.Issuer and Audience (or validation of either turned off deliberately) —
otherwise the options throw the first time they are built. Generate all three.JwtSigningKey.FromSecret/FromRsa/FromEcdsa; AddJwtBearer also registers a
JwtTokenGenerator — inject it in a login endpoint so issuing and validating cannot drift.
Never generate a key at startup in production code (it invalidates every issued token on restart).CORS / rate limiting / IP filtering all follow the same shape — inline policy, or named policies plus
RequireX("name") / DisableX() on routes and [EnableCors], [EnableRateLimiting],
[RequireIpFilter] (+ [DisableCors], [DisableRateLimiting], [AllowAnyIp]) on endpoints.
RateLimitPartitioners.ByIpAddress counts IPv6 per /64; ByIpAddressAndPath keys on the matched
route template (or the router-normalised path), so respelling a path does not buy a new allowance.
Problem details (AddProblemDetails): a 4xx detail carries the exception message only for
BadHttpRequestException, JsonPatchException and types mapped with
o.MapException<MyValidationException>(422, includeMessage: true) (or with IncludeExceptionDetails).
To tell a client what was wrong, throw one of those or return Results.Problem(…) — not an
ArgumentException.
Tier 2 (middleware).
builder.AddHostFiltering(o => o.AllowedHosts.Add("*.local")); // add names; loopback, IP literals, tunnels already pass
app.UseHostFiltering(); // FIRST in the pipeline
builder.AddRequestLocalization(o => o.SetDefaultCulture("en")
.AddSupportedCultures("en", "fr", "de").AddSupportedUICultures("en", "fr", "de"));
app.UseRequestLocalization(); // or just app.UseRequestLocalization("en", "fr", "de")
var culture = ctx.GetRequestCulture(); // CultureInfo.Current(UI)Culture are already set for the request
UseHostFiltering() for any server bound to 0.0.0.0 / IPAddress.Any that a browser can reach.AllowedHosts is not allow-all (unlike ASP.NET); "*" is. Do not add IP addresses or localhost, they already pass.myphone.local needs *.local listed. Tunnel agents (cloudflared/ngrok/tailscale) need o.AllowPublicUrl(() => agent.PublicUrl); ITunnelProvider connections pass by default.UseForwardedHeaders, both Host and X-Forwarded-Host must pass.culture/ui-culture), then cookie (CookieRequestCultureProvider.DefaultCookieName, write it with MakeCookieValue), then Accept-Language. AddSupportedCultures replaces the list.InvariantGlobalization (AOT/mobile) every request is invariant and nothing throws; tell the user to set <PredefinedCulturesOnly>false</PredefinedCulturesOnly> if they localize text with .resx.Tier 1 (routing) for MapWebhook, the default. The metadata form is tier 2 (after-routing middleware) plus tier 3 (attribute and a WebhookContext parameter).
using Shiny.Net.HttpServer.Webhooks;
// Default: verification is part of the route. No registration, no middleware.
app.MapWebhook("/hooks/github", WebhookSignature.GitHub(secret), async (WebhookContext ctx) =>
{
if (ctx.EventType == "push") await Handle(ctx.ReadFromJson(AppJson.Default.PushEvent)!);
}); // writes nothing → 200
app.MapWebhook("/hooks/stripe", WebhookSignature.Stripe(stripeSecret),
async (WebhookContext ctx) => { ...; return Results.Ok(); },
o => o.SuppressDuplicates()); // per-route options
GitHub, Stripe, StandardWebhooks (Svix/Clerk/Resend, whsec_ secrets), Slack, Hmac(o => { o.HeaderName; o.Algorithm; o.Encoding; o.Prefix; o.Secrets; o.DeliveryIdHeader; }). Every factory takes several secrets for rotation. The timestamp tolerance is 5 minutes (Stripe(TimeSpan.FromMinutes(10), secret)).ctx.Body, ReadAsString() or ReadFromJson(JsonTypeInfo). Never parse and re-serialize before verification. ctx.Request.Body is a rewound copy, so [FromBody] and form readers still work.MaxBodySize (5 MiB) is a 413.SuppressDuplicates() answers a seen delivery id with 200 without running the handler, and forgets the id if the handler throws or returns 5xx. Slack has no id.builder.AddWebhooks(o => o.AddVerifier("github", WebhookSignature.GitHub(secret))), then app.MapWebhook("/hooks/github", "github", handler).app.UseWebhookVerification() plus .RequireWebhookSignature("github"), or [RequireWebhookSignature("github")] with a WebhookContext parameter. Metadata does nothing without UseWebhookVerification(). Prefer MapWebhook for new routes.WebhookSigner.StandardWebhooks(secret).Apply(request, messageId, bodyBytes). WebhookSigner.GenerateSecret() makes a whsec_ secret.Tier 2 (middleware), with per-endpoint metadata and tier-3 attributes.
builder.AddIdempotency(); // o.Expiration, o.ShouldStoreStatusCode, o.MaxResponseBodyBytes
builder.AddContentDigest(); // o.ResponseDigest = DigestEmission.Always
app.UseContentDigest(); // BEFORE UseRequestDecompression/UseResponseCompression
app.UseAuthentication();
app.UseAuthorization();
app.UseIdempotency(); // after routing, AFTER UseAuthorization (the caller is part of the key)
app.MapPost("/payments", Charge).RequireIdempotencyKey(); // no key → 400
app.MapPost("/notes", Save).WithIdempotency(); // honoured if sent
app.MapPost("/firmware", Upload).RequireContentDigest(); // body must carry a verifying Content-Digest
app.MapGet("/snapshot", Get).WithContentDigest(); // response always digested
var payments = group.RequireIdempotencyKey(); // group convention: use the RETURNED builder
Tier 3: [Idempotent] (Required = true by default, ExpirationSeconds), [DisableIdempotency],
[ContentDigest(RequireRequestDigest = true)], [DisableContentDigest].
Idempotent-Replayed: true. Same key with a different body → 422, still running → 409, 5xx/exceptions are not stored. Keep idempotent responses small and non-streamed or they cannot be replayed.IIdempotencyStore (shared store behind a load balancer) must make TryReserveAsync atomic, and should implement the owner-aware overloads (TryReserveAsync(key, fingerprint, owner, lease, ct), CompleteAsync(key, owner, record, ct) returning false when the claim moved on, ReleaseAsync(key, owner, ct)). Store the owner with the claim and act only when it matches, so a request that overran InFlightTimeout cannot release or overwrite the retry's claim.Want-Content-Digest), as a header up to 64KB and as a trailer beyond that. HTTP/1.1 with a declared Content-Length cannot trail. Use DigestFields.Compute(...) to set one by hand.Shiny.Net.HttpServer.OAuthLoopback receives the redirect of an OAuth 2.0 / OpenID Connect sign-in
done in the system browser (RFC 8252 §7.3). It is a one-shot server on an ephemeral 127.0.0.1 port.
It is not an OAuth client. The authorization URL, PKCE and the token exchange belong to the
user's OAuth library or their own HttpClient code. Do not generate PKCE or token code inside it.
Tier: internally it is a tier-0 OnRequest server that the class owns. The app maps nothing and
configures no pipeline. It is a standalone class, not something added to the app's own server.
using Shiny.Net.HttpServer.OAuthLoopback;
// One call: start, build the URL, open the browser, wait, stop.
var result = await LoopbackCallbackListener.AuthorizeAsync((redirectUri, state) => new Uri(
$"{authorizeEndpoint}?response_type=code&client_id={clientId}" +
$"&redirect_uri={Uri.EscapeDataString(redirectUri.AbsoluteUri)}&state={Uri.EscapeDataString(state)}" +
$"&code_challenge={challenge}&code_challenge_method=S256"));
if (result.IsError) { /* result.Error, result.ErrorDescription */ }
else await ExchangeAsync(result.Code!, verifier);
// Or step by step, when the redirect URI is needed first:
await using var callback = await LoopbackCallbackListener.StartAsync(new() { Timeout = TimeSpan.FromMinutes(5) });
SystemBrowser.Open(Build(callback.RedirectUri, callback.State));
var r = await callback.WaitAsync(ct);
Rules when generating it:
callback.State (or the state passed to the builder) in the authorization request.
A callback with a missing or wrong state gets a 400 and is ignored. It does not complete the wait.ValidateState = false (OidcClient owns state),
set OidcClientOptions.RedirectUri = listener.RedirectUri.AbsoluteUri, and write an IBrowser that
calls SystemBrowser.Open(options.StartUrl) and returns
new BrowserResult { ResultType = Success, Response = result.CallbackUri.AbsoluteUri }. Map a
TimeoutException to BrowserResultType.Timeout.AcquireTokenInteractive.Address (127.0.0.1) and Port = 0. Anything that is not loopback throws.
Set a fixed Port only if the provider demands an exact redirect URI.access_denied) come back as result.IsError, not as exceptions.
WaitAsync throws TimeoutException on Timeout (default 5 min) and OperationCanceledException
on cancellation.response_mode=form_post is accepted (AllowFormPost, on by default).RenderPage (HTML-encode anything taken from the result) or send
the browser elsewhere with RedirectTo.WebAuthenticator with a custom scheme there. SystemBrowser.Open throws
PlatformNotSupportedException on those platforms.app.UseStaticFiles("./wwwroot", o => o.FallbackFile = "index.html");
app.UseEmbeddedFiles(typeof(App).Assembly, "MyApp.wwwroot"); // packaged / MAUI
app.UseBlazorWebAssembly("./wwwroot"); // SPA + precompressed + cache policy
app.MapFileBrowser("/files", o => o.RootPath = FileSystem.AppDataDirectory).RequireAuthorization();
Four sources, all IStaticFileSource and all usable with any of the above:
PhysicalFileSource (a directory), EmbeddedFileSource (loose embedded resources),
ZipFileSource (a zip on disk or embedded in the assembly), CompositeFileSource (tried in order —
a directory in front of the packaged copy is the development arrangement).
// A packaged Blazor publish, zipped into the app - one embedded resource instead of thousands,
// and the paths survive instead of being flattened into the resource name.
app.UseBlazorWebAssembly(
new ZipFileSource(typeof(App).Assembly, "MyApp.wwwroot.zip")
{
PrecompressedEncodings = ["br", "gzip"] // a publish zips its .br/.gz sidecars too
}
);
new ZipFileSource("./content/site.zip"); // on disk
new ZipFileSource("./site.zip", "wwwroot"); // zipped with its parent folder
Prefer ZipFileSource over EmbeddedFileSource for a publish output: EmbeddedFileSource has
to un-mangle dotted resource names, which is ambiguous for site.min.css, and thousands of loose
resources inflate the assembly. A zip keeps real paths and stays compressed.
Unknown file extensions are not served by default. Add ContentTypeOverrides[".x"] rather than
turning on ServeUnknownFileTypes.
PhysicalFileSource and MapFileBrowser follow every link along a path and refuse any that
leads out of the root (reads and writes); links inside the root work. Do not add your own
symlink checks. Hidden-attribute entries are refused like dotfiles unless ServeHiddenFiles.
FileBrowserOptions.Filter receives the on-disk spelling of the path (case-insensitive file
systems included), so compare it ordinally against the real directory names.
Store large media in a zip uncompressed if it will be range-requested: stored entries seek, deflated ones inflate from the start.
MapFileBrowser("/", …) mounts the browser on the whole site — the shape a "serve this directory"
CLI wants. Literals still beat its catch-all, so routes mapped alongside it keep answering.
Uploads: await foreach (var part in ctx.Request.ReadMultipartAsync(ct)) and
part.SafeFileName() (never part.FileName — traversal). ReadFormAsync buffers; use it only for
small fields.
Body size: keep the server-wide Limits.MaxRequestBodySize small and give the upload route its own
limit. This is route metadata, so it belongs to tier 1 (route builder) and tier 3 (attributes):
app.MapPost("/images", Upload).WithRequestSizeLimit(500 * 1024 * 1024); // tier 1
app.MapPut("/backups/{name}", Restore).DisableRequestSizeLimit(); // no limit
[Post("/images")] [RequestSizeLimit(500 * 1024 * 1024)] // tier 3
public Task<IActionResult> Upload(HttpContext ctx) => ...;
Routing applies it before authorization and the handler run. Middleware ahead of routing can set
ctx.Request.MaxBodySize instead. It can't change once the body has started to be read
(IsMaxBodySizeReadOnly; setting it then throws). Over the limit is a 413 when the body is read,
never a truncated read. HTTP/1.1 sends 100 Continue only when the body is first read. The
per-route limit has no effect on HTTP/3, which is held to the server-wide value.
Downloads: FileDownloadResult.FromFile(...) gives ranges, ETags and conditional GETs.
MapFileBrowser is a JSON API you drive with curl or your own client. MapWebDav speaks the
protocol the operating system already has a client for, so the folder mounts as a drive with no
client code at all — and its browser GET is a working file manager. Reach for it whenever the user
says mount, Finder, Explorer, map a network drive, WebDAV, or wants a UI over a
directory; reach for the file browser when they want a JSON API to call from their own code.
Tier 1: a mounted module of raw routes, twenty-two of them, mapped in one call.
app.MapWebDav("/dav", o =>
{
o.RootPath = FileSystem.AppDataDirectory;
o.AllowWrite = true; // PUT, MKCOL, PROPPATCH, COPY, LOCK — off by default
o.AllowDelete = true; // DELETE, and the delete half of MOVE — off by default
})
.RequireAuthorization(); // or .RequireAuthorizationForChanges() to leave reads open
EnableLocking on. It is class 2, and Finder and the Windows redirector both mount a class 1
server read-only however much AllowWrite allows.AllowInfiniteDepth is off by default: PROPFIND with Depth: infinity answers 403 with
<DAV:propfind-finite-depth/>, which is the refusal RFC 4918 §9.1 defines. Turn it on only for a
small tree.PROPPATCH) are held in memory. Assign PropertyStore to keep them across
restarts.GET on a collection returns a file manager: listing with sizes and times,
breadcrumbs, drag-and-drop upload (PUT, folders walked and recreated), new folder (MKCOL),
rename (MOVE, Overwrite: F) and delete (DELETE), plus a download button per file. It offers
only what the options allow — read-only renders a listing with no buttons — and it is one
self-contained response with nothing fetched from outside. Entries and the link to the parent are
absolute, so the tree is walkable with or without a trailing slash on the mount URL.
DirectoryBrowsing = false turns the page off entirely (a browser GET then answers 405). This
is the same page shinyhttpserver serves — say "open the mount URL in a browser" when a user asks
for a UI over a directory; there is no other file-manager UI in this library.IWebDavFileSystem and set o.FileSystem instead of RootPath
— several roots side by side, platform-API folders, a photo library. Paths arrive relative,
/-separated, root "", already stripped of ../dotfiles/Filter misses. WebDavEntry.Name is
the URL segment and DisplayName what the client shows. Refuse with
throw new WebDavException(403); UnauthorizedAccessException→403,
FileNotFound/DirectoryNotFound→404, other IOException→409. Let the 413 the write stream throws
propagate. OpenReadAsync returning a seekable stream wins over the entry's length — buffer
anything transcoded on the fly. Do not copy the data into a temp directory to use RootPath.Reach for it when the user wants their app's events or contacts to show up in iOS or macOS Calendar or
Contacts, Thunderbird or DAVx⁵, or says CalDAV, CardDAV, subscribe, sync calendar. Tier 1: one
call mounts the whole protocol (package Shiny.Net.HttpServer.CalDav, built on the WebDAV package).
app.MapCalDav("/dav", o =>
{
o.CalendarStore = new FileCalendarStore(Path.Combine(FileSystem.AppDataDirectory, "calendars"));
o.AddressBookStore = new FileAddressBookStore(Path.Combine(FileSystem.AppDataDirectory, "contacts")); // either may be null
})
.RequireAuthorization(); // Basic auth; the user name IS the principal
/.well-known/caldav and /.well-known/carddav (HttpServer overload only; otherwise call app.MapCalDavWellKnown("/dav")). On iOS: Settings → Calendar → Accounts → Add CalDAV Account → device address plus the Basic credentials.User.Identity.Name; other principals' URLs get 403. DefaultPrincipal exists only for a single-user mount with no auth.ICalendarStore / IAddressBookStore — collections of text objects keyed by name, each with a quoted strong ETag that changes whenever the text does. PutObjectAsync gets a validated DavObjectWrite (with .Component already parsed); return the new ETag, or null if the text was not stored byte for byte. Refuse with throw new WebDavException(403). Set IsReadOnly on read-only calendars.ContentComponent.Parse(text), GetComponents("VEVENT"), GetProperty("SUMMARY")?.GetText() and ToString() (folds lines). It is not a full calendar library.A plain upload restarts from zero when the connection drops. MapTus speaks tus 1.0.0, so any tus
client (tus-js-client, Uppy, TusDotNetClient, TUSKit) resumes from the last byte the server kept.
Reach for it whenever the user says resumable, large upload from a phone, resume after
disconnect, tus, or chunked upload. Tier 1: seven raw routes mapped in one call (package
Shiny.Net.HttpServer.Tus).
app.MapTus("/files", o =>
{
o.Store = new DiskTusStore(Path.Combine(FileSystem.AppDataDirectory, "uploads")); // required
o.MaxSize = 4L * 1024 * 1024 * 1024; // Tus-Max-Size
o.Expiration = TimeSpan.FromDays(1); // sliding; unfinished uploads only
o.OnBeforeCreateAsync = ctx =>
{
if (!ctx.Metadata.ContainsKey("filename")) ctx.Reject(400, "filename required");
return ValueTask.CompletedTask;
};
o.OnUploadCompleteAsync = async ctx =>
{
await using var file = await ctx.OpenReadAsync(); // runs once, never for partials
};
o.OwnerSelector = ctx => ctx.User?.FindFirst("sub")?.Value; // only the creator may use an upload
})
.RequireAuthorization();
app.UseCors(p => p.WithOrigins("https://app.example.com").WithTusHeaders()); // browser clients
MapPost. Use this.MaxSize, or have
no body limit without it — not by Limits.MaxRequestBodySize. Do not tell the client to chunk
below 30 MB, and do not add WithRequestSizeLimit to make big PATCHes work. To bound one request
more tightly: MapTus(…).ForEach(r => r.WithRequestSizeLimit(n)) (a later route limit wins).RequireAuthorization(), generate OwnerSelector whenever uploads belong to users:
without it any authorised caller with an upload URL can HEAD/PATCH/DELETE it or concatenate it
into their own. Other caller rules go in AuthorizeUploadAsync (sees HttpContext, Upload,
Operation). Refusals answer 404. A custom store must persist TusCreateRequest.Owner and return
it as TusUpload.Owner.ctx.Metadata["filename"] is text, never a path to write to.ITusStore.AppendAsync must keep the bytes when the stream ends early and roll back
when the stream throws. Throw TusException(409) for a wrong offset and TusException(404) for a
missing upload. The endpoint already serialises requests per upload, so the store needs no lock.LockReleaseTimeout = TimeSpan.Zero answers 423 instead of taking over a stalled request.RemoveExpiredUploadsAsync() on the returned builder runs cleanup from a platform background job.MapNuGetFeed serves the NuGet V3 server API, so dotnet restore, dotnet nuget push,
dotnet package search, Visual Studio and Rider work against it as a plain package source. Reach for it
when the user says NuGet server, private feed, package feed, or host our packages. Tier 1:
nine raw routes mapped in one call (package Shiny.Net.HttpServer.NuGet; versions via the official
NuGet.Versioning).
app.MapNuGetFeed("/nuget", o =>
{
o.Store = new DiskNuGetPackageStore(Path.Combine(dataDir, "packages")); // required
o.ApiKeys.Add(config["NuGet:ApiKey"]!); // X-NuGet-ApiKey, compared in constant time
o.DeleteBehavior = NuGetDeleteBehavior.Unlist; // default; .Delete removes files
o.OnBeforePushAsync = ctx =>
{
if (!ctx.Package.Id.StartsWith("Contoso.")) ctx.Reject(403, "Only Contoso.* packages.");
return ValueTask.CompletedTask;
};
})
.ForReads(r => r.RequireAuthorization()); // optional: a private feed (Basic creds in nuget.config)
// client: dotnet nuget add source http://host:5000/nuget/v3/index.json -n team --allow-insecure-connections
// dotnet nuget push My.Pkg.1.0.0.nupkg -s team -k <key>
{prefix}/v3/index.json. Give users that URL, not the prefix.ApiKeys and no ValidateApiKeyAsync throws when mapped. Use
ReadOnly = true for a feed nobody pushes to, or RequireApiKey = false only when ForWrites(...)
adds authorization.allowInsecureConnections="true" is set on the source. On a
public host, serve it with TLS or ACME.MaxPackageSize (250 MB) and Limits.MaxRequestBodySize (30 MB) both apply. For big
packages raise it on the write routes only: .ForWrites(r => r.WithRequestSizeLimit(250 * 1024 * 1024)).PublicBaseUrl. Every link in the protocol is absolute.DiskNuGetPackageStore uses NuGet's own folder-feed layout ({id}/{version}/{id}.{version}.nupkg),
so the directory is also a local source and can be seeded by copying files in (call Reload())..snupkg symbol packages, the catalog, download counts, the V2/OData API. Do not
promise them.MapNpmRegistry serves the npm registry API, so npm install, publish, view, search,
dist-tag, deprecate, unpublish, owner, whoami and login work. Reach for it when the user
says npm registry, private npm, Verdaccio replacement, or publish our JS packages. Tier 1:
one catch-all route per HTTP method in one call, dispatched internally (npm's URLs do not fit
templates).
app.MapNpmRegistry("/npm", o =>
{
o.Store = new DiskNpmPackageStore(Path.Combine(dataDir, "npm")); // required
o.Tokens[config["Npm:Token"]!] = "ci"; // token → user name (Bearer, constant-time)
o.Upstream = new Uri("https://registry.npmjs.org"); // optional: one URL for private + public
});
// .npmrc
// @acme:registry=http://host:5000/npm/ (or registry=… when Upstream is set)
// //host:5000/npm/:_authToken=${NPM_TOKEN}
{server}{prefix}/ (with trailing slash). The .npmrc auth line must match it:
//host:port/prefix/:_authToken=….Tokens and no ValidateTokenAsync throws when mapped. Use
ReadOnly = true for none. RequireTokenForReads makes installs authenticated too.npm owner add adds more; tokens map to user names for this.npm login works only with LoginAsync (user/password → token). Otherwise hand tokens out.Limits.MaxRequestBodySize
(30 MB) applies before MaxTarballSize (100 MB). Raise it on the write routes only:
.ForWrites(r => r.WithRequestSizeLimit(150 * 1024 * 1024)).{"error": "..."} bodies, not problem details. Do not wrap them.MapFileSync is a content-addressed chunk store plus a change feed. FileSyncClient (in
Shiny.Net.HttpServer.FileSync.Client, which has no dependency on the server package) keeps a
local folder in sync with it both ways. Files are split into content-defined chunks (FastCDC,
64 KB–1 MB), so an edit moves only the changed chunks, and downloads reuse chunks already on the
device. Reach for it when the user says sync a folder, Dropbox/OneDrive-like, delta/binary
patch upload, only send what changed, or offline files that sync. Server: tier 1, ten raw routes
plus a tus mount in one call. Client: not a server tier; it is a separate client library.
// server
app.MapFileSync("/sync", o => o.Store = new DiskFileSyncStore(Path.Combine(dataDir, "sync")))
.RequireAuthorization();
// per-user trees: o.StoreSelector = ctx => stores.GetOrAdd(ctx.User!.Identity!.Name!, n => new DiskFileSyncStore(...));
// cleanup from a timer: await mount.CollectGarbageAsync();
// app (MAUI)
builder.Services.AddFileSyncClient(o =>
{
o.ServerUri = new("https://files.example.com/sync"); // server URL + the MapFileSync prefix
o.LocalPath = Path.Combine(FileSystem.AppDataDirectory, "Documents");
o.Headers["Authorization"] = "Bearer " + token; // or o.HeadersProvider for refreshing tokens
});
var result = await sync.SyncAsync(); // one pass: result.BytesTransferred vs result.BytesReused
await sync.RunAsync(ct); // continuous: long-poll + FileSystemWatcher + ScanInterval
MapPost. Use this.AddFileSyncClient registers Shiny.Net.Http transfers itself if the app has not. An app with its
own transfer delegate must call AddHttpTransfers<TDelegate>() before AddFileSyncClient.NSURLSession, Android foreground service). Pack uploads go over tus
(UseTusUploads, default on; needs the server's EnableTusUploads, also on by default).
Downloads resume with Range. With no transfer manager registered (plain .NET), it falls back to
HttpClient. Outside a Shiny host, call HttpClientHttpTransferManager.Start().name (conflicted copy {DeviceName} {date}).ext. Handle Activity events with
FileSyncActivityKind.Conflict to tell the user.AverageChunkSize must be a power of two.PUT chunks/pack (non-tus) are bounded by Limits.MaxRequestBodySize
(30 MB) unless the route raises it with WithRequestSizeLimit. Client packs default to 8 MB.// WebSockets — the handler owns the socket; loop inside it
app.MapGet("/ws", async ctx =>
{
if (!ctx.Request.IsWebSocketRequest()) { ctx.Response.StatusCode = 400; return; }
await using var socket = await ctx.AcceptWebSocketAsync();
while (await socket.ReceiveAsync(ctx.RequestAborted) is { } msg)
await socket.SendAsync(msg.Text, ctx.RequestAborted);
});
// SSE
app.MapGet("/events", ctx => ctx.SendEventsAsync(async stream =>
{
while (!stream.Aborted.IsCancellationRequested)
{
await stream.SendAsync($"tick {DateTime.UtcNow:O}");
await Task.Delay(1000, stream.Aborted);
}
}));
A handler owns one socket and cannot reach the others. IWebSocketRegistry is the list, the groups
and the dead-socket cleanup, so an app does not hand-roll all three.
builder.AddWebSocketRegistry();
app.MapGet("/ws", async ctx =>
{
await using var tracked = await ctx.AcceptTrackedWebSocketAsync(cancellationToken: ctx.RequestAborted);
tracked.JoinGroup("kitchen");
while (await tracked.ReceiveAsync(ctx.RequestAborted) is { } message)
await registry.SendToGroupAsync("kitchen", message.Text, ctx.RequestAborted);
});
await registry.BroadcastAsync("the door unlocked"); // returns how many were reached
await registry.SendToUserAsync("ada", "your build finished");
TrackedWebSocket untracks and closes it, so the registry needs no cleanup pass.EnablePerMessageDeflate = false turns it off; CompressionThreshold (256 bytes) is the floor
below which deflate makes a message bigger.AllowedOrigins. WebSockets ignore the same-origin policy and
browsers send cookies on the handshake, so set new WebSocketAcceptOptions { AllowedOrigins = { "https://app.example.com" } }; a disallowed Origin gets 403.ReceiveAsync throws WebSocketProtocolException — catch it in
the receive loop.KeepAliveInterval = null to switch it off (do that in tests that assert on frames).Shiny.Net.HttpServer.Switchboard (server) and Shiny.Net.HttpServer.Switchboard.Client (.NET
client). Reach for it whenever the user asks for SignalR, a hub, IHubContext, or "push to clients
and let them call back" — ASP.NET Core SignalR cannot run in MAUI, and hand-rolling SSE plus
IWebSocketRegistry rebuilds half of this badly. Use raw SSE only for one-way feeds; use raw
WebSockets only for high-rate binary traffic.
Tiers: a switchboard class is tier 3 (source-generated dispatch, like [Route] classes);
MapSwitchboard is tier 1; ISwitchboardFilter is tier 2 (switchboard middleware).
Vocabulary: switchboard = hub, line = connection (Context.LineId, Clients.Line(id)),
operator = IHubContext (IOperator<TBoard> / IOperator<TBoard, TClient>), hang up = abort.
Not wire-compatible with SignalR — never generate a SignalR client against it, or vice versa.
using Shiny.Net.HttpServer.Switchboard;
public interface IChatClient // what the server calls on clients
{
Task MessageReceived(ChatMessage message);
Task<bool> ConfirmDelete(string room); // Task<T> = a client result (server waits)
}
public class ChatBoard(IRoomStore rooms) : Switchboard<IChatClient> // or : Switchboard (untyped)
{
public override Task OnConnectedAsync() => Clients.Caller.MessageReceived(…);
public override Task OnDisconnectedAsync(DisconnectContext d) => …; // d.Reason: ClientClosed/Dropped/HungUp/ServerShutdown/Faulted
public Task JoinRoom(string room) => Groups.AddToGroupAsync(Context.LineId, room);
public Task Send(string room, string text) => Clients.Group(room).MessageReceived(new(room, Context.UserIdentifier!, text));
public IReadOnlyList<RoomInfo> Rooms() => rooms.List(); // returns a value
public async IAsyncEnumerable<ChatMessage> History(string room, [EnumeratorCancellation] CancellationToken ct) { … } // streams
[Authorize(Roles = "admin")]
public async Task DeleteRoom(string room)
{
if (!await Clients.Caller.ConfirmDelete(room)) throw new SwitchboardException("Cancelled"); // message reaches the client
}
}
builder.AddSwitchboard(o => o.ResumeWindow = TimeSpan.FromSeconds(30));
app.MapSwitchboard<ChatBoard>("/chat").RequireAuthorization();
// outside a switchboard
public class Shipped(IOperator<ChatBoard, IChatClient> board)
{
public Task Handle(Order o) => board.Clients.User(o.CustomerId).MessageReceived(…);
}
await board.Lines.HangUpUserAsync("mallory", "Banned"); // token dies; client does not reconnect
Client:
using Shiny.Net.HttpServer.Switchboard.Client;
var line = new SwitchboardLineBuilder()
.WithUrl("https://host/chat", o => o.AccessTokenProvider = () => GetTokenAsync())
.WithJson(ChatJson.Default) // the same JsonSerializerContext as the server
.WithAutomaticReconnect()
.Build();
line.On<ChatMessage>("MessageReceived", m => …);
line.On<string, bool>("ConfirmDelete", room => AskAsync(room)); // answers a client result
line.Reconnected += (_, e) => { if (e.NewLine) /* rejoin groups */; };
line.Closed += (_, e) => …; // e.Reason: ClientClosed/HungUp/ServerClosed/Dropped/RetriesExhausted/Faulted
await line.StartAsync();
var n = await line.InvokeAsync<int>("Count");
await foreach (var m in line.StreamAsync<ChatMessage>("History", "general")) { }
// Typed client from shared contract interfaces:
[SwitchboardClient<IChatBoard, IChatClient>] public partial class ChatBoardClient;
// → calls as methods, IChatClient methods as events, Task<T> ones as a handler property, Register(IChatClient)
Rules that matter when generating code:
[JsonSerializable] in a context;
pass it to WithJson on the client). Primitives are built in. Never suggest reflection.Context.Items (safe without locks while
MaximumParallelInvocationsPerLine is 1, the default) or a service, never in fields.[LineMethod("x")], hide with
[NotALineMethod]. Parameters: JSON by position, plus CancellationToken, CallerContext,
[FromServices] T. No IAsyncEnumerable<T> parameters (client streaming is not supported).Clients.Caller / Clients.Line(id) (typed: Task<T> on a group throws
NotSupportedException). They work from inside a switchboard method — no parallelism setting needed.ResumeWindow with its id, groups, Items and missed messages;
after that the client gets a new line (Reconnected.NewLine) and must rejoin groups.[Authorize] on methods is enforced by the switchboard itself; [AllowAnonymous] on a method cannot
lift a class requirement (SWB007).[ExportContract(Namespace = "X.Contracts")] on the board
plus <SwitchboardContractsOutputPath> in the server csproj writes one self-contained file.Shiny.Net.HttpServer.* namespace must qualify the Switchboard base
class (CS0118). A file importing both server and client namespaces must alias SwitchboardException.builder.AddRequestTimeouts(o =>
{
o.DefaultPolicy = new RequestTimeoutPolicy(TimeSpan.FromSeconds(30));
o.AddPolicy("reports", TimeSpan.FromMinutes(2));
});
builder.AddOutputCache(o => o.AddPolicy("lists", new OutputCachePolicy(TimeSpan.FromSeconds(30))
{
VaryByHeaders = ["Accept"]
}));
app.UseRequestTimeouts();
app.UseOutputCache();
app.MapGet("/report", Handler).WithRequestTimeout(TimeSpan.FromSeconds(10));
app.MapGet("/stream", Handler).DisableRequestTimeout(); // SSE, downloads, upgrades
app.MapGet("/list", Handler).CacheOutput(TimeSpan.FromSeconds(10));
app.MapGet("/live", Handler).NoOutputCache();
Tier 3 spells the same things as attributes: [RequestTimeout(2000)], [RequestTimeout("reports")],
[DisableRequestTimeout], [OutputCache(Seconds = 30)], [OutputCache("lists")], [NoOutputCache].
ctx.RequestAborted. A handler that ignores its
token still runs to completion — it just does so after the client has been answered. Pass the token
into the slow thing.Set-Cookie, and skips
authenticated callers unless the policy says otherwise (AllowAuthenticated) — a cache keyed on
the URL cannot tell two users apart. A streamed response (one that called StartAsync) is passed
through untouched.Conditional requests for handlers that are not serving a file:
app.MapGet("/items/{id}", async ctx =>
{
var item = await store.GetAsync(ctx.Request.RouteValues["id"]!, ctx.RequestAborted);
// Writes ETag/Last-Modified, answers 304 or 412 itself, and returns true when it did.
if (await ctx.TryCompleteConditionalAsync(EntityTag.FromContent(item.Version), item.Updated))
return;
await ctx.Response.WriteJsonAsync(item, AppJson.Default.Item, ctx.RequestAborted);
});
CheckPreconditions is the same evaluation without writing anything; SetETag, SetLastModified,
SetCacheControl and SetNoStore are the response-side helpers. A 304 saves the serialisation as
well as the bytes.
builder.AddHealthChecks()
.AddServerCheck() // is the listener actually up
.AddCheck("database", async ct => await db.CanConnectAsync(ct)
? HealthCheckResult.Healthy()
: HealthCheckResult.Unhealthy("no connection"), "ready")
.AddCheck<SyncHealthCheck>("sync", tags: ["ready"]); // resolved from the container
app.MapHealthChecks(); // /health
app.MapHealthChecks("/health/live", "live"); // filtered by tag
app.MapHealthChecks("/health/ready", "ready");
Utf8JsonWriter, so no JsonSerializerContext is needed for it.IncludeDetails = false for an endpoint reachable through a tunnel — the entry list is a free
inventory of the app's dependencies.app.UseTelemetry(); // one Activity + one duration measurement per request
builder.Services.AddOpenTelemetry()
.WithTracing(t => t.AddSource(HttpServerTelemetry.ActivitySourceName))
.WithMetrics(m => m.AddMeter(HttpServerTelemetry.MeterName));
http.server.request.duration,
http.server.active_requests, http.server.active_connections — so an ASP.NET dashboard reads
this server without being told about it.GET /users/{id} once routing has chosen an endpoint, never the raw path.ContinueIncomingTrace is on by default and should be turned off for a tunnelled server: a
caller who picks the trace id can graft spans onto someone else's trace.RecordUrl is off: paths carry identifiers and query strings carry secrets.app.UseW3CLogging(o =>
{
o.LogDirectory = FileSystem.AppDataDirectory; // must be writable — the default is not, in MAUI
o.Fields = W3CLoggingFields.Default | W3CLoggingFields.Route;
o.ShouldLog = ctx => ctx.Request.Path != "/health";
});
One line per request in the format IIS writes, which every log analyser reads. Suggest it when the ask is "a log I can look at later" and there is nowhere to ship telemetry to — a device, an appliance, a customer's machine.
#Remark: N line(s) dropped into the file — a slow disk must not become a slow server.FileSizeLimit and are pruned to RetainedFileCountLimit. There is no unbounded
setting on purpose.W3CLoggingFields.Default excludes Cookie. Do not add it without a reason — a log file gets
copied around and that header is a session token.x-route and x-connection-id are extensions (the x- prefix is the format's own rule) and are
what make the log useful for finding a slow endpoint rather than a slow URL.IW3CLogWriter is the seam for sending lines somewhere other than a file.builder.AddAntiforgery();
app.UseAntiforgery();
app.UseSecurityHeaders(o => o.ContentSecurityPolicy = SecurityHeaderOptions.SelfOnlyContentSecurityPolicy);
app.UseHttpsRedirection(); // port taken from the first TLS endpoint
// issue the pair to the page, then echo the request token back in X-CSRF-TOKEN
var tokens = ctx.GetRequiredService<IAntiforgery>().GetTokens(ctx);
[ValidateAntiforgery] / .ValidateAntiforgery() forces the check anyway;
[DisableAntiforgery] / .DisableAntiforgery() exempts a webhook.ValidateToken(ctx, form["__RequestVerificationToken"]).Lifetime (8h) from issue and are bound to the signed-in user — fetch a fresh token
after sign-in. Over HTTPS the cookie is Secure and named __Host-shiny.antiforgery
(SecurePolicy = SameAsRequest, UseHostPrefix = true); client script must echo
tokens.RequestToken, never read the cookie by name.Host configuration (builder setup plus TLS options), beside tier 2 — the only thing it adds to the
pipeline is the HTTP-01 challenge middleware. Package Shiny.Net.HttpServer.Acme.
builder.Options.Listen(IPAddress.Any, 80); // HTTP-01 is ALWAYS port 80
builder.Options.Listen(IPAddress.Any, 443).Https = new HttpsOptions(); // no certificate => ACME's
builder.AddAcme(o =>
{
o.Domains = ["example.com", "www.example.com"];
o.Email = "[email protected]";
o.AcceptTermsOfService = true; // required; never default it silently
o.DirectoryUrl = AcmeDirectories.LetsEncryptStaging; // staging until it works (rate limits)
// o.Challenges = AcmeChallengeTypes.TlsAlpn01; // when only 443 is reachable
// o.ExternalAccountBinding = new(kid, hmacKey); // ZeroSSL / commercial CAs
// o.StorePath = "/var/lib/myapp/acme"; // put on a volume in containers
});
// HTTP/3: await app.ListenHttp3Async(o => { o.Port = 443; o.UseAcme(app); });
ServerCertificate + pinning or a tunnel.Certificate/CertificateSelector gets the ACME certificate; renewals
hot-swap per handshake, no restart. app.GetAcmeCertificateManager() exposes CertificateChanged,
RenewAsync, EnsureCertificateAsync.AddAcme before auth/IP-filter/host-filter middleware that would reject the CA's anonymous GET.StorePath — re-ordering on every start hits rate limits.HttpsOptions.CertificateContextSelector (per-handshake certificate
Http3Options) and HttpsOptions.ChallengeResponder (sees SNI + ALPN first).shiny.acme.certificate.issuances (result) and
shiny.acme.certificate.expiry (days, observable). manager.IssuanceFailed fires on every failed order.Many hosts, one certificate each, added at runtime (an edge server, a multi-tenant host) — use
AcmeCertificateRegistry, not several managers:
builder.AddAcmeRegistry(o =>
{
o.Email = "[email protected]";
o.AcceptTermsOfService = true;
o.DirectoryUrl = AcmeDirectories.LetsEncryptStaging;
// o.FallbackCertificate = ServerCertificate.Create(...); // unknown SNI gets this, not a failed handshake
});
var app = builder.Build();
var acme = app.GetAcmeCertificateRegistry(); // no container: new AcmeCertificateRegistry(opts); app.UseAcme(acme);
// HTTP/3: o.UseAcme(acme) on Http3Options
acme.Set("grafana", ["grafana.example.com"]); // add/rename; loads a stored cert; NEVER orders
await acme.IssueAsync("grafana", ct); // the only first issuance; throws AcmeException
await acme.ImportAsync("grafana", fullChainPem, keyPem, ct); // or X509Certificate2 / pfx bytes
await acme.RemoveAsync("grafana");
acme.IssuanceFailed += (_, e) => ...; // e.Name, e.Exception, e.IsRenewal
IssueAsync automatically on Set/startup/discovery: first issuance is
meant to be explicit, and a failed one is deliberately not retried (CA rate limits). Renewal of an
issued certificate is automatic.*. wildcard on a served cert → FallbackCertificate → refused. One HTTP-01
middleware and one TLS-ALPN-01 hook serve every entry, including ones added after start.acme.Entries / GetEntry(name) give State (None/Issuing/Issued/Failed), NotAfter,
NextRenewal, LastError for a status screen.Needs Shiny.Net.HttpServer.Proxy and using Shiny.Net.HttpServer.Proxy; — it is not in the core
package. This is tier 2 (routes): a proxy route is an ordinary route with a generated handler, so
middleware, authentication and rate limiting apply to it exactly as they do to anything else.
One destination — reach for this first, and only escalate when the ask names more than one:
app.MapProxy("/api/{*path}", "https://api.example.com");
app.MapProxy("/printer/{*path}", "http://192.168.1.50", o => o.RewriteHost = false);
Bodies stream both ways, X-Forwarded-* describe the original caller, an unreachable upstream is a
502, one that will not answer is a 504, and a WebSocket (or any HTTP/1.1 upgrade) is forwarded
end to end — o.ForwardUpgrades = false turns that off.
A cluster when there is more than one instance behind the route:
app.MapProxy("/api/{*path}", cluster =>
{
cluster.AddDestination("a", "https://a.internal");
cluster.AddDestination("b", "https://b.internal");
cluster.LoadBalancing = LoadBalancingPolicy.PowerOfTwoChoices; // default; also RoundRobin,
// LeastRequests, Random, First
cluster.HealthCheck.Active.Enabled = true; // off by default — probes cost battery
cluster.HealthCheck.Active.Path = "/health";
cluster.SessionAffinity.Mode = SessionAffinityMode.Cookie; // or Header
});
Passive health (on for a cluster, off for the single-destination call) takes a destination out after
FailureThreshold consecutive transport failures for ReactivationPeriod. When every destination is
out, the route answers 503.
Transforms instead of BeforeSend/AfterReceive whenever the change is a path, query or header:
cluster.Transforms
.RemovePathPrefix("/api")
.SetQueryValue("tenant", "acme")
.SetRequestHeader("X-Api-Key", key)
.RemoveResponseHeader("Server");
From configuration when the ask is routes that change without a rebuild. The shape matches YARP's,
including {**rest} catch-alls, and it is parsed by hand rather than reflection-bound, so it stays
AOT-clean:
builder.AddReverseProxy(configuration.GetSection("ReverseProxy"));
// no container: var proxy = app.MapReverseProxy(configuration.GetSection("ReverseProxy"));
{ "ReverseProxy": {
"Routes": { "api": { "ClusterId": "backend", "Match": { "Path": "/api/{**rest}" },
"Transforms": [ { "PathRemovePrefix": "/api" } ] } },
"Clusters": { "backend": { "LoadBalancingPolicy": "LeastRequests",
"Destinations": { "d1": { "Address": "https://a.internal" } } } } } }
Reload is applied to the running server: routes swap atomically, clusters keep their health state, and routes mapped in code are untouched.
HttpForwarder.ForwardAsync(ctx, "http://…") forwards from inside a hand-written handler.TE: trailers is
forwarded, so gRPC proxies: set o.RequestVersion = HttpVersion.Version20; o.VersionPolicy = HttpVersionPolicy.RequestVersionExact; for an h2c upstream, and the caller must use h2/h3.shiny.proxy.upstream.request.duration (to response headers, by
server.address) and shiny.proxy.upstream.errors (error.type = ProxyError kind).// Zero-account public HTTPS from a phone — pinggy by default, nothing to configure
builder.AddQuickTunnel();
// then, from a button: await tunnel.StartAsync();
// A host you own
builder.AddSshTunnel(o =>
{
o.Host = "tunnel.example.com"; o.Username = "tunnel"; o.PrivateKeyPath = keyPath;
o.RemoteBindAddress = "0.0.0.0"; o.RemotePort = 8080;
o.HostKeyFingerprints.Add("SHA256:…"); // required unless AcceptAnyHostKey
});
// Any provider, manually
await app.RunTunnelAsync(provider, logger, ct);
QuickTunnel is INotifyPropertyChanged — bind to PublicUrl, never read it once: a free
tunnel reassigns the address on every reconnect. Its events fire on a background thread; marshal to
the UI thread in MAUI.
StartAsync can return null — the tunnel connected but never learned an address. Check for it
and show LastError; do not treat a non-throwing call as success. It takes a cancellation token,
and a UI must leave the user a way to cancel rather than disabling every control behind one
IsBusy flag while a network round trip is in flight.
Host presets are not interchangeable, and only the default works with nothing provisioned:
QuickTunnelHost.Pinggy (default) — no account. Generates its own key via UseEphemeralKey.
Anonymous tunnels expire after 60 minutes; pass an access token as the subdomain argument.QuickTunnelHost.Sish — tuns.sh needs a key enrolled at pico.sh; an unknown key is
refused. Use it when the address must stay stable, since it follows the key.QuickTunnelHost.LocalhostRun — cannot report its own address through this library: it never
confirms the session request carrying the URL, and SSH.NET waits for that confirmation where the
ssh binary does not. Only usable with a known custom domain set as PublicUrl.QuickTunnelHost.Serveo — frequently unreachable; do not suggest it for a demo.Writing UrlPattern for a provider of your own: anchor it to that provider's tunnel domain. Never
"first https:// in the output" — providers print a welcome banner full of links to their own site
before announcing your address.
Always put authentication in front of anything exposed by a tunnel.
Hosting the relay with a real key registry: RelayServerOptions.Authorize is async —
(request, ct) => ValueTask<string?> returning the subdomain to grant or null to refuse. Pass
ct into the lookup (it carries AuthorizeTimeout, 15s by default), and do not catch-and-grant:
a throw already refuses the registration and is logged. Tag the grant with request.State = keyId
rather than keeping the token; relay.TunnelConnected / TunnelDisconnected report it, relay.Tunnels
lists it, and revoking is "make Authorize refuse, then await relay.DisconnectAsync(t => Equals(t.State, keyId))" — in the other order a reconnecting client is simply let back in.
Relay flow control is on by default on both ends (EnableFlowControl), negotiated with Settings
frames that older ends ignore, so any mix of old and new relay/client works. Each request gets its own
window, so one stalled download or unread upload no longer stalls the tunnel. Leave the defaults
unless asked: windows start at StreamWindowSize (256 KiB) and grow toward MaxStreamWindowSize
(8 MiB) within TunnelWindowBudget (64 MiB). On a shared relay with many tunnels, lower
MaxStreamsPerTunnel (512) to bound memory: worst case per tunnel is streams × starting window +
budget. A public connection beyond the limit gets a 503, and kept-alive connections count while open.
PublicWriteTimeout (60s) disconnects a public client that stops reading.
Relay behind a reverse proxy on the same host: ControlAddress = IPAddress.Any,
PublicAddress = IPAddress.Loopback, IncludePortInPublicUrl = false, and
AddForwardedHeaders = false (the proxy's X-Forwarded-* are the real ones). Per-tunnel scheme:
set request.PublicScheme = "http" in Authorize for a host without a certificate.
AzureRelay is deliberately not AOT-clean; do not suggest it for a trimmed/AOT app.
Shiny.Net.HttpServer.Tunnels supervises the vendor agents — cloudflared, ngrok,
tailscale — for hosts that can start a process. Not on a phone: iOS forbids it outright, so
on mobile the answer is still the SSH provider or the relay.
await server.StartAsync(); // an agent forwards to a bound port
await using var tunnel = new CloudflareTunnel(new CloudflareTunnelOptions());
var url = await tunnel.StartAsync(server); // reads the port off the server
// or with a host: registered after AddHttpServer, so the port exists when the agent starts
builder.AddCloudflareTunnel(); // AddNgrokTunnel() / AddTailscaleFunnel()
A missing binary throws TunnelAgentException naming what to install; an agent that dies during
startup fails immediately rather than waiting out the timeout. A token-less Cloudflare tunnel is a
quick tunnel — new hostname every run.
Tier 1: a mounted module of raw routes — one POST /{service}/{method} per method. Reach for it when
the user says gRPC, proto, RPC service or grpc-web.
app.MapGrpcService("greet.Greeter", svc =>
{
// Marshalling is supplied, never discovered. Register once per message type, before the
// methods that use it — a missing one throws at startup naming the method.
svc.AddMarshaller<HelloRequest>(m => m.ToByteArray(), HelloRequest.Parser.ParseFrom);
svc.AddMarshaller<HelloReply>(m => m.ToByteArray(), HelloReply.Parser.ParseFrom);
svc.MapUnary<HelloRequest, HelloReply>("SayHello", (request, context) =>
new ValueTask<HelloReply>(new HelloReply { Message = $"Hello {request.Name}" }));
svc.MapServerStreaming<HelloRequest, HelloReply>("Greetings", Greetings);
svc.MapClientStreaming<Reading, Summary>("Upload", async (requests, context) => …);
svc.MapDuplexStreaming<Note, Note>("Chat", Chat);
})
.RequireAuthorization();
Grpc.AspNetCore, Grpc.Core or Grpc.Core.Api on the server side — none of
them are used here. For .proto files add Google.Protobuf + Grpc.Tools with
GrpcServices="None": the generated message classes are what marshallers need, not the
generated service base.IAsyncEnumerable<T> both ways. Generate static async IAsyncEnumerable<TResponse>
local or member methods for streaming handlers rather than lambdas — a lambda cannot be an
iterator.throw new GrpcStatusException(GrpcStatusCode.NotFound, "…").
Anything else becomes Unknown with a generic message unless EnableDetailedErrors is set.context.CancellationToken — thread it through every await. grpc-timeout
expiry is reported as DeadlineExceeded automatically.context.RequestHeaders, write context.ResponseTrailers (writable all call long)
and context.ResponseHeaders (until the first message goes out).Options.Http2.AllowCleartext = true; an HTTP/1.1
caller gets a 505 pointing at gRPC-Web.Unimplemented to a web caller. Pair it with a CORS policy that exposes
grpc-status and grpc-message.Response.Trailers / AppendTrailer / DeclareTrailer send headers after the body, on HTTP/1.1,
HTTP/2 and HTTP/3. Use them to report what a handler could not know before it started writing — a
checksum, a row count, an outcome discovered mid-stream. This is what carries gRPC's status.
ctx.Response.DeclareTrailer("X-Row-Count");
await WriteCsvAsync(ctx.Response.Body);
ctx.Response.AppendTrailer("X-Row-Count", rows.ToString());
On HTTP/1.1 the response must not declare a Content-Length — trailers ride the terminating
chunk, and there is no chunk on a length-delimited body. Do not set ContentLength on a handler
that appends trailers.
Tier 1 (raw delegate) — a response API usable from any handler, middleware or typed endpoint.
Send Link preloads before doing slow work, so the browser fetches CSS/JS/fonts in parallel.
Call it first in the handler — after the response has started it throws InvalidOperationException.
app.MapGet("/", async ctx =>
{
await ctx.Response.SendEarlyHintsAsync(
"</app.css>; rel=preload; as=style",
"</app.js>; rel=preload; as=script");
var model = await LoadAsync(ctx.RequestAborted); // slow part
ctx.Response.Headers.Append(HeaderNames.Link, "</app.css>; rel=preload; as=style"); // repeat on final
await ctx.Response.WriteTextAsync(Render(model), "text/html; charset=utf-8");
});
ValueTask<bool>: false (nothing sent) on HTTP/1.0 — do not treat that as an error.SendInformationalResponseAsync(StatusCodes.Status102Processing, headers); any 1xx except 101.Content-Length or Transfer-Encoding in the headers (throws).HttpClient (and the in-memory test client) discard 1xx — assert on the final response, or read raw bytes.builder.Services
.AddMcpServer(o => o.ServerInfo = new Implementation { Name = "device", Version = "1.0.0" })
.WithTools<DeviceTools>(AppJson.Default.Options) // pass the context — see below
.WithHttpTransport(o => { o.MaxSessions = 8; o.IdleSessionTimeout = TimeSpan.FromMinutes(10); });
app.MapMcp(); // POST/GET/DELETE/OPTIONS on /mcp
JsonSerializerContext's options to WithTools<T>(). MapMcp() throws at startup naming the type
if you miss one.WithToolsFromAssembly() / WithPromptsFromAssembly() / the IEnumerable<Type>
overloads — they scan at runtime and are RequiresUnreferencedCode.AllowedOrigins is empty by default and should stay that way unless a browser client needs it.An MCP client that meets a bare 401 has no idea where to log in. RFC 9728 is the protocol's answer: the challenge names a metadata document, and the document names the authorization server.
builder.AddMcpProtectedResource(o =>
{
o.AuthorizationServers.Add("https://login.example.com");
o.ScopesSupported.Add("mcp:tools");
o.Resource = "https://device.example.com/mcp"; // omit to derive it from the request host
});
app.MapMcp().RequireAuthorization();
app.MapMcpProtectedResource(); // /.well-known/oauth-protected-resource[/mcp]
The 401 then carries WWW-Authenticate: Bearer resource_metadata="…". Validating the token itself is
still AddJwtBearer(...), and its audience should be the same Resource published here. Leave
Resource null for a tunnelled server whose public address is new every run.
Shiny.Net.HttpServer.Mediator publishes mediator handlers as endpoints. Same shape as
Shiny.Mediator.AspNet; the generator ships inside the package, so there is no second reference.
[MediatorHttpGroup("/api/widgets", Tags = ["Widgets"], RequiresAuthorization = true)]
public class WidgetHandlers : IRequestHandler<GetWidget, Widget>, ICommandHandler<DeleteWidget>
{
[MediatorHttpGet("/{id:int}")]
public Task<Widget> Handle(GetWidget request, IMediatorContext ctx, CancellationToken ct) => …;
[MediatorHttpDelete("/{id:int}")] // ICommand -> 204, or SuccessStatusCode = 202
public Task Handle(DeleteWidget command, IMediatorContext ctx, CancellationToken ct) => …;
}
app.MapGeneratedMediatorEndpoints(); // or Map{Handler}MediatorEndpoints()
GET/DELETE bind each contract
member from a route token (matched by name) or the query string. POST/PUT/PATCH read the
whole contract from the JSON body, then apply any route token over the top — so PUT /widgets/{id}
works, and the URL wins over the body.IParsable<T>, an enum, a string, or an array of those —
otherwise SWM003 at build time. Nullable members and members with defaults are optional.with) or a settable property;
init-only on a non-record is SWM008.IStreamRequest<T> becomes Server-Sent Events and must be GET (SWM007). EventName sets the
SSE event: field.[JsonSerializable] — SWM006 warns when a context misses one.MapMediatorPost<TRequest,TResult>(pattern) etc. exist for runtime routes;
the GET/DELETE overloads take a bind delegate because there is no reflection to fall back on.Two integration gotchas — mention both when generating this:
Shiny.Mediator's own generator turns a contract with [Get]/[Post]/[Put]/[Patch]/
[Delete] into an HTTP client, and matches those attributes by simple name with no namespace
check — so it errors (SHINYMED_HTTP001) on this server's minimal-endpoint attributes. A
project using both needs <NoWarn>$(NoWarn);SHINYMED_HTTP001</NoWarn>.IConfiguration in the container (AddShinyMediator registers a
timer-refresh stream middleware that takes one). A generic host has it; a hand-built
ServiceCollection does not, and the failure lands after the SSE headers have gone out.Shiny.Net.HttpServer.DocumentDb publishes a document type as a REST resource. Same shape as
Shiny.DocumentDb.AspNetCore, on a server that runs in a MAUI app.
app.MapDocuments<Order>("/orders", o =>
{
o.Operations = DocumentEndpoints.All; // default is Read | Count — the safe half
o.TypeInfo = AppJson.Default.Order; // REQUIRED here; there is no reflection fallback
o.AllowFilterOn(x => x.Status, x => x.Total);
o.Scope<ITenant>((t, ctx) => x => x.TenantId == t.Id);
})
.RequireAuthorization("orders"); // fans out to every route the resource mapped
GET /, GET /{id}, GET /count, GET /stream (SSE), POST /, PUT /{id}, PATCH /{id},
DELETE /{id} — whichever the Operations flags allow.TypeInfo. Unlike the ASP.NET package there is no reflection fallback; without
source-generated metadata the first request throws with a message naming the property.?filter= (grammar), ?orderby=, ?fields= (sparse), ?skip=/?take= (clamped to
MaxPageSize, never refused), ?cursor= (keyset; cannot be combined with fields).Scope(...) is the security boundary: AND-ed into reads, checked on both sides of a write,
and an out-of-scope document is 404 not 403. Read the request from ctx.Http — the ambient
IHttpContextAccessor is only published when UseSessions() is mapped.MapDocumentCollection is the schema-free lane (relational providers only). Scoped inserts and
replaces are refused there by design — a raw JSON body cannot be checked against the scope.PATCH is RFC 7396 merge patch (an explicit null removes the member) — or RFC 6902 JSON Patch when
Content-Type is application/json-patch+json; same If-Match and scope checks either way.DocumentEndpoints.Stream on a provider without change monitoring is a startup error.tvOS is not MAUI — MAUI does not target the platform. A tvOS app is a native app on
net10.0-tvos or net11.0-tvos with a UIApplicationDelegate, and it references Shiny.Net.HttpServer
the way any other project does; the core is plain net10.0/net11.0 and needs nothing special.
Rules that matter when generating a tvOS app:
NSLocalNetworkUsageDescription in Info.plist is mandatory. tvOS 16+ gates serving on the
local network exactly as iOS 14+ does. Without it the listener binds, reports Running, and
nothing can reach it — and a TV has no browser to test with, so nothing reveals the cause.IPAddress.Any, not loopback. Nothing on the device itself will ever consume the server.Shiny.Net.HttpServer.Mobile — it does not target tvOS yet. Start the server from
WillEnterForeground / OnActivated and stop it in DidEnterBackground. tvOS suspends a
backgrounded app exactly as iOS does, so there is no keep-serving option to offer.Microsoft.Extensions.Logging.Console. It P/Invokes GetStdHandle and
GetConsoleMode, which fails the native link on tvOS — the C# compiles clean and clang then
stops with "Undefined symbols for architecture arm64". The server resolves ILoggerFactory
optionally, so registering no logging provider at all is supported.ZipFileSource, and do not put a DocumentDb file anywhere you
need to survive a relaunch.JsonSerializerContext; nothing can
fall back to reflection.NSLocalNetworkUsageDescription in Info.plist — without it the app is denied silently.
Add every browsed service type to NSBonjourServices as well if you use discovery.com.apple.security.network.server entitlement — without it the bind is refused.android.permission.INTERNET; plus FOREGROUND_SERVICE,
FOREGROUND_SERVICE_DATA_SYNC and (API 33+) POST_NOTIFICATIONS for background serving.LocalNetworkAccess.Check() (in Shiny.Net.HttpServer.Mobile) reads the bundle or manifest and
reports which are missing, because none of these produce an error you could diagnose from. Missing
entries that stop the server being reachable come back in Problems (CanServe is then false);
things that only matter for discovery come back in Notes. The one it cannot check is the Mac
Catalyst entitlement — that is not in Info.plist and is not readable from inside the process, so on
Catalyst it is always reported as a note to go and verify by hand.
services.AddShinyHttpServer(
http =>
{
http.Options.Address = IPAddress.Any;
http.AddHttpServerLifecycle(o =>
{
o.BackgroundMode = BackgroundServerMode.Stop; // or KeepAlive
o.RestartOnConnectivityChange = true; // default
o.RestartAttempts = 3; // default
o.RestartRetryDelay = TimeSpan.FromSeconds(5); // default, doubling
o.MaxRestartRetryDelay = TimeSpan.FromSeconds(30); // default
});
},
autoStart: false
);
Needs a Shiny host (UseShiny() in MauiProgram) — that is what delivers the platform callbacks. It registers
Shiny's IConnectivity itself — do not generate a separate services.AddConnectivity() for it.
Stop makes that visible: IsRunning goes false, the UI can say so, and
clients get a refused connection instead of a hang.KeepAlive starts a foreground service with a permanent notification, which is the
only supported way to hold a socket open in the background. That notification is the deal.KeepAlive restores the server on resume instead. It cannot keep the listener open, so it
does the other useful thing: the server is left running as the app goes away (a few seconds is
often enough to finish the request in flight) and is restarted when the app comes back. This
matters because the suspension takes the socket while IsRunning goes on saying true, so an app
that only re-checked IsRunning would find nothing wrong and serve nothing — and calling
StartAsync() yourself would not help either, since it is idempotent and agrees with the stale
state. RestartAsync() is what fixes it, and this does it for you.AlwaysStartOnForeground. A server that was off when the user left stays off; only
one that was running is put back. Set AlwaysStartOnForeground = true if you want it on at every
resume regardless — that overrides a toggle the user switched off, so make it a deliberate choice.HttpServer.StateChanged — so an app whose server is a toggle does not need its own bookkeeping.options.RebindOnNetworkChange, and raises
server.NetworkAddressesChanged either way so a QR code or advertisement can be refreshed.
Binding to IPAddress.Any survives an address change; binding to a specific address does not. The
rebind retries on a network that is still coming up — see
Lifecycle — and its transitions carry
NetworkChanged, so a subscriber can tell a rebind from a shutdown.Error. A
connectivity rebind, an Apple resume restart and a foreground start all run through the same
bounded backoff — RestartAttempts (3), RestartRetryDelay (5s, doubling), MaxRestartRetryDelay
(30s) — because the moment they run is the moment a bind is refused: the new interface is not
routable yet, or the old port is still in TIME_WAIT. Do not "fix" a rebind failure by wrapping
these calls in your own retry; set the options instead. This is outside the core's
StartRetryAttempts, so the two multiply — the core never retries a start the caller asked for,
and on this path the caller is a lifecycle callback with nobody to tell.LogWarning when generating similar code. A crash reporter's
Microsoft.Extensions.Logging bridge files an event at Error and only a breadcrumb at Warning,
so a server that stopped and logged a warning is a server that told nobody. The same rule applies
to any handler an app writes on StateTransitioned.StartService posts an
intent and returns; Android refuses it inside the service when the app is not entitled to one, and
the package checks five seconds later and logs at Error naming the manifest entries to look at.
If the OS stops the service while the server is still listening, that is logged at Error too. If
an app generates its own foreground service, do the same — do not assume StartService worked.Binding a port solves being reachable. It does not solve being found: the address is assigned by whatever network the device joined and changes when it moves. That leaves a QR code, typing an IP, or mDNS.
// the device that serves — inside AddShinyHttpServer / on the builder
http.AddHttpServerAdvertisement(o =>
{
o.ServiceType = "_myapp._tcp"; // a private type: browsing _http._tcp finds printers too
o.TxtRecords["role"] = "controller";
});
// the device that looks
builder.Services.AddHttpServerLocator();
var found = await locator.FindFirstAsync("_myapp._tcp", TimeSpan.FromSeconds(5));
using var client = new HttpClient { BaseAddress = found!.BaseAddress };
// or bind a list to it
await foreach (var change in locator.WatchAsync("_myapp._tcp", ct)) { … }
The advertisement follows the server — published when it starts, withdrawn (with a goodbye packet)
when it stops, re-announced when the device changes network. Read the final instance name back from
IHttpServerAdvertiser.Publication: the responder renames a service whose name is already taken.
The locator leaves out the app's own advertisement (anything an advertiser has live on the same
IMdnsManager), so a peer-to-peer app that both hosts and browses never lists itself. Don't generate a
"skip my own instance name" filter — it's already done, and exactly.
It follows the reason as well as the state, which is the pattern to copy for any handler that
tears something down on Stopped: it subscribes to StateTransitioned, and a Stopped carrying
Restarting or NetworkChanged leaves the record standing rather than sending a goodbye and a fresh
announcement for a service that never actually went away. If the start half never lands, the
Stopped that follows carries BindFailed, and that one withdraws.
Publishing is retried — PublishAttempts (3), PublishRetryDelay (1s, doubling),
MaxPublishRetryDelay (15s) — with an Error carrying the exception when the attempts are spent. A
responder is least likely to answer at exactly the moment this runs, and the failure it leaves behind
is the hard kind: a server running perfectly, findable by nobody, looking healthy from inside the app.
Prefer plain HTTP on the LAN; terminate TLS at a tunnel. A self-signed certificate needs per-device
trust (ServerCertificate.Create() / CreateOrLoad(path), plus
CertificatePinning.CreateHandler(cert) for the app's own HttpClient).
await using var app = TestHttpServer.Create(
server => server.MapMyAppEndpoints(),
builder => builder.Services.AddSingleton<IClock>(new FrozenClock(…))
);
Assert.Equal("pong", await app.Client.GetStringAsync("/ping"));
No port, no listener, nothing to leak when a test fails half way through — but the real parser,
router, middleware and response framing, because only the socket is replaced. app.CreateClient()
gives a second caller with its own connection and cookies; TestHttpServer.Create(..., useHttp2: true) speaks HTTP/2 by prior knowledge. server.CreateInMemoryClient() does the same for a server
built elsewhere.
Use a real socket instead when the thing under test is the socket: TLS, connection limits, or anything about how the OS frames bytes.
| Code | Meaning |
|---|---|
| SWS001 | Invalid route template |
| SWS002 | Parameter cannot be bound |
| SWS003 | Unsupported return type |
| SWS004 | Endpoint class/method not reachable from generated code |
| SWS005 | Duplicate route in the assembly |
| SWS006 | Warning — type crosses an endpoint boundary but no JsonSerializerContext declares it |
| SWS007 | More than one body parameter |
| SWS008 | [FromRoute] names a token the template lacks |
| SWS009 | Warning — template captures a token no parameter receives |
| SWS010 | IHttpEndpoint without a single Handle/HandleAsync |
| SWS011 | IHttpEndpoint without a verb attribute on the class |
| SWS030 | Invalid API version in [ApiVersion] / [MapToApiVersion] |
| SWS031 | Warning — [MapToApiVersion] names a version the class never declares |
| SWB001–SWB006 | Switchboard can't be dispatched: generic/inaccessible class, no constructor, duplicate method name, unbindable parameter, unsupported return, generic method |
| SWB007 | Warning — [AllowAnonymous] on a method of an [Authorize] switchboard has no effect |
| SWB008 | TClient interface member that can't be called on a client (non-method, non-Task return) |
| SWB009 | Warning — a contract interface method clients can't call under its name |
| SWB010 | Warning — a type [ExportContract] can't include |
| SWBC001–SWBC003 | Typed client ([SwitchboardClient<,>]): class not partial/top-level, unsupported member, name collision |
The generator also emits metadata for [RequestTimeout], [DisableRequestTimeout], [OutputCache],
[NoOutputCache], [ValidateAntiforgery], [DisableAntiforgery], [Idempotent], [DisableIdempotency],
[ContentDigest], [DisableContentDigest], [RequireWebhookSignature], [RequestSizeLimit],
[DisableRequestSizeLimit] and [ApiVersion]/[MapToApiVersion]/
[ApiVersionNeutral], exactly as it does for
[Authorize], [EnableCors], [EnableRateLimiting] and [RequireIpFilter] — a method's attribute
replaces the class's, and a Disable anywhere wins.
JsonSerializerContext and never use the reflection JSON overloads.byte, short, int, long, float, double,
decimal, bool, guid, alpha, datetime, dateonly, timeonly, timespan,
minlength(n), maxlength(n), length(n), min(n), max(n), range(a,b), apiVersion. There is no
regex — validate anything richer in the handler so it can return a meaningful error instead of
a 404. Length constraints count characters; min/max/range compare the value and may take
negative arguments.
A constraint only decides whether the route matches — it converts nothing, and the binder
handles every IParsable<T> regardless. So {id:int} on a handler taking a long is fine, a
refused segment is a 404, and a matched-but-unparseable one is a 400.v{version} is rejected at registration).CancellationToken (or ctx.RequestAborted) into everything async.HttpContext past the handler — contexts are pooled.IPAddress.Any when the user asked for network access.ContinueIncomingTrace and health-check details there too.IPAddress.Any, or turn on rebinding; publish
the server over mDNS rather than asking anyone to type an IP.ctx.RequestAborted into the slow work
or the timeout only changes what the client sees.WithRequestSizeLimit /
[RequestSizeLimit] on the upload, a small Limits.MaxRequestBodySize everywhere else.StateTransitioned, not StateChanged, when the app has to report why the server
stopped. StateChanged cannot tell "the user switched it off" from "the listener died", and on
a device that distinction is the whole bug report. Treat Reason == Restarting as not down.ContentLength only to the exact number of bytes you will write. Every protocol holds a
response to it: writing more throws, and writing less fails the response (a reset stream on
HTTP/2 and HTTP/3, a closed connection on HTTP/1.1). For HEAD, write the same body as the GET
— the server drops it — so the headers match.What SKILL.md sets; muted values are Claude Code's defaults
npx claudepluginhub shinyorg/skills --plugin shiny57 skills in this plugin
/localizegenGenerate strongly-typed localization classes from .resx resource files using Shiny.Extensions.Localization.Generator for .NET applications/shiny-accessorysetupGenerate code using Shiny.BluetoothLE.AccessorySetup - the system accessory pairing sheet for BLE devices (AccessorySetupKit on iOS 18+, CompanionDeviceManager on Android) with no MFi, no location prompt and no general Bluetooth prompt on iOS; picked accessories open as Shiny.BluetoothLE peripherals, and Android adds background presence (device appeared / disappeared) events/shiny-actorsGenerate code using Shiny.Actors — Orleans-style virtual actors without the ceremony, AOT/trim-clean, running anywhere .NET runs including .NET MAUI and Blazor WebAssembly. Covers actor interfaces and source-generated proxies, the ShinyActorBuilder (AddShinyActors), persistent state with ETags, named states and auto-save, event-sourced JournaledActor with snapshots, state and event migrations, typed streams (durable, implicit consumers), timers and persistent reminders (background jobs via Shiny.Jobs, on-time OS notifications via Shiny.Notifications), concurrency control ([Reentrant], [AlwaysInterleave], [ReadOnly], [StatelessWorker]), call filters and request context, OpenTelemetry traces and metrics, remoting over HTTP (Shiny.Net.HttpServer server, RemoteActorSystem client), mDNS discovery of actor servers, Shiny.DocumentDb storage (SQLite, PostgreSQL, IndexedDB...), and the ActorTestHost testing kit./shiny-agesignalsGenerate code using Shiny.AgeSignals to read the user's age range as the app store shares it - Apple Declared Age Range (iOS / macOS 26+) and Google Play Age Signals (Android) - for app store accountability laws (Texas SB 2420, Utah, Louisiana, California AB 1043) and age-gated features/shiny-aiconversationGenerate code for Shiny.AiConversation v3 - session-based AI conversations for .NET over Microsoft.Extensions.AI with persisted, resumable sessions (IAiSessionManager / IAiSession / IAiTurn), streaming replies, the built-in ask_user tool for typed questions and multiple-choice answers, interruption and supersession, usage and context-window pressure tracking, rolling context compaction, silent assistant memory, conversation search, session titles and long-term memory, session interceptors for tools and prompts, pluggable session/memory stores (in-memory or SQLite via Shiny.AiConversation.Stores.SqliteDocDb), and voice conversations via Shiny.AiConversation.Voice (wake word, push-to-talk, follow-up, quiet-word/barge-in interruption, sentence-streamed text-to-speech over Shiny.Speech, acknowledgement modes, sounds, voice selection tools), the MAUI chat UI via Shiny.AiConversation.Maui (AiChatView bound to a session with streaming bubbles, choice chips, push-to-talk and an AiMemoryView), image attachments with retention, and the use_camera tool via Shiny.AiConversation.Maui.Camera, and AG-UI via Shiny.AiConversation.AgUi (a session as an AG-UI event stream for the Shiny AgentView with ask_user questions as interrupts, the render_ui tool for A2UI surfaces, and a remote AG-UI agent as the chat client)/shiny-appdevicebridgeGenerate code using Shiny.AppDeviceBridge, device bridges on your app's own Shiny.Net.HttpServer that also host a web app (Blazor WebAssembly, React, Vue, any static build) inside a .NET MAUI app on Android, iOS, Mac Catalyst, Windows and the maui-labs macOS and Linux heads — served from a loopback HTTP server, updated over the air from a signed release server, GitHub releases or a provider of your own, and given device access through bridges with typed C# and TypeScript clientsOther plugins' skills for the same job