Skip to content

Instantly share code, notes, and snippets.

@BadgerCode
Last active January 26, 2019 11:40
Show Gist options
  • Save BadgerCode/260745dd28d0784b95664790ec013d4c to your computer and use it in GitHub Desktop.
Save BadgerCode/260745dd28d0784b95664790ec013d4c to your computer and use it in GitHub Desktop.
Azure function service bus interop
  • Old format: BrokeredMessage = .net Framework format (WindowsAzure.ServiceBus)
  • New format: Message = .net standard format (Microsoft.Azure.ServiceBus)

More info

Consuming

A BrokeredMessage was produced. I wish to consume it as a Message.

using Microsoft.Azure.ServiceBus;
using Microsoft.Azure.ServiceBus.InteropExtensions; // This is in the Microsoft.Azure.ServiceBus package
...
Message queueMessage = ...
string rawBody = queueMessage.GetBody<string>();
// If the code above doesn't work, get it from:
// https://github.com/Azure/azure-service-bus-dotnet/blob/dev/src/Microsoft.Azure.ServiceBus/Extensions/MessageInterOpExtensions.cs

A Message was produced. I wish to consume it as a BrokeredMessage.

using using Microsoft.ServiceBus.Messaging;
using System.IO;
...
BrokeredMessage queueMessage = ...
string message = new StreamReader(queueMessage.GetBody<Stream>()).ReadToEnd();

Producing

I have created a Message. It will be consumed as a BrokeredMessage.

using System.IO;
using Microsoft.Azure.ServiceBus;
using Microsoft.Azure.ServiceBus.InteropExtensions;
...

string rawBody = "...";
Message serviceBusMessage;

var serializer = DataContractBinarySerializer<string>.Instance;
using (var stream = new MemoryStream())
{
    serializer.WriteObject(stream, rawBody);
    serviceBusMessage = new Message(stream.ToArray());
}

I have created a BrokeredMessage. It will be consumed as a Message.

string rawBody = "...";
var stream = new MemoryStream(Encoding.UTF8.GetBytes(rawBody));
var serviceBusMessage = new BrokeredMessage(stream);
@BadgerCode
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment