Infinity.Toolkit.Messaging
1.0.0
dotnet add package Infinity.Toolkit.Messaging --version 1.0.0
NuGet\Install-Package Infinity.Toolkit.Messaging -Version 1.0.0
<PackageReference Include="Infinity.Toolkit.Messaging" Version="1.0.0" />
paket add Infinity.Toolkit.Messaging --version 1.0.0
#r "nuget: Infinity.Toolkit.Messaging, 1.0.0"
// Install Infinity.Toolkit.Messaging as a Cake Addin #addin nuget:?package=Infinity.Toolkit.Messaging&version=1.0.0 // Install Infinity.Toolkit.Messaging as a Cake Tool #tool nuget:?package=Infinity.Toolkit.Messaging&version=1.0.0
Infinity.Toolkit.Messaging
Infinity Toolkit Messaging is a no frills messaging lightweight library with a super simple API. It can be used to send messages between different parts of the application. The library is built with simplicity in mind and is designed to be easy to use and easy to understand. The nomenclature is following Async API and the library is built with the idea of being able to easily integrate with any messaging system such as Azure Service Bus, RabbitMQ, Kafka etc.
Features
- An Async API inspired API.
- An In-Memory message bus
- Azure Service Bus integration
Quick Start
To get started with Infinity.Toolkit.Messaging follow these steps:
- Look at the sample project in the repository. The sample project found here MessagingSample is a simple web api that demonstrates how to use the library.
- Create a new project and integrate the library.
Let's look create a new project and integrate the library.
- Create a new web api project using the dotnet cli.
dotnet new webapi -n MyWebApi
- Add the Infinity.Toolkit.Messaging package to the project.
dotnet add package Infinity.Toolkit.Messaging
- Start by creating a message class that will be sent between different parts of the application. For this sample let's reuse the WeatherForecast class from the sample project.
internal record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
{
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
}
- Create a message handler that will handle the message. The message handler should implement the
IMessageHandler<TMessage>
interface.
internal class WeatherForecastHandler : IMessageHandler<WeatherForecast>
{
public Task HandleAsync(WeatherForecast message, CancellationToken cancellationToken)
{
Console.WriteLine($"Received message: {message.Summary}");
return Task.CompletedTask;
}
}
- Now it's time to add and configure the message bus. First add the message bus to the services collection in the
Program.cs
file.
using Infinity.Toolkit.Messaging;
var builder = WebApplication.CreateBuilder(args);
builder.AddInfinityMessaging();
var app = builder.Build();
...
- Good, now the message bus is added to the services collection. Next, we need to configure a broker that will be used to send and receive messages. In this sample, we will use the in-memory message broker.
builder.AddInfinityMessaging().ConfigureInMemoryBus();
Build and run the application and make sure everything is working as expected.
Now we need to configure a ChannelProducer that will be used to send messages. In this sample, we will use the in-memory channel producer.
builder.AddInfinityMessaging()
.ConfigureInMemoryBus(builder =>
{
builder.AddChannelProducer<WeatherForecast>(options => { options.ChannelName = "weatherforecasts"; })
});
This will add a channel producer that will send messages of type WeatherForecast to the channel named weatherforecasts.
- Now we can send messages to the channel but we also need to configure a channel consumer that will consume the messages. In this sample, we will use the in-memory channel consumer that consumes messages from the weatherforecasts channel. The channel is configured as a topic so we need to configure a subscription name as well.
builder.AddInfinityMessaging()
.ConfigureInMemoryBus(builder =>
{
builder
.AddChannelProducer<WeatherForecast>(options => { options.ChannelName = "weatherforecasts"; })
.AddChannelConsumer<WeatherForecast>(options =>
{
options.ChannelName = "weatherforecasts";
options.SubscriptionName = "weathersubscription";
});
});
- Now we can send messages to the channel and consume the messages. However we have no handler that will handle the messages with the type WeatherForecast. Let's add the handler to the services collection.
builder.AddInfinityMessaging()
.ConfigureInMemoryBus(builder =>
{
builder
.AddChannelProducer<WeatherForecast>(options => { options.ChannelName = "weatherforecasts"; })
.AddChannelConsumer<WeatherForecast>(options =>
{
options.ChannelName = "weatherforecasts";
options.SubscriptionName = "weathersubscription";
});
})
.MapMessageHandler<WeatherForecast, WeatherForecastMessageHandler>();
- Let's add an endpoint so we can send messages to the channel.
app.MapPost("/send", async (ChannelProducer<WeatherForecast> producer) =>
{
await producer.SendAsync(new WeatherForecast(DateTime.Now, 20, "Sunny"));
return Results.Ok();
});
- Now we have a complete setup with a message bus, channel producer, channel consumer and a message handler. We can now send messages to the channel and the message handler will handle the messages.
Contributing
If you have any ideas, suggestions or issues, please create an issue or a pull request. Or reach out to me on BlueSky.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
-
net9.0
- Infinity.Toolkit (>= 1.0.0)
- Microsoft.Extensions.DependencyInjection (>= 9.0.1)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.1)
- Microsoft.Extensions.Hosting (>= 9.0.1)
- Microsoft.Extensions.Hosting.Abstractions (>= 9.0.1)
- Microsoft.Extensions.Logging (>= 9.0.1)
- Microsoft.Extensions.Logging.Abstractions (>= 9.0.1)
- Microsoft.Extensions.Options (>= 9.0.1)
- Microsoft.Extensions.Options.DataAnnotations (>= 9.0.1)
- OpenTelemetry (>= 1.11.1)
- OpenTelemetry.Api (>= 1.11.1)
- Scrutor (>= 6.0.1)
- System.Diagnostics.DiagnosticSource (>= 9.0.1)
- System.Memory.Data (>= 9.0.1)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Infinity.Toolkit.Messaging:
Package | Downloads |
---|---|
Infinity.Toolkit.Messaging.AzureServiceBus
Azure Service Bus Integration for Infinity.Toolkit.Messaging. |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.0 | 82 | 1/30/2025 |