0
votes

I'm using Azure functions to send and receive messages from service bus. I want to send and receive correlationId in service bus message header to track message lifecycle.

I'm sending first JSON payload from ServiceBusExplore, and getting text If using string insted of BrokeredMessage

Receiver Code

public static async Task RunAsync([ServiceBusTrigger("validate-trade", AccessRights.Manage,
        Connection = "ServiceBusConnection")]BrokeredMessage message,
        TraceWriter log)
    {
        string queueData = message.GetBody<string>();
}

getting below error

'message.GetBody<string>()' threw an exception of type 'System.Runtime.Serialization.SerializationException'    string {System.Runtime.Serialization.SerializationException}

I tried this as well

string s = receiveMessage.GetBody<string>(new DataContractSerializer(typeof(string)))
1

1 Answers

1
votes

This should be the safest way to read contents of any Service Bus message as pure string:

string body;
using (var stream = message.GetBody<Stream>())
using (var streamReader = new StreamReader(stream, Encoding.UTF8))
{
    body = streamReader.ReadToEnd();
}