Home > Blockchain >  Migration from Microsoft.Azure.ServiceBus to Azure.Messaging.ServiceBus
Migration from Microsoft.Azure.ServiceBus to Azure.Messaging.ServiceBus

Time:11-16

I'm trying to update this function to make use of Azure.Messaging.ServiceBus and drop Microsoft.Azure.ServiceBus altogether, however cant seem to find any resources for this. Anybody knows how to send a message to a topic using this package?

The older function is:

  public async Task SendMessageToServiceBusTopic<T>(T request, string topicSubName, string submissionNumber)
    {
        ServiceBusConnectionStringBuilder serviceBusConnectionStringBuilder =
            new ServiceBusConnectionStringBuilder(settings.ServiceBusConnectionString)
            {
                EntityPath = settings.ServiceBusTopic
            };

     
            TopicClient topicClient = new TopicClient(serviceBusConnectionStringBuilder);

            byte[] bytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(request));

            await topicClient.SendAsync(new Message(bytes)
            {
                CorrelationId = context.CorrelationId,
                Label=topicSubName,
                UserProperties = { new KeyValuePair<string, object>("TrackingId", submissionNumber) }
            });          
    }

So far I have managed:

Am i headed in the right direction?

  public async Task SendMessageToServiceBusTopic<T>(T request, string topicSubName, string submissionNumber)
    {
        ServiceBusClient client = new ServiceBusClient(settings.ServiceBusConnectionString);
        ServiceBusSender s = client.CreateSender(settings.ServiceBusTopic);


            byte[] bytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(request));
            await s.SendMessageAsync(new ServiceBusMessage(bytes));
      }

CodePudding user response:

you're in the right direction. There's no easy migration tool/sample as they are different libraries dealing with the same service (Azure Service Bus).

CodePudding user response:

While you can construct a Service Bus Client each time, it's not ideal. Assuming you're using the latest In-Proc SDK, you can use one of these options:

[FunctionName("PublishToTopic")]
public static async Task Run(
    [TimerTrigger("0 */5 * * * *")] TimerInfo myTimer,
    [ServiceBus("<topic_name>", Connection = "<connection_name>")] IAsyncCollector<ServiceBusMessage> collector)
{
    await collector.AddAsync(new ServiceBusMessage(new BinaryData($"Message 1 added at: {DateTime.Now}")));
    await collector.AddAsync(new ServiceBusMessage(new BinaryData($"Message 2 added at: {DateTime.Now}")));
}

Alternatively,

[FunctionName("PublishWithSender"]
public static async Task Run(
    [TimerTrigger("0 */5 * * * *")] TimerInfo myTimer,
    [ServiceBus("<topic_name>", Connection = "<connection_name>")] ServiceBusSender sender)
{
    await sender.SendMessagesAsync(new[]
    {
        new ServiceBusMessage(new BinaryData($"Message 1 added at: {DateTime.Now}")),
        new ServiceBusMessage(new BinaryData($"Message 2 added at: {DateTime.Now}"))
    });
}

For Isolated Worker SDK it's somewhat different. See this post for details.

  • Related