All Projects → GeeWee → BetterHostedServices

GeeWee / BetterHostedServices

Licence: MIT license
Fixed a wide variety of issues that have to do with Hosted Services and BackgroundServices, such as error handling and the abillity to access them via the DI.

Programming Languages

C#
18002 projects

Projects that are alternatives of or similar to BetterHostedServices

prometheus.aspnetcore
Prometheus instrumentation for .NET Core
Stars: ✭ 29 (+16%)
Mutual labels:  asp-net-core
ApiDmS
Open Source Document Management System.
Stars: ✭ 16 (-36%)
Mutual labels:  asp-net-core
Asp-net-Core-Project-with-Admin-Template-Setup
AdminLTE Template Setup with Asp.net Core MVC 2.1 Project
Stars: ✭ 50 (+100%)
Mutual labels:  asp-net-core
Awesome-Nuget-Packages
📦 A collection of awesome and top .NET packages sorted by most popular needs.
Stars: ✭ 87 (+248%)
Mutual labels:  asp-net-core
FluentAssertions.Web
FluentAssertions for HTTP APIs
Stars: ✭ 71 (+184%)
Mutual labels:  asp-net-core
DNZ.MvcComponents
A set of useful UI-Components (HtmlHelper) for ASP.NET Core MVC based-on Popular JavaScript Plugins (Experimental project).
Stars: ✭ 25 (+0%)
Mutual labels:  asp-net-core
ExcelExport
Classes to generate Excel/CSV Report in ASP.NET Core
Stars: ✭ 39 (+56%)
Mutual labels:  asp-net-core
Lib.AspNetCore.Mvc.Ndjson
Lib.AspNetCore.Mvc.Ndjson is a library that provides support for NDJSON (Newline Delimited JSON) based structured data streaming to ASP.NET Core MVC
Stars: ✭ 13 (-48%)
Mutual labels:  asp-net-core
HotelListing-API-dotnet5
An Educational Web API built using .NET Core 5.0. We look at REST principles, connecting to a database, using swagger and developing custom middleware to bring out the full feature set of a .NET API.
Stars: ✭ 37 (+48%)
Mutual labels:  asp-net-core
MusicDatabase-API
This project is able to manage your songs, artists, albums and more by RESTful API. Developed with ASP.NET Core 2.0 & EF Core and used PostgreSQL Database Provider. Implemented Swagger to project.
Stars: ✭ 18 (-28%)
Mutual labels:  asp-net-core
AngularAspNetCoreSignalR
Build a simple chat app with Angular and ASP.NET Core SignalR
Stars: ✭ 12 (-52%)
Mutual labels:  asp-net-core
Joonasw.ManagedIdentityDemos
Example uses of Azure Managed Identities
Stars: ✭ 24 (-4%)
Mutual labels:  asp-net-core
RESTvsGRPC
Evaluating Performance of REST vs. gRPC
Stars: ✭ 36 (+44%)
Mutual labels:  asp-net-core
Cake-Shop
A sample Cake Shop Website built with ASP.NET Core (Multi-Page Application)
Stars: ✭ 44 (+76%)
Mutual labels:  asp-net-core
.NetCorePluginManager
.Net Core Plugin Manager, extend web applications using plugin technology enabling true SOLID and DRY principles when developing applications
Stars: ✭ 17 (-32%)
Mutual labels:  asp-net-core
AspNetCore-Dynamic-Permission
Dynamic Permission Samples in ASP.NET Core and ASP.NET MVC 5.
Stars: ✭ 19 (-24%)
Mutual labels:  asp-net-core
X.Web.Sitemap
Simple sitemap generator for .NET
Stars: ✭ 66 (+164%)
Mutual labels:  asp-net-core
AspNetCoreFileUploadFileTable
ASP.NET Core MVC file upload / download with MS SQL Server FileTable
Stars: ✭ 112 (+348%)
Mutual labels:  asp-net-core
ASPCore.Two-Factor-Authentication
Perform two factor authentication in an ASP.NET core application using Google Authenticator app
Stars: ✭ 29 (+16%)
Mutual labels:  asp-net-core
smart-blazor
Blazor UI Components & Examples
Stars: ✭ 32 (+28%)
Mutual labels:  asp-net-core

Better Hosted Services

GitHub Actions Status GitHub Actions Build History

BetterHostedServices is a tiny library (<500 lines of code not including tests) that aims to improve the experience of running background tasks in ASP.NET Core. You can read more details about the issues and warts IHostedService and BackgroundService has here.

Installation

From nuget:

dotnet add package BetterHostedServices

And then call services.AddBetterHostedServices() inside your Startup.cs's ConfigureServices

BackgroundService, Error handling and CriticalBackgroundService

Microsoft recommends extending from BackgroundService for long running tasks. However BackgroundServices fails silently if an uncaught error is thrown.

That means this example will not throw an error but simply fail silently.

public class YieldingAndThenCrashingCriticalBackgroundService: BackgroundService
{
    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        await Task.Yield(); // Or some other async work
        throw new Exception("Oh no something really bad happened");
    }
}

We can do better.

Using BetterHostedServices you can inherit from CriticalBackgroundService instead of the regular BackgroundService.

If an uncaught error happens in a CriticalBackgroundService it will be logged, and it will crash the application.

You can use it like this:

public class YieldingAndThenCrashingBackgroundService: CriticalBackgroundService
{
    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        await Task.Yield(); // Or some other async work
        throw new Exception("Oh no something really bad happened");
    }
    
    public YieldingAndThenCrashingBackgroundService(IApplicationEnder applicationEnder) : base(applicationEnder) { }
}

And then you can use it like any other IHostedService. E.g. inside ConfigureServices you add the following:

services.AddBetterHostedServices();
services.AddHostedService<YieldingAndThenCrashingCriticalBackgroundService>();

That's it! Your CriticalBackgroundService now stops the application if an error happens

Customizing error handling in CriticalBackgroundService

If you need to customize error logging or handle the error in another way, you can override the OnError method.

public class YieldingAndThenCrashingCriticalBackgroundService : CriticalBackgroundService
{
    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        await Task.Yield(); // Hand over control explicitly, to ensure this behaviour also works
        throw new Exception("Crash after yielding");
    }

    protected override void OnError(Exception exceptionFromExecuteAsync)
    {
        // Add your custom logging here
        this._applicationEnder.ShutDownApplication(); // or simply call base.OnError
    }

    public YieldingAndThenCrashingCriticalBackgroundService(IApplicationEnder applicationEnder) : base(applicationEnder)
    {
    }
}

AddHostedServiceAsSingleton

Hosted Services and BackgroundServices aren't part of the dependency injection container. This means that you can't get them injected into your services or controllers. If you need to do this, you can use the AddHostedServiceAsSingleton extension method on the IServiceCollection

services.AddHostedServiceAsSingleton<ISomeBackgroundService, SomeBackgroundService>();

After that, you can inject them via the DI container just like any ordinary singleton.

RunPeriodicTasks

If you simply want your BackgroundService to run a periodic tasks, there's some boilerplate you generally have to deal with. Some best-practices for using BackgroundServices to run periodic tasks are documented here - but we provide a shortcut here.

If you want to run a periodic task, implement the IPeriodicTask interface, and use the IServiceCollection.AddPeriodicTask method, like below.

public class MyPeriodicTask: IPeriodicTask
    {
        public async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            // Do some businesss logic
        }
    }
# In ConfigureServices
services.AddPeriodicTask<MyPeriodicTask>(failureMode: PeriodicTaskFailureMode.CrashApplication, timeBetweenTasks: TimeSpan.FromSeconds(5));

You can determine two different Failure Modes:

  • PeriodicTaskFailureMode.CrashApplication : If this is set and the task throws an uncaught exception, the exception will bubble up and crash the application. This is similar to how an uncaught exception inside a CriticalBackgroundService works. Use this if you do not expect your tasks to fail, and it would be unwise to continue if one did.
  • PeriodicTaskFailureMode.RetryLater : If you expect that tasks might fail occasionally, and that retrying is safe, use this method. It will log the error and run a new task after timeBetweenTasks has elapsed.

The parameter timeBetweenTasks is how long until after the completion of the previous task, the next one should start. The clock will not start ticking until the previous task has completed so there is no risk of tasks overlapping in time.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].