Azure Scheduler and Azure Service Bus

321 Views Asked by At

I'm using Azure Scheduler to place a message in a topic in Azure Service Bus. In Azure Scheduler I have entered the following information in the "Message" property:

{"StartTid":null,"Id":"c594eab8-382c-49ec-ba9c-2db0aaf5e167","CreationDate":"2018-02-11T17:42:42.9823622Z","TenantId":null}

When retrieving the message from my app, using this code:

var messageData = Encoding.UTF8.GetString(message.Body);

The message I get looks like this:

@string3http://schemas.microsoft.com/2003/10/Serialization/�{{"StartTid":null,"Id":"c594eab8-382c-49ec-ba9c-2db0aaf5e167","CreationDate":"2018-02-11T17:42:42.9823622Z","TenantId":null}

It seems like Azure Scheduler is adding information to the message.

What is the correct way to retrieve only the message I entered in Azure Schedulers Message property without get all the garbage?

2

There are 2 best solutions below

4
Sean Feldman On BEST ANSWER

Azure Service Bus .NET Standard Client deals with streams only. When the old client is used and data is not sent using a stream, the receiving code based on Azure Service Bus .NET Standard Client has to perform de-serialization as was outlined in the other answer. There's a Broken Wire Compatability issue that was closed by the messaging team that has the details.

The good news is that you don't need the serializer code in your code base. The new client has an interop extension method for Message so you don't have to deal with data contract deserialization. For example, in your case you'd be able to write

var message = await messageReceiver.ReceiveAsync();
var data = message.GetBody<string>();
3
Bruce Chen On

var messageData = Encoding.UTF8.GetString(message.Body);

Based on your code, I assumed that you are using the Azure Service Bus .NET Standard Client SDK.

@string3http://schemas.microsoft.com/2003/10/Serialization/�{{"StartTid":null,"Id":"c594eab8-382c-49ec-ba9c-2db0aaf5e167","CreationDate":"2018-02-11T17:42:42.9823622Z","TenantId":null}

Per my testing, the Message property for the Service Bus action on Azure Portal is treated as set the string message. If you use the WindowsAzure.ServiceBus package, you could leverage the following code to retrieve your message:

var messageData = message.GetBody<string>();

And I assumed that Azure side use the approach as WindowsAzure.ServiceBus to send the message as follows:

myTopicClient.Send(new BrokeredMessage("<the-value-of-the-Message-property-you-set>"));

For your issue, you could use the approach for receiving the message by using the WindowsAzure.ServiceBus. I just de-compiled the WindowsAzure.ServiceBus library, you could use the following code:

var mySerializer = new DataContractSerializer(typeof(string));
var obj = mySerializer.ReadObject(XmlDictionaryReader.CreateBinaryReader(new MemoryStream(message.Body), XmlDictionaryReaderQuotas.Max));
Console.WriteLine($"message received: {obj}");

TEST:

enter image description here