How to create a Worker Service in ASP.NET Core
In this article, let's learn about what is a Hosted Service and how we can create and use a Hosted Service inside ASP.NET Core with an example.
We can make use of the built-in UseExceptionHandler() middleware for catching Global Errors in ASP.NET Core.
app.UseExceptionHandler(err =>
{
err.Run(async ctx =>
{
context.Response.StatusCode = 500;
context.Response.ContentType = "application/json";
// gets error detail
var ex = context.Features.Get<IExceptionHandlerPathFeature>();
var error = new MyResponseModel() { Content = ex.Error.Message };
await context.Response.WriteAsync(JsonConvert.SerializeObject(error));
});
});