RabbitMQ
This connector simplifies accessing RabbitMQ message brokers. It supports the following .NET drivers:
- RabbitMQ.Client, which provides an
IConnection
.
The remainder of this page assumes you're familiar with the basic concepts of Steeltoe Connectors.
Usage
To use this connector:
- Create a RabbitMQ server instance or use a docker container.
- Add NuGet references to your project.
- Configure your connection string in
appsettings.json
(optional). - Initialize the Steeltoe Connector at startup.
- Use the driver-specific connection/client instance.
Add NuGet References
To use this connector, add a NuGet reference to Steeltoe.Connectors
.
Also add a NuGet reference to one of the .NET drivers listed above, as you would if you were not using Steeltoe.
Configure connection string
The available connection string parameters for RabbitMQ are documented here.
The following example appsettings.json
uses the docker container from above:
{
"Steeltoe": {
"Client": {
"RabbitMQ": {
"Default": {
"ConnectionString": "amqp://localhost"
}
}
}
}
}
Initialize Steeltoe Connector
Update your Program.cs
as below to initialize the Connector:
using Steeltoe.Connectors.RabbitMQ;
var builder = WebApplication.CreateBuilder(args);
builder.AddRabbitMQ();
Use IConnection
To obtain an IConnection
instance in your application, inject the Steeltoe factory in a controller or view:
using System.Text;
using Microsoft.AspNetCore.Mvc;
using RabbitMQ.Client;
using Steeltoe.Connectors;
using Steeltoe.Connectors.RabbitMQ;
public class HomeController : Controller
{
public IActionResult Index(
[FromServices] ConnectorFactory<RabbitMQOptions, IConnection> connectorFactory)
{
var connector = connectorFactory.Get();
IConnection connection = connector.GetConnection();
using IModel channel = connection.CreateModel();
BasicGetResult? result = channel.BasicGet("ExampleQueue", true);
string? message = result != null ? Encoding.UTF8.GetString(result.Body.ToArray()) : null;
ViewData["Result"] = message;
return View();
}
}
A complete sample app that uses IConnection
is provided at https://github.com/SteeltoeOSS/Samples/tree/main/Connectors/src/RabbitMQ.
Cloud Foundry
This Connector supports the following service brokers:
You can create and bind an instance to your application by using the Cloud Foundry CLI:
# Create RabbitMQ service
cf create-service p.rabbitmq single-node myRabbitMQService
# Bind service to your app
cf bind-service myApp myRabbitMQService
# Restage the app to pick up change
cf restage myApp
Kubernetes
This Connector supports the Service Binding Specification for Kubernetes. It can be used through the Bitnami Services Toolkit.
For details on how to use this, see the instructions at https://github.com/SteeltoeOSS/Samples/tree/main/Connectors/src/RabbitMQ#running-on-tanzu-application-platform-tap.