Overview
editMeziantou.AspNetCore.ServiceDefaults gives a web application the setup that production needs, in two calls: UseMeziantouConventions on the builder registers the services, and MapMeziantouDefaultEndpoints on the application adds the middleware and the endpoints.
var builder = WebApplication.CreateBuilder(args);
// Add Meziantou service defaults
builder.UseMeziantouConventions();
var app = builder.Build();
// Map default endpoints (health checks, OpenAPI, etc.)
app.MapMeziantouDefaultEndpoints();
app.MapGet("/", () => "Hello World!");
app.Run();The extension methods are in the Meziantou.AspNetCore.ServiceDefaults namespace. UseMeziantouConventions works on any IHostApplicationBuilder; MapMeziantouDefaultEndpoints extends WebApplication.
What the builder gets
UseMeziantouConventions configures:
- Dependency injection validation: the service provider is built with
ValidateOnBuildandValidateScopes, so a missing registration or a scoped service injected into a singleton makesbuilder.Build()throw instead of failing on the first request. - OpenTelemetry: logs (with the formatted message and the scopes), metrics (ASP.NET Core,
HttpClient, the .NET runtime and theMeziantou.*meters) and traces (ASP.NET Core with SignalR,HttpClient, theMeziantou.*sources and a source named after the application). The/healthand/aliverequests are not traced. The service name isOTEL_SERVICE_NAMEwhen it is set, the application name otherwise, and the service version is the version of the entry assembly. The OTLP exporter is enabled whenOTEL_EXPORTER_OTLP_ENDPOINTis set. - Health checks: a
selfcheck taggedlive. - OpenAPI:
AddOpenApi, for the document served by MapMeziantouDefaultEndpoints. - HTTP clients: service discovery, the standard resilience handler (retries, timeouts, circuit breaker) and a
User-Agentheader named after the application, on every client created byIHttpClientFactory. - JSON: camelCase names, string enums and the other conventions described in JSON conventions, for minimal APIs and for controllers.
- Problem details and validation:
AddProblemDetailsandAddValidation. - Antiforgery:
AddAntiforgery. - Kestrel: the
Serverresponse header is no longer sent.
Each feature that can be turned off has an option; see Options.
What the application gets
MapMeziantouDefaultEndpoints adds forwarded headers, the no-cache default, HTTPS redirection, an exception handler and HSTS outside Development, and antiforgery, then maps /health, /alive, the static assets and the OpenAPI document. The order and the details are in Default endpoints and middleware.
Forgetting a call
The two calls go together. When MapMeziantouDefaultEndpoints is not called, the application throws an InvalidOperationException that names the missing call when it starts, rather than running without its health checks and security middleware.
Calling it more than once
UseMeziantouConventions registers everything again each time it is called. Code that cannot know whether the conventions are already registered, such as a shared extension method, calls TryUseMeziantouConventions, which does nothing when they are:
builder.UseMeziantouConventions();
builder.TryUseMeziantouConventions(); // Will not register againWhen TryUseMeziantouConventions does nothing, its options are ignored: the options of the first call apply. MapMeziantouDefaultEndpoints can be called more than once; the calls after the first do nothing.
Applying the defaults without writing the calls
The Meziantou.AspNetCore.ServiceDefaults.AutoRegister package adds both calls to every WebApplicationBuilder.Build() of your project. See Applying the defaults automatically.
In this group
- Options: turn features off and customize OpenAPI, OpenTelemetry and JSON.
- Default endpoints and middleware: the middleware order, the health endpoints and what to check before going to production.
- JSON conventions: how requests and responses are serialized.
- Applying the defaults automatically: the source generator that writes the calls for you.