1

I'm using Microsoft.Azure.ServiceBus 2.0.0 to subscribe for queue messages, and get unexpected characters when I use Encoding.UTF8.GetString(serviceBusMessage.Body).

enter image description here

It looks like the message content should have been valid XML, but its certainly not.

The code that sends messages, uses the old WindowsAzure.ServiceBus 4.1.6 library, and looks like this:

private void SendToProcessingQueue(Guid accountId, Message msg) { string queueName = msg.MessageType.ToLower(); var client = CreateQueueClient(queueName); client.Send(new BrokeredMessage(new MessageHint() { AccountId = accountId, MessageId = msg.Id, MessageType = msg.MessageType })); }

Are the new and old libraries incompatible?

Eivind Gussiås Løkseth
  • 1,862
  • 2
  • 21
  • 35

2 Answers2

1

To create compatible messages it seems you need to encode them using a DataContractBinarySerializer. Thankfully they include one in the Microsoft.Azure.ServiceBus nuget package. The compatibility serialization function looks like this:

byte[] Serialize<T>(T input)
{
    var serializer = Microsoft.Azure.ServiceBus.InteropExtensions.DataContractBinarySerializer<T>.Instance;
    MemoryStream memoryStream = new MemoryStream(256);
    serializer.WriteObject(memoryStream, input);
    memoryStream.Flush();
    memoryStream.Position = 0L;
    return memoryStream.ToArray();
}

So sending your message with the new library would look like:

private void SendToProcessingQueue(Guid accountId, Message msg)
{
    string queueName = msg.MessageType.ToLower();
    var client = CreateQueueClient(queueName);
    client.Send(new Message(Serialize(new MessageHint()
    {
        AccountId = accountId,
        MessageId = msg.Id,
        MessageType = msg.MessageType
    })));
}

If you are receiving a message with the new library from the old library, the developers of the new library have helpfully provided an extension method Microsoft.Azure.ServiceBus.InteropExtensions.MessageInteropExtensions.GetBody<T> to read the message in a compatible way. You use it like this:

MessageHint hint = message.GetBody<MessageHint>();
Ceilingfish
  • 5,397
  • 4
  • 44
  • 71
0

Incompatible or not - I was able to parse the message after reimplementing the class library as .NET 4.7 instead of .NET Standard 2.0 and referencing WindowsAzure.ServiceBus 4.1.7 instead of Microsoft.Azure.ServiceBus 2.0.0. The API is about the same, so it took me only 15 minutes to reimplement.

enter image description here

Update: If someone knows how to exchange messages between the two versions, please post another answer describing how it should be done.

Eivind Gussiås Løkseth
  • 1,862
  • 2
  • 21
  • 35