Aiursoft.Canon 8.0.1

dotnet add package Aiursoft.Canon --version 8.0.1
NuGet\Install-Package Aiursoft.Canon -Version 8.0.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Aiursoft.Canon" Version="8.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Aiursoft.Canon --version 8.0.1
#r "nuget: Aiursoft.Canon, 8.0.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Aiursoft.Canon as a Cake Addin
#addin nuget:?package=Aiursoft.Canon&version=8.0.1

// Install Aiursoft.Canon as a Cake Tool
#tool nuget:?package=Aiursoft.Canon&version=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.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (4)

Showing the top 4 NuGet packages that depend on Aiursoft.Canon:

Package Downloads
Aiursoft.AiurProtocol

API Communication practice

Aiursoft.GitRunner

Nuget package of 'GitRunner' provided by Aiursoft

Crawler.Shared

Package Description

Aiursoft.DotDownload.Core

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
8.0.1 413 2/19/2024
8.0.0 189 2/19/2024
7.0.8 258 2/2/2024
7.0.7 123 1/30/2024
7.0.6 1,463 11/22/2023
7.0.5 505 11/2/2023
7.0.4 735 9/21/2023
7.0.3 101 9/21/2023
7.0.2 251 9/13/2023
7.0.1 111 9/13/2023
7.0.0 365 9/5/2023
6.0.13 217 8/20/2023
6.0.12 874 6/24/2023
6.0.11 275 6/23/2023
6.0.10 145 6/19/2023
6.0.9 466 6/18/2023
6.0.8 145 6/18/2023
6.0.7 122 6/18/2023
6.0.6 147 6/15/2023
6.0.5 134 6/15/2023
6.0.4 146 6/10/2023
6.0.1 161 6/7/2023
6.0.0 145 6/7/2023