Tuesday, June 5, 2018

ServiceBus Serialization Scenarios

ServiceBus Serialization Scenarios.
Use BrokeredMessage with explicit json to understand whats going on.
E.g. for using in Azure functions
class MyData
{
 public string name {get;set;}
}

// Sender
[FunctionName("Send")]
public static void Send(..., [ServiceBus("%Topic%")] out BrokeredMessage brokeredMessage, ...)
{
 ...
 MyData myData = new MyData();
 string bodyJson = JsonConvert.SerializeObject(myData);
 brokeredMessage = new BrokeredMessage(bodyJson);
 brokeredMessage.Label = "My routing info";
 ...
}

// Receiver
[FunctionName("Receive")]
public static void Receive([ServiceBusTrigger("%Topic%", "%Subscription%")] BrokeredMessage brokeredMessage, ...)
{
 ...
 string bodyJson = brokeredMessage.GetBody<string>();
 MyData myData = JsonConvert.DeserializeObject<MyData>(bodyJson);
 ...
}

If you want to send a test message using Service Bus Explorer in the send message dialog just paste the json e.g. {"name":"Andreas"} into Message -> Message Text and then set Sender -> Body Type to String (i.e. not Stream). Set the Label as well if you want to route the message.
ServiceBus Serialization Scenarios
Messages, payloads, and serialization
Azure Service Bus bindings for Azure Functions

No comments: