Skip to content

Exposes Startup extensions to be used with generic host. Also, it exposes task scheduling utilities based on cron expressions.

License

Notifications You must be signed in to change notification settings

ffernandolima/extensions-hosting

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

extensions-hosting

Exposes Startup extensions to be used with generic host.

Also, it exposes task scheduling utilities based on cron expressions.

build-and-publish Workflow Status

Package NuGet
Extensions.Hosting Nuget Nuget
Extensions.Hosting.Scheduling Nuget Nuget

Installation

It is available on Nuget.

Install-Package Extensions.Hosting -Version 2.3.0
Install-Package Extensions.Hosting.Scheduling -Version 2.3.0

P.S.: There's no dependency between the packages. Which one has its own features.

Usage

The following code demonstrates basic usage of Startup extensions.

public class Program
{
   public static void Main(string[] args) => CreateHostBuilder(args).Build().Run();

   public static IHostBuilder CreateHostBuilder(string[] args) =>
       Host.CreateDefaultBuilder(args)
           .ConfigureServices((hostContext, services) =>
           {
               services.UseStartup<Startup>(); // Extension used here
           });
}

// Startup class should inherit IStartup interface
public class Startup : IStartup
{
   public Startup(IConfiguration configuration) => Configuration = configuration;

   public IConfiguration Configuration { get; }

   public void ConfigureServices(IServiceCollection services)
   {
       if (services == null)
       {
           throw new ArgumentNullException(nameof(services));
       }
       
       // Register your services here
   }
}

The following code demonstrates basic usage of task scheduling.

// Define a task which inherits from IScheduledTask interface
public class FooTask : IScheduledTask
{
   public string Schedule { get; private set; }

   public FooTask(IConfiguration configuration)
   {
       if (configuration == null)
       {
           throw new ArgumentNullException(nameof(configuration));
       }

       var schedule = configuration["Scheduling:Tasks:FooTask:Schedule"];

       if (string.IsNullOrWhiteSpace(schedule))
       {
           throw new ArgumentException(nameof(schedule));
       }

       Schedule = schedule; // Set the cron expression
   }
   
   public async Task ExecuteAsync(CancellationToken cancellationToken)
   {
       // Write your logic here
   }
}
 
public class Startup : IStartup
{
   public Startup(IConfiguration configuration) => Configuration = configuration;

   public IConfiguration Configuration { get; }

   public void ConfigureServices(IServiceCollection services)
   {
       if (services == null)
       {
           throw new ArgumentNullException(nameof(services));
       }
       
       var delay = configuration?.GetValue<TimeSpan>("Scheduling:Delay");
       
       services.AddSingleton<IScheduledTask, FooTask>();
       services.AddScheduler((sender, args) => args.SetObserved(), delay);
   }
}

appsettings.json:

"Scheduling": {
    "Delay": "00:00:30",
    "Tasks": {
      "FooTask": {
        "Schedule": "* * * * *"
      }
    }
  }

Support / Contributing

If you want to help with the project, feel free to open pull requests and submit issues.

Donate

If you would like to show your support for this project, then please feel free to buy me a coffee.

Buy Me A Coffee

About

Exposes Startup extensions to be used with generic host. Also, it exposes task scheduling utilities based on cron expressions.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages