Register and activate problem-details middleware
To integrate Middleware into an ASP.NET Core application, you must register the required services and then activate the middleware in the request pipeline.
The AddProblemDetails extension method on IServiceCollection registers the internal ProblemDetailsFactory and configuration services. Once registered, the UseProblemDetails extension method on IApplicationBuilder inserts the ProblemDetailsMiddleware into the pipeline. If UseProblemDetails is called without a prior call to AddProblemDetails, the middleware throws an InvalidOperationException to prevent misconfiguration.
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 Hellang.Middleware.ProblemDetails
builder.Services.AddProblemDetails();
var app = builder.Build();
// Add the middleware 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.");
}