SimpleQueue.NET.InMemory 1.1.1

dotnet add package SimpleQueue.NET.InMemory --version 1.1.1                
NuGet\Install-Package SimpleQueue.NET.InMemory -Version 1.1.1                
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.InMemory" Version="1.1.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SimpleQueue.NET.InMemory --version 1.1.1                
#r "nuget: SimpleQueue.NET.InMemory, 1.1.1"                
#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.InMemory as a Cake Addin
#addin nuget:?package=SimpleQueue.NET.InMemory&version=1.1.1

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

SimpleQueue.InMemory

SimpleQueue.InMemory is a simple in-memory message queue implementation designed for .NET applications. It provides a lightweight and easy-to-use mechanism for sending, receiving, and processing messages within the application without the need for external dependencies like RabbitMQ or Kafka.

Features

  • In-memory storage: Keeps messages in memory using a concurrent queue.
  • Asynchronous message handling: Supports asynchronous sending and receiving of messages.
  • Logging: Integrated with Microsoft.Extensions.Logging for easy logging.
  • Message consumers: Handles messages based on their event types (Added, Updated, Deleted).

Usage

Setup

To configure the in-memory message queue, use the RegisterQueueHandlersAndServices method in your Startup class or wherever you configure your services.

using Microsoft.Extensions.DependencyInjection;
using SimpleQueue.InMemory;

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        var assemblies = AppDomain.CurrentDomain.GetAssemblies();
        services.RegisterQueueHandlersAndServices(assemblies);
    }
}

Define Messages

Create a message class that inherits from BaseMessage:

using SimpleQueue;

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

Implement Message Handlers

Implement the IMessageHandler<T> interface to handle your messages:

using SimpleQueue;
using System.Threading.Tasks;

public class MyMessageHandler : IMessageHandler<MyMessage>
{
    public Task HandleAddedAsync(MyMessage message)
    {
        // Handle added message
        return Task.CompletedTask;
    }

    public Task HandleUpdatedAsync(MyMessage message)
    {
        // Handle updated message
        return Task.CompletedTask;
    }

    public Task HandleDeletedAsync(MyMessage message)
    {
        // Handle deleted message
        return Task.CompletedTask;
    }
}

Send Messages

Inject the IMessageQueue<MyMessage> into your service or controller and use it to send messages:

using SimpleQueue;
using System.Threading.Tasks;

public class MyService
{
    private readonly IMessageQueue<MyMessage> _messageQueue;

    public MyService(IMessageQueue<MyMessage> messageQueue)
    {
        _messageQueue = messageQueue;
    }

    public Task SendMessageAsync()
    {
        var message = new MyMessage { Content = "Hello, World!", EventType = EventTypes.Added };
        return _messageQueue.SendAsync(message, message.EventType);
    }
}

Receive Messages

The message queue will automatically start receiving messages and pass them to the appropriate handlers based on their event types.

Logging

SimpleQueue.InMemory uses Microsoft.Extensions.Logging for logging. Make sure to configure logging in your application to see the log messages.

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

public class Program
{
    public static void Main(string[] args)
    {
        var host = Host.CreateDefaultBuilder(args)
            .ConfigureServices((context, services) =>
            {
                services.RegisterQueueHandlersAndServices();
                services.AddLogging(configure => configure.AddConsole());
            })
            .Build();

        host.Run();
    }
}

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

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
1.1.1 88 7/28/2024
1.1.0 89 7/22/2024
1.0.0 94 7/16/2024