Search code examples
c#azure.net-coreazure-storageazure-queues

Azure Queue: Unable to read cloud queue messages. AsString and AsBytes properties are throwing System.ArgumentException


Ok, I have a class intended to manage Azure cloud queue messages, the insertions are working fine:

    public async void Insert(string message)
    {
        await Queue.AddMessageAsync(new CloudQueueMessage(message));
    }

Note that this Queue is a CloudQueue instance.

This is the message inserted in the storage using the above method

This is the message inserted in the storage using the above method

But, while trying to Get or Peek messages, a weird behavior is happening and I can't read anything of the content:

The content of the message is throwing an exception and is being returned as null

The content of the message is throwing an exception and is being returned as null.

This is the messageCount value

This is the messageCount value.

Here's textually, the method used to retrieve the messages:

    public async Task<List<string>> GetMessages()
    {
        var list = new List<string>();

        await Queue.FetchAttributesAsync();
        int messageCount = Queue.ApproximateMessageCount ?? 0;

        if (messageCount == 0) return list;

        foreach (var msg in await Queue.GetMessagesAsync(messageCount))
        {
            
             list.Add(msg.AsString);
        }

        return list;
    }

EDIT: I checked as pointed in the approved answer and figured out that I was using a deprecated package that seems to work no longer. Deprecated package This library has been split in multiple parts and is deprecated.

I had to use a most recent API and change my code, it's actually easier to use but a certain rework was needed. It's now working, and the way to use the queue in this new version (.NET v12) is documented here.


Solution

  • I believe it's related to wrong (legacy) Nuget Package. I've faced a problem like this recently and it was solved when I've upgraded to the following Packages:

    Install-Package Microsoft.Azure.Storage.Common -Version 11.1.7
    Install-Package Microsoft.Azure.Storage.Queue -Version 11.1.7
    

    https://www.nuget.org/packages/Microsoft.Azure.Storage.Queue/

    https://www.nuget.org/packages/Microsoft.Azure.Storage.Common/