Aiursoft.Canon
8.0.6
dotnet add package Aiursoft.Canon --version 8.0.6
NuGet\Install-Package Aiursoft.Canon -Version 8.0.6
<PackageReference Include="Aiursoft.Canon" Version="8.0.6" />
paket add Aiursoft.Canon --version 8.0.6
#r "nuget: Aiursoft.Canon, 8.0.6"
// Install Aiursoft.Canon as a Cake Addin #addin nuget:?package=Aiursoft.Canon&version=8.0.6 // Install Aiursoft.Canon as a Cake Tool #tool nuget:?package=Aiursoft.Canon&version=8.0.6
Aiursoft Canon
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 | Versions 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. |
-
net8.0
- Aiursoft.Scanner.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Caching.Memory (>= 8.0.1)
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 | 151 | 10/27/2024 |
8.0.5 | 196 | 10/9/2024 |
8.0.4 | 615 | 6/24/2024 |
8.0.3 | 173 | 6/12/2024 |
8.0.2 | 131 | 6/6/2024 |
8.0.1 | 1,277 | 2/19/2024 |
8.0.0 | 425 | 2/19/2024 |
7.0.8 | 377 | 2/2/2024 |
7.0.7 | 169 | 1/30/2024 |
7.0.6 | 2,193 | 11/22/2023 |
7.0.5 | 695 | 11/2/2023 |
7.0.4 | 1,049 | 9/21/2023 |
7.0.3 | 125 | 9/21/2023 |
7.0.2 | 328 | 9/13/2023 |
7.0.1 | 139 | 9/13/2023 |
7.0.0 | 513 | 9/5/2023 |
6.0.13 | 263 | 8/20/2023 |
6.0.12 | 1,129 | 6/24/2023 |
6.0.11 | 360 | 6/23/2023 |
6.0.10 | 169 | 6/19/2023 |
6.0.9 | 566 | 6/18/2023 |
6.0.8 | 170 | 6/18/2023 |
6.0.7 | 150 | 6/18/2023 |
6.0.6 | 170 | 6/15/2023 |
6.0.5 | 160 | 6/15/2023 |
6.0.4 | 168 | 6/10/2023 |
6.0.1 | 187 | 6/7/2023 |
6.0.0 | 168 | 6/7/2023 |