ScheduledServices 1.3.3

dotnet add package ScheduledServices --version 1.3.3                
NuGet\Install-Package ScheduledServices -Version 1.3.3                
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="ScheduledServices" Version="1.3.3" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ScheduledServices --version 1.3.3                
#r "nuget: ScheduledServices, 1.3.3"                
#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 ScheduledServices as a Cake Addin
#addin nuget:?package=ScheduledServices&version=1.3.3

// Install ScheduledServices as a Cake Tool
#tool nuget:?package=ScheduledServices&version=1.3.3                

ScheduledServices

This is a simple library that makes it easy to schedule your services. You can schedule a service to run after a delay, or to keep running with some delay between runs.

There are three classes provided:

ToggledService

Inherits BackgroundService. This gives you the ability to toggle the service on or off.

ScheduledService

Inherits ToggledService. This gives you the ability to schedule a single execution at a fixed or computed date after the program starts. This will only execute one time.

RecurringService

Inherits ScheduledService. This will execute forever by default, with a delay between executions. You can set the delay between executions in appsettings or compute it in code.

See it in action

To implement this, have your service inherit ToggleService, ScheduledService, or RecurringService. The example below shows how to configure your service to run one minute after startup and keep running every minute.

appsettings.json

{
  "Services": {
    "YourService": {
      "Enabled": true,
      "DelayBeforeExecution": "00:01:00.0",
      "DelayBetweenExecutions": "00:01:00.0"
    }
}

Program.cs

public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args)
    .ConfigureScheduledServices((host, services) =>
    {
        // schedules a service with no extension methods
        services.Configure<YourServiceOptions>(context.Configuration.GetRequiredSection($"Services:{typeof(YourService).Name}"));
            .AddSingleton<YourService>()
            .AddHostedService(services => services.GetRequiredService<YourService>());

        // schedules a service using provided extension methods
        services.ConfigureScheduledService<YourService, YourServiceOptions>().AddHostedSingleton<YourService>();

        // configuration of non-scheduled services can still use these extension methods
        // this one will use Any:Path:Here:YourService
        services.Configure<YourServiceOptions>(context.Configuration.GetCustomSection<YourService>("Any:Path:Here:"));

        // this one will use Services:YourService
        services.Configure<YourServiceOptions>(context.Configuration.GetServicesSection<YourService>());
    });

Options

public class YourServiceOptions : IRecurringServiceOptions
{
    // implement the interface, or inherit RecurringServiceOptions

    public TimeSpan DelayBeforeExecution { get; set; }
    public TimeSpan DelayBetweenExecutions { get; set; }
    public bool Enabled { get; set; }
}

Service

public class YourService : RecurringService
{
    public YourService(ILogger<YourService> logger, IOptions<YourServiceOptions> options) : base(logger, options)
    {
    }

    protected override async Task ExecuteScheduledTaskAsync(CancellationToken cancellationToken)
    {
        // Errors will be logged. They will not crash the program, 
        // nor prevent recurring services from running again.
        // To change this behaviour, override OnExecutionError.
    }

    protected override ValueTask<TimeSpan> GetDelayBeforeExecutionAsync(CancellationToken cancellationToken)
    {
        // Optionally you can compute the delay here.
        // This controls when the service FIRST executes after the program starts.
        // It overrides DelayBeforeExecution from the IConfiguration.
    }

    protected override ValueTask<TimeSpan> GetDelayBetweenExecutionAsync(CancellationToken cancellationToken)
    {
        // This controls the delay BETWEEN executions in a recurring service.
        // It overrides DelayBetweenExecutions from the IConfiguration
    }
}
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on ScheduledServices:

Package Downloads
CocApi.Cache

Caches response from the Clash of Clans API.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.3.3 1,327 10/9/2022
1.3.2 717 10/2/2022
1.3.1 421 10/2/2022
1.3.0 415 9/3/2022
1.2.0 448 5/15/2022
1.1.1 454 5/9/2022
1.1.0 432 5/7/2022
1.0.0 463 1/17/2022
1.0.0-preview0.0.13 129 1/17/2022
1.0.0-preview0.0.12 138 1/17/2022
1.0.0-preview0.0.11 131 1/17/2022
1.0.0-preview0.0.10 142 1/2/2022
1.0.0-preview0.0.9 143 1/1/2022
1.0.0-preview0.0.7 347 12/13/2021
1.0.0-preview0.0.6 337 12/13/2021
1.0.0-preview0.0.5 326 12/13/2021
1.0.0-preview0.0.4 314 12/12/2021
1.0.0-preview0.0.3 149 12/12/2021
1.0.0-preview0.0.2 134 12/12/2021
1.0.0-preview0.0.1 138 12/12/2021