Aiursoft.Canon 7.0.7

There is a newer version of this package available.
See the version list below for details.
dotnet add package Aiursoft.Canon --version 7.0.7                
NuGet\Install-Package Aiursoft.Canon -Version 7.0.7                
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="7.0.7" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Aiursoft.Canon --version 7.0.7                
#r "nuget: Aiursoft.Canon, 7.0.7"                
#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=7.0.7

// Install Aiursoft.Canon as a Cake Tool
#tool nuget:?package=Aiursoft.Canon&version=7.0.7                

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 net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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 (5)

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

Package Downloads
Aiursoft.AiurProtocol

API Communication practice

Aiursoft.GitRunner

Nuget package of 'GitRunner' provided by Aiursoft

Aiursoft.NiBot.Core

Nuget package of 'Core' 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.6 295 10/27/2024
8.0.5 222 10/9/2024
8.0.4 633 6/24/2024
8.0.3 175 6/12/2024
8.0.2 134 6/6/2024
8.0.1 1,325 2/19/2024
8.0.0 442 2/19/2024
7.0.8 380 2/2/2024
7.0.7 171 1/30/2024
7.0.6 2,209 11/22/2023
7.0.5 697 11/2/2023
7.0.4 1,053 9/21/2023
7.0.3 127 9/21/2023
7.0.2 330 9/13/2023
7.0.1 141 9/13/2023
7.0.0 515 9/5/2023
6.0.13 267 8/20/2023
6.0.12 1,133 6/24/2023
6.0.11 362 6/23/2023
6.0.10 171 6/19/2023
6.0.9 570 6/18/2023
6.0.8 172 6/18/2023
6.0.7 152 6/18/2023
6.0.6 172 6/15/2023
6.0.5 162 6/15/2023
6.0.4 170 6/10/2023
6.0.1 189 6/7/2023
6.0.0 170 6/7/2023