https://github.com/ffernandolima/extensions-hosting
Exposes Startup extensions to be used with generic host. Also, it exposes task scheduling utilities based on cron expressions.
https://github.com/ffernandolima/extensions-hosting
Last synced: 11 months ago
JSON representation
Exposes Startup extensions to be used with generic host. Also, it exposes task scheduling utilities based on cron expressions.
- Host: GitHub
- URL: https://github.com/ffernandolima/extensions-hosting
- Owner: ffernandolima
- License: mit
- Created: 2020-07-04T18:03:38.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2021-08-26T18:06:23.000Z (almost 5 years ago)
- Last Synced: 2025-07-02T13:19:50.438Z (12 months ago)
- Language: C#
- Homepage:
- Size: 1.73 MB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# extensions-hosting
Exposes Startup extensions to be used with generic host.
Also, it exposes task scheduling utilities based on cron expressions.
[](https://github.com/ffernandolima/extensions-hosting/actions/workflows/build-and-publish.yml?branch=master)
| Package | NuGet |
| ------- | ----- |
| Extensions.Hosting | [ ](https://www.nuget.org/packages/Extensions.Hosting/2.3.0) |
| Extensions.Hosting.Scheduling | [ ](https://www.nuget.org/packages/Extensions.Hosting.Scheduling/2.3.0) |
## 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.
```C#
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(); // 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.
```C#
// 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("Scheduling:Delay");
services.AddSingleton();
services.AddScheduler((sender, args) => args.SetObserved(), delay);
}
}
```
appsettings.json:
```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.
