IPC .NET library powered by RabbitMQ. Provides RPC query, broadcast, and queue processing functionality.
Install NuGet package:
dotnet add package HareIsle
Add using derective:
using RabbitMQ.Client;
using Usefull.HareIsle;Create RabbitMq connection factory:
ConnectionFactory connectionFactory = new()
{
Uri = new Uri("amqp://user:pass@rabbithost:5672/"),
ConsumerDispatchConcurrency = 10,
AutomaticRecoveryEnabled = true,
TopologyRecoveryEnabled = true,
RequestedHeartbeat = TimeSpan.FromSeconds(10)
};Create the broadcast message handler:
// RabbitMq connection
using var connection = await connectionFactory.CreateConnectionAsync();
// Message handler
using var handler = new BroadcastHandler<SomeMessage>(
"Handler actor ID", // ID of the actor on whose behalf the message is processed
connection, // RabbitMq connection
"Broadcast ID", // ID of the actor on whose behalf messages are broadcast
message => // handling function
{
// do some handling
});
Console.ReadKey(); // message listening stops when press a keyPublish the broadcast message:
// Create the broadcast publisher
using var emitter = new Emitter("Broadcast ID", connection);
// Publish the message
await emitter.BroadcastAsync(new SomeMessage
{
Message = "some text",
Value = 123
});Create the queue handler:
// RabbitMq connection
using var connection = await connectionFactory.CreateConnectionAsync();
// Message handler
using var handler = new QueueHandler<SomeMessage>(
"Handler actor ID", // ID of the actor on whose behalf the message is processed
connection, // RabbitMq connection
"QueueName", // queue name
5, // concurrency factor
message => // handling function
{
// do some handling
});
Console.ReadKey(); // queue listening stops when press a keyPlace the message in the queue:
using var emitter = new Emitter("SomeActorID", connection); // create the emmiter
await emitter.DeclareQueueAsync("QueueName"); // declare the queue
await emitter.EnqueueAsync("QueueName", new SomeMessage // send the message
{
Text = "some text"
});Create the RPC handler:
// RabbitMq connection
using var connection = await connectionFactory.CreateConnectionAsync();
// RPC handler
using var handler = new RpcHandler<Request, Response>(
"Actor ID", // ID of the actor on whose behalf the RPC is processed
connection, // RabbitMq connection
5, // concurrency factor
reques => // RPC handling function
{
// do some handling
});
Console.ReadKey(); // message listening stops when press a keyMake the RPC and get a response:
using var rpcClient = new RpcClient("ClientID", connection);
var request = new Request
{
Value = 123,
Text = "some text"
};
var response = await rpcClient.CallAsync<Request, Response>("Actor ID", request);