This is a simple library written in C# .NET 6 for adding startup tasks using hosted services.
Add the nuget package reference to your project
- Using dotnet-cli
dotnet add package StartupTasks
- Using Package Manager
Install-Package StartupTasks
Create as class that implements IStartupTask
public class MyStartupTask : IStartupTask
{
public Task RunAsync(CancellationToken cancellationToken)
{
// Startup logic goes here
return Task.CompletedTask;
}
}
Register startup tasks with IServiceCollection
and add your startup task. Code block below shows common usage scenarios.
using StartupTasks;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddStartupTasks()
// Adds a startup task that runs first.
.Add<MyStartupTask>();
// Adds another startup task that runs second.
.Add<AnotherStartupTask>();
// Adds a startup task that runs in parlallel
// and begins before the sequential tasks are
// executed.
.Add<ParallelStartupTask>(runInParllel: true)
// Adds a startup task without a class file.
// Delegate takes a CancellationToken as parameter
// and returns a Task.
.AddAction(async (cancellationToken) =>
await Console.out.WriteLineAsync("Let's begin", cancellationToken),
runInParallel: true)
var app = builder.Build();
app.Run();
- Add startup tasks executed in hosted services
- Add startup task health check report after all have ran
- Add option to verification of registered tasks can be resolved at before execution
- Add startup groups so that groups of startup tasks can run sequentially within it's group but in parallel with other groups
See the open issues for a full list of proposed features (and known issues).
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
Distributed under the MIT License. See LICENSE
for more information.
Albin Ma - ama@almatek.io
Project Link: https://github.com/almatek/dotnet-startup-tasks