0

I have been trying to migrate my code base to latest ServiceBus SDK and unable to find a method which can serialize a binary body to a source object.

In the past I use to retrieve the object as below:

receivedMessage.GetBody<RemoteExecutionContext>(
                   new DataContractJsonSerializer(typeof(RemoteExecutionContext)));

How do I archive similar result in new library?

Shiju Samuel
  • 1,373
  • 6
  • 22
  • 45
  • 1
    the from & to is identical... maybe you should mention the versions. – Raptor Sep 01 '21 at 04:31
  • Can you clarify which SDK you're moving to? The title suggests that you're migrating from the current generation, `Azure.Messaging.ServiceBus`, to a legacy package. If you're attempting to move from `WindowsAzure.ServiceBus` to `Azure.Messaging.ServiceBus`, I believe [this sample](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/servicebus/Azure.Messaging.ServiceBus/samples/Sample08_Interop.md) illustrates the interop scenario that you're asking about. – Jesse Squire Sep 01 '21 at 14:14

1 Answers1

0

The sample below express how to interact with messages that are sent using WindowsAzure.ServiceBus and received using Azure.Messaging.Service. To serialize the BrokeredMessage body the WindowsAzure.ServiceBus uses DataContractSerializer. Which helps in working with this library, below steps are required in sending messages through WindowsAzure.ServiceBus.

ServiceBusReceiver receiver = client.CreateReceiver(queueName);

ServiceBusReceivedMessage received = await receiver.ReceiveMessageAsync();

// Similar to the send scenario, we still rely on the DataContractSerializer and we use string as our type because we are expecting a JSON
// message body.
var deserializer = new DataContractSerializer(typeof(string));
XmlDictionaryReader reader =
    XmlDictionaryReader.CreateBinaryReader(received.Body.ToStream(), XmlDictionaryReaderQuotas.Max);

// deserialize the XML envelope into a string
string receivedJson = (string) deserializer.ReadObject(reader);

// deserialize the JSON string into TestModel
TestModel output = JsonSerializer.Deserialize<TestModel>(receivedJson);

For more information check ServiceBus link.

SauravDas-MT
  • 1,224
  • 1
  • 4
  • 10