0

We send servicebus messages with an old library via Microsoft.ServiceBus.Messaging which is available in the nuget-package 'ServiceBus.v1_1' and want to receive and parse the message with the newest service bus client: Azure.Messaging.ServiceBus.

[FunctionName("FunctionHandler")]
public async Task RunFunction(
    [ServiceBusTrigger("%ServiceBusQueueName%", Connection = ApFunctionDefaults.ServiceBusNames.ServiceBusConnectionString)]
    ServiceBusReceivedMessage queueMessage)
{
    var jsonSettings = KnownTypesBinder.GetSerializerSettings(KnownTypeProvider.GetKnownTypes());
    command = JsonConvert.DeserializeObject<MyDto>(Encoding.UTF8.GetString(queueMessage.Body), jsonSettings); // <- throws an exception
}

The body of the message queueMessage.Body has some value like that:

@\u000eCommandMessage\bUhttp://schemas.datacontract.org/2004/07/ ...more payload here... a\u0001"

With JsonConvert.DeserializeObject, we get an format exception.

How do we deserialize a message which was send by Microsoft.ServiceBus.Messaging (Nuget-Package: ServiceBus.v1_1) with Azure.Messaging.ServiceBus?

1 Answer 1

1

The legacy package uses a DataContractSerializer to encode the paylaod, where newer generations treat the message payload as an opaque blob of bytes. To process the message using Azure.Messaging.ServiceBus, you'll need to decode the body.

// receive the message
var receiver = client.CreateReceiver(queueName);
var receivedMessage = await receiver.ReceiveMessageAsync();

// deserialize the XML envelope into a string
var deserializer = new DataContractSerializer(typeof(MyDto));

var reader = XmlDictionaryReader.CreateBinaryReader(
    receivedMessage.Body.ToStream(), 
    XmlDictionaryReaderQuotas.Max);

MyDto myDto = deserializer.ReadObject(reader);

More context and discussion can be found in the Service Bus interop sample.

Sign up to request clarification or add additional context in comments.

4 Comments

Your solution can be used, when sending messages with WindowsAzure.ServiceBus. But in my case, we are sending messages with Microsoft.ServiceBus.Messaging.
Apologies for the confusion. Microsoft.ServiceBus.Messaging is the namespace used by WindowsAzure.ServiceBus and is often confused with the package name. As I've never actually seen ServiceBus.v1_1 before and am shocked that it hasn't been marked as unmaintained, I've passed this along to the Service Bus team. The code is not accessible internally for me to see how they're encoding/serializing.
According to the Service Bus team, that package was associated with the "Service Bus for Windows Server" product, which reached end-of-life in 2023 and is no longer supported. The package was never intended to be used with Azure Service Bus and the details of its internals are no longer known. I've shared that they missed deprecating/retiring the SDK package and they will follow-up to ensure that it is annotated as such in NuGet. learn.microsoft.com/en-us/lifecycle/products/…
I have fixed some code in your answer which now works with my prototype, dispite using Microsoft.ServiceBus.Messaging (ServiceBus.v1_1)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.