Kieren Johnstone
Kieren Johnstone

Reputation: 41983

Azure Service Bus - topics, messages - using .NET Core

I'm trying to use Azure Service Bus with .NET Core. Obviously at the moment, this kind of sucks. I have tried the following routes:

  1. The official SDK: doesn't work with .NET Core
  2. AMQP.Net Lite: no (decent) documentation, no management APIs around creating/listing topics, etc. Only Service Bus examples cover a small subset of functionality and need you to have a topic already, etc
  3. The community wrapper around AMQP.Net Lite which mirrors the Azure SDK (https://github.com/ppatierno/azuresblite): doesn't work with .NET Core

Then, I moved on to REST.

https://azure.microsoft.com/en-gb/documentation/articles/service-bus-brokered-tutorial-rest/ is a good start (although no RestSharp support for .NET Core either, and for some reason the official SDK doesn't seem to cover a REST client - no Swagger def, no AutoRest client, etc). Although this crappy example concatenates strings into XML without encoding, and covers a small subset of functionality.

So I decided to look for REST documentation. There are two sections, "classic" REST and just REST. Plain-old new REST doesn't support actually sending and receiving messages it seems (...huh?). I'm loathed to use an older technology labelled "classic" unless I can understand what it is - of course, docs are no help here. It also uses XML and ATOM rather than JSON. I have no idea why.

Bonus: the sample linked to in the documentation for the REST API, e.g. from https://msdn.microsoft.com/en-US/library/azure/hh780786.aspx, no longer exists.

Are there any viable approaches anyone has managed to use to read/write messages to topics/from subscriptions with Azure Service Bus and .NET Core?

Upvotes: 11

Views: 5374

Answers (3)

Mik
Mik

Reputation: 4416

The support for Azure Service Bus in .Net Core is getting better and better. There is a dedicated nuget package for it: Microsoft.Azure.ServiceBus. As for now (March 2018) it supports most of the scenarios that you might need, altough there are some gaps, like:

  • receiving messages in batches
  • checking if topic / queue / subscription exists
  • creating new topic / queue / subscription from code

As for OnMessage support for receiving messages, there is a new method: RegisterMessageHandler, that does the same thing.

Here is a code sample how it can be used:

public class MessageReceiver
{
    private const string ServiceBusConnectionString = "Endpoint=sb://bialecki.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=[privateKey]";

    public void Receive()
    {
        var subscriptionClient = new SubscriptionClient(ServiceBusConnectionString, "productRatingUpdates", "sampleSubscription");

        try
        {
            subscriptionClient.RegisterMessageHandler(
                async (message, token) =>
                {
                    var messageJson = Encoding.UTF8.GetString(message.Body);
                    var updateMessage = JsonConvert.DeserializeObject<ProductRatingUpdateMessage>(messageJson);

                    Console.WriteLine($"Received message with productId: {updateMessage.ProductId}");

                    await subscriptionClient.CompleteAsync(message.SystemProperties.LockToken);
                },
                new MessageHandlerOptions(async args => Console.WriteLine(args.Exception))
                { MaxConcurrentCalls = 1, AutoComplete = false });
        }
        catch (Exception e)
        {
            Console.WriteLine("Exception: " + e.Message);
        }
    }
}

For full information have a look at my blog posts:

Sending messages in .Net Core: http://www.michalbialecki.com/2017/12/21/sending-a-azure-service-bus-message-in-asp-net-core/

Receiving messages in .Net Core: http://www.michalbialecki.com/2018/02/28/receiving-messages-azure-service-bus-net-core/

Upvotes: 2

Youngjae
Youngjae

Reputation: 25050

Still there's not sufficient support for OnMessage implementation which I think the-most important thing in ServiceBus, .Net Core version of ServiceBus was rolled out several days ago.

Upvotes: 3

NeroS
NeroS

Reputation: 1189

Unfortunately, as of time of this writing, your only options for using service bus are either to roll your own if you want to use Azure Storage, or an alternative third party library, such as Hangfire, which has a sort-of-queue in form of Sql Server storage.

Upvotes: 1

Related Questions