SimpleQueue.NET 1.0.0

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

// Install SimpleQueue.NET as a Cake Tool
#tool nuget:?package=SimpleQueue.NET&version=1.0.0                

SimpleQueue

Overview

SimpleQueue is a lightweight library for managing message queues in .NET applications. It provides a straightforward and flexible way to send, receive, and process messages asynchronously. The library supports handling different event types and integrates seamlessly with dependency injection and logging in ASP.NET Core applications.

Features

  • Define and handle various types of events (Added, Updated, Deleted).
  • Abstract base classes and interfaces for defining messages and message handlers.
  • Easy integration with .NET's IHostedService for background processing.
  • Built-in support for logging and dependency injection in ASP.NET Core applications.
  • Supports both synchronous and asynchronous message sending and handling.
  • Provides a consistent and simple API for queue management.

Installation

Install the SimpleQueue package via NuGet:

dotnet add package SimpleQueue.NET

Usage

Define a Message

Create a class that represents the message you want to send. The message class should inherit from the BaseMessage base class and define any properties that are required for the message.

using SimpleQueue;

public class MyMessage : BaseMessage
{
	public string Content { get; set; }
}

Define a Message Handler

Create a class that implements the IMessageHandler<T> interface, where T is the message type. Implement the HandleAddedAsync, HandleUpdatedAsync, HandleDeletedAsync methods to define how the message should be processed.

using SimpleQueue;
using System.Threading.Tasks;

public class MyMessageHandler : MessageConsumerBase<MyMessage>
{
    public override Task HandleAddedAsync(MyMessage message)
    {
        // Handle the added message
        Console.WriteLine($"Added: {message.Content}");
        return Task.CompletedTask;
    }

    public override Task HandleUpdatedAsync(MyMessage message)
    {
        // Handle the updated message
        Console.WriteLine($"Updated: {message.Content}");
        return Task.CompletedTask;
    }

    public override Task HandleDeletedAsync(MyMessage message)
    {
        // Handle the deleted message
        Console.WriteLine($"Deleted: {message.Content}");
        return Task.CompletedTask;
    }
}

Configure Message Queue

Implement the IMessageQueue interface for your specific message queue backend.

using SimpleQueue;
using System;
using System.Threading.Tasks;

public class InMemoryMessageQueue<T> : IMessageQueue<T> where T : IMessage
{
    private readonly ConcurrentQueue<T> _queue = new();

    public void Send(T message, EventTypes eventType)
    {
        _queue.Enqueue(message);
    }

    public Task SendAsync(T message, EventTypes eventType)
    {
        Send(message, eventType);
        return Task.CompletedTask;
    }

    public void Receive(Func<T, Task> handleMessage)
    {
        Task.Run(async () =>
        {
            while (true)
            {
                if (_queue.TryDequeue(out var message))
                {
                    await handleMessage(message);
                }
                await Task.Delay(100); // Adding a small delay to prevent busy-waiting
            }
        });
    }

    public void DeleteQueue()
    {
        _queue.Clear();
    }

    public void CloseConnection()
    {
    }
}

Register Services

Register the message queue and message handler in the DI container.

using Microsoft.Extensions.DependencyInjection;
using SimpleQueue;

public class Startup
{
	public void ConfigureServices(IServiceCollection services)
	{
            services.AddSingleton<IMessageQueue<MyMessage>, InMemoryMessageQueue<MyMessage>>();
            services.AddSingleton<IMessageHandler<MyMessage>, MyMessageHandler>();
            services.AddSingleton<MessageHandler<MyMessage>>();
            services.AddHostedService<MessageQueueHostedService<MyMessage>>();
	}
}

Run the Application

Run your ASP.NET Core application, and the message queue will start processing messages in the background.

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. Please make sure to update tests as appropriate.

License

This project is licensed under the MIT License. See the MIT file for details.

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 (2)

Showing the top 2 NuGet packages that depend on SimpleQueue.NET:

Package Downloads
SimpleQueue.NET.RabbitMQ

SimpleQueue implementation for RabbitMQ.

SimpleQueue.NET.InMemory

SimpleQueue implementation for InMemory.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.1.1 111 7/28/2024
1.1.0 119 7/22/2024
1.0.0 137 7/16/2024