Search code examples
c#.net-coreazure-functionsazure-queuesazure-triggers

Azure Functions: Queue Trigger is expecting Base-64 messages and doesn't process them correctly


I have this Queue Trigger. The expected is when I insert a message in the Queue, the trigger must fire and process the dequeued message.

    [FunctionName("NewPayrollQueueTrigger")]
    public async static void Run([QueueTrigger("myqueue", Connection = 
    "AzureWebJobsStorage")]string myQueueItem,
        [DurableClient] IDurableOrchestrationClient starter,
        ILogger log)
    {
        log.LogInformation($"C# Queue trigger function processed: {myQueueItem}");

        await starter.StartNewAsync("NewPayrollOrchestrator", input: myQueueItem);

    }

The trigger is being activated normally, but this weird behavior is happening. The function apparently expects that the message is encoded in Base-64.

Exception binding parameter 'myQueueItem' <--- The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.

I'm sending messages to the queue using this method from the Azure Queue library v.12 from Azure.Storage.Queues and found no overloads that encodes the message to Base-64. Note that _queue is a QueueClient instance.

 public async Task<Response<SendReceipt>> SendAsync(string message)
 {
        return await _queue.SendMessageAsync(message);
 }

So I tried to encode the message by myself...

 public async Task<Response<SendReceipt>> SendAsBase64Async(string message)
 {
      byte[] buffer = Encoding.Unicode.GetBytes(message);
      string msg = Convert.ToBase64String(buffer);

      return await _queue.SendMessageAsync(msg);

 }

... and it doesn't work either. Here's my code passing by that part but throwing error further on, indicating that it could get the message but it was not decoded correctly, since it was a filename of an existing blob in a storage:

Decode error

The only way to get this working is if I manually send a message to the queue using the Azure Storage Explorerchoosing for encode the message via UI.


Solution

  • As an alternative, you can take advantage of the built in functionality for converting the message to Base64 by creating the client with an QueueClientOptions with MessageEncoding set to QueueMessageEncoding.Base64.

    For example:

    _queue = new QueueClient(connectionString, queueName, new QueueClientOptions
    {
        MessageEncoding = QueueMessageEncoding.Base64
    });
    
    ...
    
    var message = "some message";
    await _queue.SendMessageAsync(message); // Will be converted as Base64.