Connect FlowMQ with AMQP .NET Client
FlowMQ's native support for AMQP 0.9.1 makes it an excellent choice for .NET applications that require reliable, message-based communication. This guide will walk you through using the official RabbitMQ .NET Client to connect to FlowMQ, declare queues, publish messages, and consume them.
Prerequisites
Before you start, ensure you have the following:
- A running instance of FlowMQ.
- .NET 6 or later installed.
- A .NET development environment like Visual Studio or the .NET CLI.
Installation
Add the RabbitMQ .NET Client to your project using the NuGet package manager.
# Using .NET CLI
dotnet add package RabbitMQ.Client
# Or using Package Manager Console
Install-Package RabbitMQ.Client
Connecting to FlowMQ
First, you need to establish a connection to your FlowMQ broker and open a channel. All AMQP operations are performed on a channel.
using RabbitMQ.Client;
using System;
using System.Text;
class AmqpDotnetClient
{
public static void Main(string[] args)
{
var factory = new ConnectionFactory() { HostName = "localhost" };
using (var connection = factory.CreateConnection())
using (var channel = connection.CreateModel())
{
// ... declare, publish, and consume logic here ...
}
}
}
Declaring a Queue
For simplicity, we'll declare a queue and publish messages directly to it using the default exchange. The default exchange will route messages to the queue whose name matches the message's routing key.
// Inside the 'using' block
channel.QueueDeclare(queue: "hello-dotnet",
durable: false,
exclusive: false,
autoDelete: false,
arguments: null);
Publishing a Message
Now you can publish a message. The routing key must match the queue name to be delivered correctly.
string message = "Hello FlowMQ from .NET!";
var body = Encoding.UTF8.GetBytes(message);
channel.BasicPublish(exchange: "",
routingKey: "hello-dotnet",
basicProperties: null,
body: body);
Console.WriteLine(" [x] Sent {0}", message);
Consuming Messages
To consume messages, you can set up an EventingBasicConsumer
, which will fire an event whenever a message is received.
using RabbitMQ.Client.Events;
var consumer = new EventingBasicConsumer(channel);
consumer.Received += (model, ea) =>
{
var body = ea.Body.ToArray();
var message = Encoding.UTF8.GetString(body);
Console.WriteLine(" [x] Received {0}", message);
};
channel.BasicConsume(queue: "hello-dotnet",
autoAck: true,
consumer: consumer);
Console.WriteLine(" Press [enter] to exit.");
Console.ReadLine();
Full Example
Here is a complete example that shows how to set up a publisher and a consumer.
Publisher (Publisher.cs
)
using RabbitMQ.Client;
using System.Text;
class Publisher
{
public static void Main()
{
var factory = new ConnectionFactory() { HostName = "localhost" };
using(var connection = factory.CreateConnection())
using(var channel = connection.CreateModel())
{
channel.QueueDeclare(queue: "hello", durable: false, exclusive: false, autoDelete: false, arguments: null);
string message = "Hello World!";
var body = Encoding.UTF8.GetBytes(message);
channel.BasicPublish(exchange: "", routingKey: "hello", basicProperties: null, body: body);
Console.WriteLine(" [x] Sent {0}", message);
}
}
}
Consumer (Consumer.cs
)
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System;
using System.Text;
class Consumer
{
public static void Main()
{
var factory = new ConnectionFactory() { HostName = "localhost" };
using(var connection = factory.CreateConnection())
using(var channel = connection.CreateModel())
{
channel.QueueDeclare(queue: "hello", durable: false, exclusive: false, autoDelete: false, arguments: null);
var consumer = new EventingBasicConsumer(channel);
consumer.Received += (model, ea) =>
{
var body = ea.Body.ToArray();
var message = Encoding.UTF8.GetString(body);
Console.WriteLine(" [x] Received {0}", message);
};
channel.BasicConsume(queue: "hello", autoAck: true, consumer: consumer);
Console.WriteLine(" Press [enter] to exit.");
Console.ReadLine();
}
}
}
Additional Resources
- For more advanced scenarios and features, refer to the official RabbitMQ .NET Client Guide.
- Explore FlowMQ's advanced features for AMQP integration.