Register and activate problem-details middleware
To integrate Middleware into an ASP.NET Core application, you must register the required services and then add the middleware to the request processing pipeline. This is achieved using the AddProblemDetails and UseProblemDetails extension methods provided by the Hellang.Middleware.ProblemDetails namespace.
The following example demonstrates the standard registration and activation sequence in a top-level program. It initializes the web application builder, registers the default services, builds the application, and activates the middleware while verifying that the application builder instance is preserved.
using System;
using Hellang.Middleware.ProblemDetails;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
var builder = WebApplication.CreateBuilder(Array.Empty<string>());
// Register the required services for ProblemDetails middleware.
// This must be called before UseProblemDetails.
builder.Services.AddProblemDetails();
var app = builder.Build();
// Add the ProblemDetailsMiddleware to the application pipeline.
var appBuilder = app.UseProblemDetails();
// Verify that UseProblemDetails returns the same IApplicationBuilder instance.
if (!object.ReferenceEquals(app, appBuilder))
{
throw new InvalidOperationException("UseProblemDetails should return the same application builder instance.");
}