SimpleApi.StatelessBackgroundServices 1.0.10

dotnet add package SimpleApi.StatelessBackgroundServices --version 1.0.10
NuGet\Install-Package SimpleApi.StatelessBackgroundServices -Version 1.0.10
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SimpleApi.StatelessBackgroundServices" Version="1.0.10" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SimpleApi.StatelessBackgroundServices --version 1.0.10
#r "nuget: SimpleApi.StatelessBackgroundServices, 1.0.10"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install SimpleApi.StatelessBackgroundServices as a Cake Addin
#addin nuget:?package=SimpleApi.StatelessBackgroundServices&version=1.0.10

// Install SimpleApi.StatelessBackgroundServices as a Cake Tool
#tool nuget:?package=SimpleApi.StatelessBackgroundServices&version=1.0.10

SimpleApi.StatelessBackgroundServices

SimpleApi.StatelessBackgroundServices is a library for creating and managing stateless background services in ASP.NET Core.

Stateless background services are services that perform some work without saving state between calls. They are useful for periodic tasks, such as clearing cache, sending notifications or processing message queues.

How to use

To use the library, you need to do the following:

  • Create a class that implements the IStatelessWorker interface. This class should contain the logic of your background work in the DoWork method.
  • Register your stateless background service in the dependency injection container using the AddStatelessWorker extension method. You can configure the options of your service, such as pause and start delay, using the setupAction parameter.
  • Get access to your stateless background service through the StatelessWorkerManager class. You can use this class to change the timer delay or force the execution of your service.

Example

Here is an example of a stateless background service that sends an email every 10 minutes:

using Microsoft.Extensions.DependencyInjection;
using SimpleApi.StatelessBackgroundServices;

// Define a class that implements IStatelessWorker
public class EmailSender : IStatelessWorker
{
    private readonly IEmailService _emailService;

    // Inject any dependencies you need
    public EmailSender(IEmailService emailService)
    {
        _emailService = emailService;
    }

    // Implement the DoWork method with your background logic
    public async Task DoWork()
    {
        await _emailService.SendEmailAsync("Hello world!");
    }
}

// Register your stateless background service in Startup.cs
public void ConfigureServices(IServiceCollection services)
{
    // Add any dependencies you need
    services.AddTransient<IEmailService, EmailService>();

    // Add your stateless background service with options
    services.AddStatelessWorker<EmailSender>(opts =>
    {
        opts.Pause = TimeSpan.FromMinutes(10); // Set the pause between calls
        opts.StartDelay = TimeSpan.FromSeconds(30); // Set the delay before the first call
    });
}

// Get access to your stateless background service in a controller or anywhere else
public class HomeController : Controller
{
    private readonly StatelessWorkerManager _workerManager;

    // Inject the StatelessWorkerManager
    public HomeController(StatelessWorkerManager workerManager)
    {
        _workerManager = workerManager;
    }

    public IActionResult Index()
    {
        // Get your stateless background service by worker type
        var emailSender = _workerManager.GetByWorker<EmailSender>();

        // Change the timer delay if you want
        emailSender.ChangeTimerDelay(TimeSpan.FromMinutes(5));

        // Force the execution of your service if you want
        emailSender.ForceToken.Cancel();

        return View();
    }
}
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.10 176 7/24/2023
1.0.8 406 8/19/2022
1.0.7 384 7/7/2022
1.0.6 397 5/27/2022
1.0.5 353 2/17/2021
1.0.3 371 12/24/2020
1.0.2 487 7/23/2020
1.0.1 403 6/8/2020
1.0.0 425 6/8/2020