HeffernanTech.Services.HostedQueueProcessor
24.156.1320
dotnet add package HeffernanTech.Services.HostedQueueProcessor --version 24.156.1320
NuGet\Install-Package HeffernanTech.Services.HostedQueueProcessor -Version 24.156.1320
<PackageReference Include="HeffernanTech.Services.HostedQueueProcessor" Version="24.156.1320" />
paket add HeffernanTech.Services.HostedQueueProcessor --version 24.156.1320
#r "nuget: HeffernanTech.Services.HostedQueueProcessor, 24.156.1320"
// Install HeffernanTech.Services.HostedQueueProcessor as a Cake Addin #addin nuget:?package=HeffernanTech.Services.HostedQueueProcessor&version=24.156.1320 // Install HeffernanTech.Services.HostedQueueProcessor as a Cake Tool #tool nuget:?package=HeffernanTech.Services.HostedQueueProcessor&version=24.156.1320
HeffernanTech.Services.HostedQueueProcessor
Overview
The HeffernanTech.Services.HostedQueueProcessor
package provides a hosted service for processing items in a queue using a specified processor. This package is designed to be used with dependency injection in an ASP.NET Core application.
Features
- Thread-Safe Queue Management: Uses a concurrent queue to manage items safely across multiple threads.
- Background Processing: Processes queue items in the background using a hosted service.
- Configurable Concurrency: Allows configuration of the maximum number of concurrent worker tasks.
- Integration with Dependency Injection: Easily integrates with the ASP.NET Core dependency injection system.
Installation
To install the HeffernanTech.Services.HostedQueueProcessor
package, use the following command in your .NET project:
dotnet add package HeffernanTech.Services.HostedQueueProcessor
Usage
Setup
Define a Queue Processor: Implement the
IQueueProcessor<T>
interface to define how to process each item in the queue.public class MyQueueProcessor : IQueueProcessor<MyItem> { public async Task Process(MyItem item, CancellationToken cancellationToken) { // Your processing logic here } }
Configure Services: In your
Startup.cs
or wherever you configure services, add the hosted queue worker to the service collection.public void ConfigureServices(IServiceCollection services) { services.AddSingleton<IQueueProcessor<MyItem>, MyQueueProcessor>(); services.AddSingleton<IQueueProvider<MyItem>, ConcurrentQueueProvider<MyItem>>(); services.AddQueueWorker<MyItem>(options => { options.MaxWorkerTasks = 4; // Set the maximum number of concurrent worker tasks }); }
Example
Below is a complete example of setting up and using the hosted queue processor in an ASP.NET Core application.
Implement the Queue Processor:
public class MyQueueProcessor : IQueueProcessor<MyItem> { public async Task Process(MyItem item, CancellationToken cancellationToken) { // Simulate some work await Task.Delay(1000, cancellationToken); Console.WriteLine($"Processed item: {item.Name}"); } } public class MyItem { public string Name { get; set; } }
Configure the Services:
public class Startup { public void ConfigureServices(IServiceCollection services) { services.AddSingleton<IQueueProcessor<MyItem>, MyQueueProcessor>(); services.AddSingleton<IQueueProvider<MyItem>, ConcurrentQueueProvider<MyItem>>(); services.AddHostedQueueWorker<MyItem>(options => { options.MaxWorkerTasks = 4; // Configure the maximum number of worker tasks }); // Other service configurations } public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { // Application configuration } }
Enqueue Items: Enqueue items to be processed by the queue worker.
public class MyController : ControllerBase { private readonly IQueueProvider<MyItem> _queueProvider; public MyController(IQueueProvider<MyItem> queueProvider) { _queueProvider = queueProvider; } [HttpPost("enqueue")] public IActionResult Enqueue([FromBody] MyItem item) { _queueProvider.Enqueue(item); return Ok("Item enqueued"); } }
Running the Application
Start your ASP.NET Core application. The QueueWorker
will run in the background, processing items from the queue using the defined MyQueueProcessor
.
Options
QueueWorkerOptions
- MaxWorkerTasks: The maximum number of worker tasks that can run concurrently. The default value is the number of processors available on the machine.
public class QueueWorkerOptions
{
public int MaxWorkerTasks { get; set; } = Environment.ProcessorCount;
}
License
This project is licensed under the MIT License.
Contributing
Contributions are welcome! Please open an issue or submit a pull request for any improvements or bug fixes.
For more information, please refer to the official documentation and API reference.
Happy coding!
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. 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. |
-
net6.0
- Microsoft.Extensions.Hosting.Abstractions (>= 6.0.0)
- Microsoft.Extensions.Logging.Abstractions (>= 6.0.0)
- Microsoft.Extensions.Options (>= 6.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
24.156.1320 | 137 | 6/4/2024 |
We are excited to announce the initial release of HeffernanTech.Services.HostedQueueProcessor. This package provides a robust framework for background processing of queued items in ASP.NET Core applications.
Key Features:
* Thread-Safe Queue Management: Uses ConcurrentQueue to ensure thread-safe operations for queue management.
* Background Processing: Implements a hosted service for processing items from a queue in the background, leveraging IHostedService.
* Configurable Concurrency: Allows configuration of the maximum number of concurrent worker tasks via QueueWorkerOptions.
* Easy Integration with Dependency Injection: Seamlessly integrates with ASP.NET Core's dependency injection system for easy setup and configuration.
* Extensible Queue Processor: Allows custom implementation of the IQueueProcessor<T> interface for processing queue items.