HeffernanTech.Services.HostedQueueProcessor is a C# package designed for ASP.NET Core applications, facilitating background processing of queued items. With thread-safe queue management using ConcurrentQueue, it ensures safe operations across multiple threads. Leveraging IHostedService, it processes items in the background, configurable with QueueWorkerOptions for maximum concurrency. Integrated with ASP.NET Core's dependency injection, it offers seamless setup. This extensible package allows custom queue processors via the IQueueProcessor<T> interface. Enhance your application's efficiency with HeffernanTech.Services.HostedQueueProcessor.
$ dotnet add package HeffernanTech.Services.HostedQueueProcessorThe 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.
To install the HeffernanTech.Services.HostedQueueProcessor package, use the following command in your .NET project:
dotnet add package HeffernanTech.Services.HostedQueueProcessor
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
});
}
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");
}
}
Start your ASP.NET Core application. The QueueWorker will run in the background, processing items from the queue using the defined MyQueueProcessor.
QueueWorkerOptionspublic class QueueWorkerOptions
{
public int MaxWorkerTasks { get; set; } = Environment.ProcessorCount;
}
This project is licensed under the MIT License.
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!