简体   繁体   中英

Replacing Microsoft.Azure.ServiceBus with Azure.Messaging.ServiceBus

I have a message publisher that uses Microsoft.Azure.ServiceBus and need to replace it with Azure.Messaging.ServiceBus as it is now deprecated.

Here is the code:

using Microsoft.Azure.ServiceBus;
using Newtonsoft.Json;
using System;
using System.Text;
using System.Threading.Tasks;

namespace gbx.infra.ware.Services
{
public interface IMessagePublisher
{
    public Task Publish<T>(T obj);
    public Task Publish(string raw);
    public Task<long> PublishScheduled<T>(T obj, DateTimeOffset time);

}
public class MessagePublisher : IMessagePublisher
{
    private readonly ITopicClient _topicClient;

    public MessagePublisher(ITopicClient topicClient)
    {
        _topicClient = topicClient;
    }

    public Task Publish<T>(T obj)
    {
        string objAsText = JsonConvert.SerializeObject(obj);

        Message message = new Message(Encoding.UTF8.GetBytes(objAsText));
        message.UserProperties["messageType"] = typeof(T).Name;
        return _topicClient.SendAsync(message);
    }

    public Task Publish(string raw)
    {
        Message message = new Message(Encoding.UTF8.GetBytes(raw));
        message.UserProperties["messageType"] = "Raw";
        return _topicClient.SendAsync(message);
    }

    public Task<long> PublishScheduled<T>(T obj, DateTimeOffset time)
    {
        string objAsText = JsonConvert.SerializeObject(obj);

        Message message = new Message(Encoding.UTF8.GetBytes(objAsText));
        message.UserProperties["messageType"] = typeof(T).Name;
        return _topicClient.ScheduleMessageAsync(message, time);
    }
    }
    }

Is there a simple way i can make the change? I can't find any info on this.

The publisher is registered like this:

services.AddSingleton<ITopicClient>(x => new TopicClient(Configuration["ServiceBus:ConnectionString"], Configuration["ServiceBus:TopicName"]));
services.AddSingleton<IMessagePublisher, MessagePublisher>();

And injected into as needed.

What i want to do is to change the code in the Message Publisher only so that no changes need to be done in the code where it is used.

If you would simply use the migration guide you would end up with

public interface IMessagePublisher
{
    public Task Publish<T>(T obj);
    public Task Publish(string raw);
    public Task<long> PublishScheduled<T>(T obj, DateTimeOffset time);

}

public class MessagePublisher : IMessagePublisher
{
    private readonly ServiceBusSender _serviceBusSender;

    public MessagePublisher(ServiceBusSender serviceBusSender)
    {
        _serviceBusSender = serviceBusSender;
    }

    public Task Publish<T>(T obj)
    {
        string objAsText = JsonConvert.SerializeObject(obj);

        ServiceBusMessage message = new ServiceBusMessage(Encoding.UTF8.GetBytes(objAsText));
        message.ApplicationProperties["messageType"] = typeof(T).Name;
        return _serviceBusSender.SendMessageAsync(message);
    }

    public Task Publish(string raw)
    {
        ServiceBusMessage message = new ServiceBusMessage(Encoding.UTF8.GetBytes(raw));
        message.ApplicationProperties["messageType"] = "Raw";
        return _serviceBusSender.SendMessageAsync(message);
    }

    public Task<long> PublishScheduled<T>(T obj, DateTimeOffset time)
    {
        string objAsText = JsonConvert.SerializeObject(obj);

        ServiceBusMessage message = new ServiceBusMessage(Encoding.UTF8.GetBytes(objAsText));
        message.ApplicationProperties["messageType"] = typeof(T).Name;
        return _serviceBusSender.ScheduleMessageAsync(message, time);
    }
}

To register the MessagePublisher try:

services.AddSingleton<IMessagePublisher>(p => 
        new MessagePublisher(
        new ServiceBusClient(Configuration["ServiceBus:ConnectionString"]).CreateSender(Configuration["ServiceBus:TopicName"])));

Note: there are other ways to register the message publisher, see How to register ServiceBusClient for dependency injection? . You might need some minor modifications of MessagePublisher though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM