The story of upgrading ServiceBus NuGet

Cosmin Vladutu
2 min readJul 6, 2022

--

If you are here you probably noticed that Microsoft.Azure.ServiceBus package is deprecated and the authors suggest migrating to Azure.Messaging.ServiceBus.

Steps

Now I will tell you how I tackled this and what were my challenges:

  1. Remove the deprecated package and add the new one.
  2. Don’t be scared after you press build. You’ll see a lot of errors, but it’s not so hard to make the changes.
  3. On the registration part, I had a topic client which looked like this:
ITopicClient topicClient = new TopicClient(serviceBusConnectionString, “MyTopic”);

This got changed into this:

var client = new ServiceBusClient(serviceBusConnectionString);var serviceBusSender= client.CreateSender(“MyTopic”);

Now, you don’t have a topicClient anymore, you need a sender.

4. Where I used it to send a message I had something like this:

await this.topicClient.SendAsync(new Message(){Label =“MyTopic”,Body = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(myNiceObject)),UserProperties = { { “EventId”, myNiceObject.EventId } }});

Now this got changed into

await this.serviceBusSender.SendMessageAsync(new ServiceBusMessage(JsonConvert.SerializeObject(myNiceObject)){Subject = “MyTopic”,ApplicationProperties = { { “EventId”, myNiceObject.EventId } }});

Basically now you can add the object that you want to send directly to the constructor, the Label is now Subject and the UserProperties are now ApplicationProperties. After you have done this you’ll have fewer errors, on your build. Be careful to use ServiceBusMessage for sending messages and ServiceBusReceivedMessag to receive messages! If you’ll use something else, in build time you won’t get any notification that something is wrong, but in runtime, everything will crash.

For sending, this is it!

5. Now let’s talk about receiving messages. For this, I had a function with a trigger (this was my specific case).

What was before the upgrade:

[FunctionName(nameof(MyTrigger))]public async Task Run([ServiceBusTrigger(“MyTopic”,“mySubscriptionName”,Connection = “serviceBusCinnection”)] Message message,ExecutionContext executionContext,[DurableClient] IDurableOrchestrationClient starter)
{
var json = message.Body != null ? Encoding.UTF8.GetString(message.Body) : string.Empty;
}

After the upgrade:

[FunctionName(nameof(MyTrigger))]public async Task Run([ServiceBusTrigger(“MyTopic”,“mySubscriptionName”,Connection = “serviceBusCinnection”)] ServiceBusReceivedMessage message,ExecutionContext executionContext,[DurableClient] IDurableOrchestrationClient starter) 
{
var json = message.Body != null ? message.Body.ToString() : string.Empty;
}

As you see, only the message class was changed and the way, the JSON object is read. Pretty simple, right?

6. The interesting thing was in tests when I wanted to test my trigger. Before I had something like you see below to construct my message

var message = new Message{Body = Encoding.ASCII.GetBytes(body),CorrelationId = Guid.NewGuid().ToString(),};

But now you can’t do that. To mock a ServiceBusReceivedMessage object you need to construct it using ServiceBusModelFactory (which is a static helper). The code will look like this:

var message = ServiceBusModelFactory.ServiceBusReceivedMessage(BinaryData.FromString(body), correlationId: Guid.NewGuid().ToString());

To be honest I lost a few hours until I found out about this.

Conclusion

It’s not hard to do the upgrade, but you need to be a little bit careful when you are doing it. If you didn’t find all the info that you needed in my article, you can find here, on GitHub, the complete migration guide offered by the guys who worked on the new package

--

--

Cosmin Vladutu
Cosmin Vladutu

Written by Cosmin Vladutu

Software Engineer | Azure & .NET Full Stack Developer | Leader

No responses yet