Aiursoft.Canon 8.0.1

Aiursoft Canon

MIT licensed Pipeline stat Test Coverage NuGet version (Aiursoft.Canon) ManHours

Aiursoft Canon is used to implement dependency-based Fire and Forget for .NET projects, which means starting a heavy task without waiting for it to complete or caring about its success, and continuing with subsequent logic.

This is very useful in many scenarios to avoid blocking, such as when sending emails.

Why this project

The traditional way to fire and forget in C# is:

_ = Task.Run(() =>
{
    // Do something heavy
});

However, if your task depends on something like Entity Framework, it's hard to control it's life cycle.

Installation

First, install Aiursoft.Canon to your ASP.NET Core project from nuget.org:

dotnet add package Aiursoft.Canon

Add the service to your IServiceCollection in StartUp.cs:

using Aiursoft.Canon;

services.AddTaskCanon();

Your project will get:

// An easier to use Cache service. Allow you to execute some code with a key to cache it.
services.AddTransient<CacheService>();

// A transient service to retry a task with limited times.
services.AddTransient<RetryEngine>();

// A transient service to replace 'Task.WhenAll()'. Start all tasks with limited concurrency.
services.AddTransient<CanonPool>();

// Simple Fire and forget service that runs immediately. (No concurrency limitation)
services.AddSingleton<CanonService>();

// Application singleton background job queue. (Default task concurrency is 8)
services.AddSingleton<CanonQueue>();

// A watch service to measure how much time a task used.
services.AddTransient<WatchService>();

How to use Aiursoft.CanonQueue

Then, you can inject CanonService to your controller. And now, you can fire and forget your task like this:

public class YourController : Controller
{
    private readonly CanonQueue _canonQueue;

    public OAuthController(CanonQueue canonQueue)
    {
        _canonQueue = canonQueue;
    }

    public IActionResult Send()
    {
        // Send an confirmation email here:
        _canonQueue.QueueWithDependency<EmailSender>(async (sender) =>
        {
            await sender.SendAsync(); // Which may be slow. The service 'EmailSender' will be kept alive!
        });
        
        return Ok();
    }
}

That's it.

How to use Aiursoft.CanonPool

You can also put all your tasks to a task queue, and run those tasks with a limit of concurrency:

Inject CanonPool first:

private readonly EmailSender _sender;
private readonly CanonPool _canonPool;

public DemoController(
    EmailSender sender,
    CanonPool canonPool)
{
    _sender = sender;
    _canonPool = canonPool;
}
foreach (var user in users)
{
    _canonPool.RegisterNewTaskToPool(async () =>
    {
        await sender.SendAsync(user); // Which may be slow.
    });
}

await _canonPool.RunAllTasksInPoolAsync(); // Execute tasks in pool, running tasks should be max at 8.

That is far better than this:

var tasks = new List<Task>();
foreach (var user in users)
{
    tasks.Add(Task.Run(() => sender.SendAsync(user)));
}
await Task.WhenAll(tasks); // It may start too many tasks and block your remote service like email sender.

Now you can control the concurrency of your tasks. For example, you can start 16 tasks at the same time:

await _canonQueue.RunTasksInQueue(16); // Start the engine with 16 concurrency and wait for all tasks to complete.

That helps you to avoid blocking your Email sender or database with too many tasks.

Showing the top 20 packages that depend on Aiursoft.Canon.

Packages Downloads
Aiursoft.GitRunner
Nuget package of 'GitRunner' provided by Aiursoft
119
Aiursoft.AiurProtocol
API Communication practice
82
Aiursoft.AiurProtocol
API Communication practice
62
Aiursoft.AiurProtocol
API Communication practice
43
Aiursoft.GitRunner
Nuget package of 'GitRunner' provided by Aiursoft
4
Aiursoft.XelNaga
Some basic C# tools for everyone and powers Aiursoft
3
Aiursoft.AiurProtocol
API Communication practice
3
Aiursoft.GitRunner
Nuget package of 'GitRunner' provided by Aiursoft
3
Aiursoft.GitRunner
Nuget package of 'GitRunner' provided by Aiursoft
2
Aiursoft.AiurProtocol
API Communication practice
2

Version Downloads Last updated
8.0.1 181 03/25/2024
8.0.0 43 03/25/2024
7.0.8 2 04/04/2024
7.0.7 1 04/29/2024
7.0.6 60 03/26/2024
7.0.5 1 04/29/2024
7.0.4 1 04/29/2024
7.0.3 1 04/29/2024
7.0.2 2 04/20/2024
7.0.1 1 04/29/2024
7.0.0 2 04/29/2024
6.0.13 1 04/29/2024
6.0.12 2 04/29/2024
6.0.11 1 04/29/2024
6.0.10 1 04/29/2024
6.0.9 2 04/06/2024
6.0.8 2 04/29/2024
6.0.7 1 04/29/2024
6.0.6 2 04/23/2024
6.0.5 1 04/29/2024
6.0.4 2 04/29/2024
6.0.1 1 04/29/2024
6.0.0 1 04/29/2024