Skip to content

Developing and Testing Locally

Yogesh Jagadeesan edited this page Dec 21, 2020 · 13 revisions

The following will show you how to develop and test the RabbitMQ Extension locally with C# functions in Visual Studio

Prerequisites

Make sure you've installed Visual Studio.

Creating a Function

  1. In Visual Studio, select File > New > Project.
  2. Search for and select the Azure Functions template, then select Next.
  3. Fill in the project and solution names, then click Create.
  4. In "Create New Azure Functions Application", choose Empty, then click Create.

Using the RabbitMQ Extension

  1. In the Solution Explorer of your project, right click on the project name. Go to Manage NuGet Packages.
  2. Search for the package Microsoft.Azure.WebJobs.Extensions.RabbitMQ and install it.
  3. Now we can start developing functions!

Here, we will show a simple example of a trigger function and an output function. Within the project, create two C# files named "OutputFunction.cs" and "TriggerFunction.cs". Their contents should look like the following:

OutputFunction.cs

using System;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;

namespace RabbitMQTest
{
    public static class OutputFunction
    {
        [FunctionName("OutputFunction")]
        public static void Run([TimerTrigger("0 */1 * * * *")]TimerInfo myTimer,
            [RabbitMQ(
            QueueName = "queue")] out string outputMessage,
            ILogger log)
        {
            log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
            outputMessage = "new";
        }
    }
}

TriggerFunction.cs

using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;

namespace RabbitMQTest
{
    public static class TriggerFunction
    {
       [FunctionName("TriggerFunction")]
        public static void Run([RabbitMQTrigger("queue")] string message,
        ILogger log)
        {
            log.LogInformation($"Message received from RabbitMQ trigger: {message}");
        }
    }
}

To configure a connection string locally, add it to the local.settings.json file. It should look like this:

{
    "IsEncrypted": false,
    "Values": {
        "AzureWebJobsStorage": "UseDevelopmentStorage=true",
        "FUNCTIONS_WORKER_RUNTIME": "dotnet",
        "connectionStringSetting":  "amqp://guest:guest@localhost:5672" // This is the default connection string.
    }
}

**Note that the output function automatically picks up this connection string value even if it isn't included in the function signature. If you were to configure a ConnectionStringSetting in the parameters of the function, it will look for the value somewhere else, not in local.settings.json. It will then default to whatever value is in that file.

Run the project from the solution. In the window that pops up, you should see logs from the functions running!

For more detailed samples, you may refer to the Samples in C# section of the Wiki.

Dead letter queues

Dead letter queues and exchanges can't be controlled or configured from the RabbitMQ trigger. In order to use dead letter queues, pre-configure the queue used by the trigger in RabbitMQ. Please refer to the RabbitMQ documentation.

Monitoring RabbitMQ endpoint

To monitor your queues and exchanges for a certain RabbitMQ endpoint: