1
votes

I use Azure Functions with Queue triggers in my backend and up to this point, I'd been using the Microsoft.WindowsAzure.Storage package to handle all Azure Storage operations i.e. queues, blobs, etc. With this package, I'd simply send a MyQueueRequest object to my queue and everything worked fine.

Because the Microsoft.WindowsAzure.Storage package has been deprecated, I swithched to Azure.Storage.Queue and my Azure Function started throwing the following error:

Microsoft.Azure.WebJobs.Host: Exception binding parameter 'message'. System.Private.CoreLib: 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've found this article that suggests that the new library requires JSON objects to be encoded in Base64 (https://briancaos.wordpress.com/2020/10/16/sending-json-with-net-core-queueclient-sendmessageasync/).

Up to this point, I actually never even serialized my MyQueueRequest object to JSON. The model binder took care of that for me automatically.

Does this mean, going forward, before sending the message to my queue, I need to first serialize MyQueueRequest object and then Base64 encode it and then reverse the process in my Azure Functions?

1

1 Answers

2
votes

Yes, for this new package you will need to do this. I ran into the same problem when trying to add POCOs to a queue. I use similar code to the article you cite.

I use the following code to handle this:

await queue.SendMessageAsync(Base64Encode(JsonSerializer.Serialize(myObject)));

Where Base64Encode is:

    private string Base64Encode(string plainText)
    {
        var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
        return Convert.ToBase64String(plainTextBytes);
    }

Be sure it's also UTF-8. I'm using System.Text.Json for this example, but Newtonsoft would work just as well.