using System.ComponentModel.DataAnnotations; var builder = WebApplication.CreateBuilder(args); builder.Services.AddValidation(); builder.Services.AddSingleton(); var app = builder.Build(); app.MapPost("/items", (CreateItem item, AcceptedCounter counter) => TypedResults.Ok(new { item.Name, item.Quantity, accepted = counter.Next() })); app.MapGet("/accepted", (AcceptedCounter counter) => new { count = counter.Count }); app.Run(); public sealed class CreateItem { [Required, MinLength(2)] public string Name { get; set; } = ""; [Range(1, 10)] public int Quantity { get; set; } } public sealed class AcceptedCounter { private int count; public int Count => Volatile.Read(ref count); public int Next() => Interlocked.Increment(ref count); }