Options

edit

Pass a delegate to UseMeziantouConventions to change the defaults:

builder.UseMeziantouConventions(options =>
{
    options.OpenApi.RoutePattern = "/swagger/{documentName}.json";
    options.Https.HstsEnabled = false;
    options.AntiForgery.Enabled = false;
    options.ForwardedHeaders.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
    options.ConfigureJsonOptions = json => json.WriteIndented = true;
    options.OpenTelemetry.ConfigureTracing = tracing => tracing.AddSource("MyCompany.*");
});

The delegate runs immediately, during the UseMeziantouConventions call, and the resulting MeziantouServiceDefaultsOptions is registered as a singleton. MapMeziantouDefaultEndpoints reads the same instance, so the options of the builder decide what the application maps.

All options

OptionDefaultEffect
ValidateDependencyContainersOnStartuptrueBuilds the service provider with ValidateOnBuild and ValidateScopes
Https.EnabledtrueAdds UseHttpsRedirection
Https.HstsEnabledtrueAdds UseHsts outside Development, when Https.Enabled is also true
OpenApi.EnabledtrueRegisters OpenAPI and maps the document
OpenApi.ConfigureOpenApinullConfigures the OpenApiOptions of the document
OpenApi.RoutePattern/openapi/{documentName}.jsonThe route of the OpenAPI document
OpenTelemetry.ConfigureLoggingnullConfigures the OpenTelemetryLoggerOptions, after the defaults
OpenTelemetry.ConfigureMetricsnullConfigures the MeterProviderBuilder, after the defaults
OpenTelemetry.ConfigureTracingnullConfigures the TracerProviderBuilder, after the defaults
ConfigureJsonOptionsnullConfigures the JsonSerializerOptions of minimal APIs and controllers, after the conventions
AntiForgery.EnabledtrueRegisters antiforgery and adds UseAntiforgery
StaticAssets.EnabledtrueMaps the static assets when the project has some
ForwardedHeaders.ForwardedHeadersXForwardedFor, XForwardedProto and XForwardedHostThe headers processed by the forwarded headers middleware
Caching.SetNoCacheWhenMissingCacheHeaderstrueAdds the no-cache middleware

Common changes

  • The application runs behind a proxy that terminates TLS and forwards plain HTTP: keep Https.Enabled and make sure the proxy sends X-Forwarded-Proto, or set Https.Enabled = false when the proxy already redirects to HTTPS.
  • The application is reachable without a proxy: restrict ForwardedHeaders.ForwardedHeaders or set it to ForwardedHeaders.None; see Forwarded headers.
  • Your own ActivitySource or Meter has a name that does not start with Meziantou.: add it with ConfigureTracing (AddSource) or ConfigureMetrics (AddMeter). The source named after the application (IHostEnvironment.ApplicationName) is already traced.
  • An API without OpenAPI: set OpenApi.Enabled = false.
  • Long-lived cacheable responses everywhere: set Caching.SetNoCacheWhenMissingCacheHeaders = false, or set Cache-Control on those responses.
  • A registration that cannot be validated at startup, such as a service built from a factory that needs a running host: set ValidateDependencyContainersOnStartup = false. Keep it on otherwise: it turns a missing registration into an error on startup instead of on the first request that needs it.