-
Notifications
You must be signed in to change notification settings - Fork 55
Samples in C#
Shown here are samples that are a bit more extensive than the ones shown in the repository readme.
We support several types with this output binding. Messages in RabbitMQ are usually processed as bytes, so we can bind to either byte[], string, or POCO. Some examples are shown below.
To directly open and bind to a RabbitMQ connection, you just need to specify the connection information. Then, you can use client just like you'd use any RabbitMQ model.
public static void BindToClient(
[RabbitMQ(ConnectionStringSetting = "rabbitMQConnectionAppSetting")] IModel client,
ILogger logger)
{
QueueDeclareOk queue = client.QueueDeclare("hello", false, false, false, null);
logger.LogInformation("Opening connection and creating queue!");
}
public static void TimerTrigger_PocoOutput(
[TimerTrigger("00:01")] TimerInfo timer,
[RabbitMQ(
Hostname = "localhost",
QueueName = "queue")] out TestClass outputMessage, ILogger logger)
{
outputMessage = new TestClass(1, 1);
logger.LogInformation($"RabbitMQ output binding message: {outputMessage}");
}
public class TestClass
{
public int x, y;
public TestClass(int x, int y)
{
this.x = x;
this.y = y;
}
}
This sample is triggered on a timer, which binds the value of the output message every second. Here the output message is a POCO called TestClass.
public static void QueueTrigger_StringOutput(
[QueueTrigger(@"samples-rabbitmq-messages")] TestClass message,
[RabbitMQ(
Hostname = "localhost",
QueueName = "queue")] out TestClass outputMessage)
{
outputMessage = message;
}
The output binding on this sample is triggered on a nonempty queue, which can be managed on the Azure portal. To run the sample:
- Create an Azure Storage account and go to the homepage for that account
- Look for Queue service on the sidebar and click Queues
- Create a queue called "samples-rabbitmq-messages"
- Add a message to the queue in POCO format (i.e. "{ "x": 1, "y": 1 })
- Run the sample
Note that any time the queue isn't empty, the trigger will continue to fire. You can add items to the queue while the sample is running, and the binding will listen for and detect these updates.
The RabbitMQ trigger binding supports a number of basic EventArgs conversions. From the trigger we currently can extract the RabbitMQ event arguments, or the delivered message as a string, as a byte{}, as a JSON object if it is delivered as a valid JSON string, or as a C# POCO. We can also extract the attributes of a RabbitMQ message using this trigger
public static void RabbitMQTrigger_BasicDeliverEventArgs(
[RabbitMQTrigger("queue", HostName = "localhost")] BasicDeliverEventArgs args,
ILogger logger
)
{
logger.LogInformation($"RabbitMQ queue trigger function processed message: {Encoding.UTF8.GetString(args.Body)}");
}
Here, we extract the entire BasicDeliverEventArgs of the message when the trigger receives it, which is the default format of RabbitMQ messages. The function itself logs the received message body.
Here the trigger message is a POCO called TestClass.
public static void RabbitMQTrigger_POCO(
[RabbitMQTrigger("queue", HostName = "localhost")] TestClass pocObj,
ILogger logger
)
{
logger.LogInformation($"RabbitMQ queue trigger function processed message: {pocObj}");
}
When the trigger is fired, we get a message from the queue named "queue" connected to "localhost" in POCO format. Like with Json objects, we will receive an error if the message isn't properly formatted as a C# object. If it is, it is then bound to the variable pocObj, which we can use for whatever we need it for.