Service Bus is, according to Microsoft, “...a generic, cloud-based messaging system for connecting just about anything.” Most commonly used as an Azure service, it can be an excellent tool for managing non-critical workloads within an application and offers the benefit of being AMQP compatible when compared to Amazon’s SQS. Connecting to Service Bus (SB) on Windows is simple, but will the new .NET Core (DNX) platform be capable of the task?

Library Adventures

As of the writing of this post, Microsoft hasn’t yet released an updated version of its Azure Service Bus tools that is compatible with DNX. Thankfully, the AMQP compatibility makes it easy for libraries to be built that don’t depend on Microsoft.

After a bit of searching, I was able to find just such a library, AzureSBLite, by Paolo Patierno (who unsurprisingly appears to be a member of the RedHat organization on GitHub). The library doesn’t explicitly state it is compatible with DNX, but I figured I’d give it a shot regardless.

A bit of trial and error led me through the following missteps:

  • Had to make sure the https://api.nuget.org/v3/index.json NuGet feed was added to my ~/.nuget/NuGet/NuGet.Config feed list
  • Had to add the AmqpNetLite dependency to my project.json dependencies:
    • “AmqpNetLite”: { “version”: “1.1.9-rc” }

Short and sweet, the library was compiling and ready for me to build code against.

The Code

I added a Program.cs file to the project and threw together my code. I won’t go through every single line, but there are certainly a few things to note:

  • SBConnStr, SBTopicName, and SBTopicSubscriptionName are values I copied directly from the Azure Service Bus management portal, available at: https://manage.windowsazure.com/ (at least while they work on moving it to the new portal)
    • In order to get a topic/subscription option for your SB queue, you’ll need to make sure you have upgraded your SB service to the Standard package instead of the Basic messaging tier
  • You’ll want to be sure you are copying the connection string with information specific to the access policy for your topic/subscription.
  • It seems the client and factory objects in the AzureSBLite library do not currently implement IDisposable; I will likely be submitting a pull request for this myself as I tend to prefer to leave cleanup to the IL compiler instead of trusting myself to call client.Close()
  • The subscription name is only relevant when you begin polling for messages on a topic, so no subscription name is supplied when pushing a message onto the queue.
  • I’ve added a limit to the number of times a queue is polled, but you will likely have different ways you’ll want to handle polling. (There are relevant details on this for pricing of the service, which can be found here.)
  • Don’t forget to mark your received messages as Complete. Not doing so will cause them to be continually consumed by all clients you have connected.

In the end, the code was fairly straightforward to produce and the library worked perfectly once it was compiling.

Conclusion

I’m not sure whether or not Microsoft plans to release a version of the Azure tools for DNX, especially given how they have been supporting the AmqpNetLite project recently that powers AzureSBLite. The good news is that even if they don’t release one, libraries like these make it not only possible but also easy to consume Azure-hosted Service Bus messages anywhere that .NET Core will run.

For additional information and articles on .NET Core visit our .NET Core web page for more on this topic.

About Andrew Male

Andrew Male (@AndyM84) is a senior engineer at an enterprise development company in Boston, MA. Andrew has been programming from a young age and is entirely self-taught; he has spent time in many corners of the programming world including game/VR work, agency work, and teaching development to students and adults alike. He spends most of his time working on architecture design and pursuing his favorite hobby—physics.

Last updated: September 3, 2019