Home > OS >  Read from Service Bus Queue and Write to a different SB Queue in .Net
Read from Service Bus Queue and Write to a different SB Queue in .Net

Time:01-21

I have a business scenario where a third party client API is calling a generic ServiceBus Queue and posting messages. I want to read those messages in Azure function and based on a business rules, write those messages into different service bus queues.

To achieve this, I created the below Azure function (v3) in .Net

namespace AzureFunctionTesting
{
    public class Function1
    {
        [FunctionName("Function1")]
        public async Task<IActionResult> Run(
            [ServiceBusTrigger("QueueName", Connection = "StorageQueueConnectionString")] string myQueueItem,
            [ServiceBus("Productqueue", Connection = "QueueConnectionString")] IAsyncCollector<dynamic> outputQueue1,
            [ServiceBus("Supplierqueue", Connection = "QueueConnectionString")] IAsyncCollector<dynamic> outputQueue2,
            ILogger log)
        {
            log.LogInformation($"C# ServiceBus queue trigger function processed message: {myQueueItem}");
            var document = JsonConvert.DeserializeObject<ShippingSKU>(myQueueItem);


            string connectionString = Environment.GetEnvironmentVariable("QueueConnectionString");
            ServiceBusClient client = new ServiceBusClient(connectionString);
            ServiceBusSender senderQueue1 = client.CreateSender("queue1");
            ServiceBusSender senderQueue2 = client.CreateSender("queue2");
            using ServiceBusMessageBatch messageBatchQueue1 = await senderQueue1.CreateMessageBatchAsync();

            if (document.Id == 1)
            {
                Console.WriteLine("Entering into Queue 1");
                Console.WriteLine(document.FirstName);
                await outputQueue1.AddAsync(document);
            }
            else
            {
                Console.WriteLine("Entering into Queue 2");
                Console.WriteLine(document.LastName);
                await outputQueue2.AddAsync(document);
            }
            return new OkObjectResult(null);
        }
    }
}

But when I am trying to run the azure function, I am getting this error

Error

I am new to Azure Functions and any help would be highly appreciated. Thanks

EDIT: Short and concise answer by Sean has helped me solving the issue. I was overthinking and overcomplicating the solution. Surprisingly on the internet, I could not find even a single example a scenario where code is reading from 1 Queue and writing to Another. For the sake of completeness, I am adding below the working code.

namespace AzureFunctionTesting
{
    public class Function1
    {
        [FunctionName("Function1")]
        public async Task Run(
        [ServiceBusTrigger("QueueName", Connection = "QueueConnectionString")] string myQueueItem,
        [ServiceBus("productqueue", Connection = "QueueConnectionString")] IAsyncCollector<dynamic> outputQueue1,
        [ServiceBus("supplierqueue", Connection = "QueueConnectionString")] IAsyncCollector<dynamic> outputQueue2,
        ILogger log)
        {
            log.LogInformation($"C# ServiceBus queue trigger function processed message: {myQueueItem}");
            var document = JsonConvert.DeserializeObject<ShippingSKU>(myQueueItem);
            if (document.Id == 1)
            {
                Console.WriteLine("Entering into Queue 1");
                Console.WriteLine(document.FirstName);
                await outputQueue1.AddAsync(document);
            }
            else
            {
                Console.WriteLine("Entering into Queue 2");
                Console.WriteLine(document.LastName);
                await outputQueue2.AddAsync(document);
            }
        }
    }
}

CodePudding user response:

  1. You're mixing a Service Bus trigger with a return type for what looks like an HTTP trigger. Remove IActionResult as your return type.
  2. IAsyncCollector is how you dispatch messages, don't instantiate a new ServiceBusClient and senders.
  •  Tags:  
  • Related