Skip to content

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

PackageNuGet
Extensions.HostingNugetNuget
Extensions.Hosting.SchedulingNugetNuget

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.

publicclassProgram{publicstaticvoidMain(string[]args)=>CreateHostBuilder(args).Build().Run();publicstaticIHostBuilderCreateHostBuilder(string[]args)=>Host.CreateDefaultBuilder(args).ConfigureServices((hostContext,services)=>{services.UseStartup<Startup>();// Extension used here});}// Startup class should inherit IStartup interfacepublicclassStartup:IStartup{publicStartup(IConfigurationconfiguration)=>Configuration=configuration;publicIConfigurationConfiguration{get;}publicvoidConfigureServices(IServiceCollectionservices){if(services==null){thrownewArgumentNullException(nameof(services));}// Register your services here}}

The following code demonstrates basic usage of task scheduling.

// Define a task which inherits from IScheduledTask interfacepublicclassFooTask:IScheduledTask{publicstringSchedule{get;privateset;}publicFooTask(IConfigurationconfiguration){if(configuration==null){thrownewArgumentNullException(nameof(configuration));}varschedule=configuration["Scheduling:Tasks:FooTask:Schedule"];if(string.IsNullOrWhiteSpace(schedule)){thrownewArgumentException(nameof(schedule));}Schedule=schedule;// Set the cron expression}publicasyncTaskExecuteAsync(CancellationTokencancellationToken){// Write your logic here}}publicclassStartup:IStartup{publicStartup(IConfigurationconfiguration)=>Configuration=configuration;publicIConfigurationConfiguration{get;}publicvoidConfigureServices(IServiceCollectionservices){if(services==null){thrownewArgumentNullException(nameof(services));}vardelay=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

Stars

1 star

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages