BackgroundService vs IHostedService in ASP.NET Core (.NET 10)

BackgroundService vs IHostedService in ASP.NET Core: IHostedService blocked startup for 2.02 s and BackgroundService for 0.00 s on .NET 10

Use BackgroundService for work that runs as long as the app does, like a sync loop, and IHostedService only when you need exact control over start and stop. That’s the rule for BackgroundService vs IHostedService: one is built on the other. I timed both on .NET 10 with 2 seconds of blocking startup work. IHostedService … Read more >>

Minimal API Validation in ASP.NET Core (.NET 10): AddValidation, DataAnnotations and 400 Errors

Minimal API validation in .NET 10: AddValidation() returns 400 with Name and Email errors from DataAnnotations on a record

Your endpoint just saved an order with a blank customer name and the email address tyler.brooks@. Nothing stopped it. Quick answer: minimal API validation is built into .NET 10. Call builder.Services.AddValidation(), put DataAnnotations such as [Required] and [Range] on your request types, and bad requests get a 400 before your handler runs: Below, we build … Read more >>

Global Error Handling in ASP.NET Core with IExceptionHandler and ProblemDetails

ASP.NET Core global exception handling in .NET 10: an IExceptionHandler turns a TimeoutException into an application/problem+json response with status 503

For ASP.NET Core global exception handling, call AddProblemDetails() and app.UseExceptionHandler(), then add an IExceptionHandler class for each kind of error you want to map. Every unhandled exception comes back as a clean application/problem+json response instead of a stack trace or an empty 500. I built a small .NET 10 shipping API with three handlers, broke … Read more >>