Home > Enterprise >  Change encoding on Azure queue message with version 12.x of Azure
Change encoding on Azure queue message with version 12.x of Azure

Time:02-02

I have moved from v11 to v12 of the Azure library. When I insert a message using the following code:

        private static void insertQueueMessage(string messageToInsert, string queueName)
    {
        CloudStorageAccount storageAccount;
        storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString")); //points to the azure storage account

        // Create the queue client.
        CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();

        // Retrieve a reference to a queue for our new files to be imported to LOB
        CloudQueue incomingCustomerQueue = queueClient.GetQueueReference(queueName);
        CloudQueueMessage newQueueMessage = new CloudQueueMessage(messageToInsert);

        incomingCustomerQueue.AddMessage(newQueueMessage);
    }

That inserts it as a base 64 encoded message

When I try and retrieve it using a normal Console app (also using the 12.x library) with the following code:

            QueueClient queue = new QueueClient(connectionString, queueName);
        
        // process each of the messages
        foreach (QueueMessage jsonMessage in queue.ReceiveMessages(maxMessages: 10).Value)
        {
            //decode message
            Console.WriteLine($"Message: {jsonMessage.Body}");
            incomingMessageInfo messageData = JsonConvert.DeserializeObject<incomingMessageInfo>(jsonMessage.Body.ToString());

it fails due to the message being encoded. If I manually using Azure Explorer change the encoding to be UTF-8, it works.

I have found some references to "specify the MessageEncoding when injecting the service" I do not know how to do this.

This is a normal C# Console application targeting .NET 4.8

Update From Answer - trying to apply a new instance of queue options:

  // Create the queue client.
        CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();
        QueueClientOptions newOptions = new QueueClientOptions();
        newOptions.MessageEncoding = QueueMessageEncoding.None;

        // Retrieve a reference to a queue for our new files to be imported to LOB
        CloudQueue incomingCustomerQueue = queueClient.GetQueueReference(queueName);
        CloudQueueMessage newQueueMessage = new CloudQueueMessage(messageToInsert);

        incomingCustomerQueue.AddMessage(newQueueMessage);

Final Code that fixed it:

            QueueClientOptions newOptions = new QueueClientOptions();
        newOptions.MessageEncoding = QueueMessageEncoding.None;
        QueueClient qcl = new QueueClient(conString, queueName, newOptions);
        var response = qcl.SendMessage(messageToInsert);

CodePudding user response:

The property you are looking for is MessageEncoding property which is available in QueueClientOptions class.

You would need to create a new instance of QueueClientOptions and set its MessageEncoding property to QueueMessageEncoding.None. You will then need to use this option when creating an instance of QueueServiceClient.

  •  Tags:  
  • Related