Search code examples
c#.netazureazureservicebusazure-queues

App Service to switch from Azure Topics to Azure Queue?


Is there any good practice, for existing implementation, designed to communicate with Azure ServiceBus Topics, to switch the communication to Azure Queue ... without breaking the Open-Closed principle?

Let's say this is my original service implementation:

   public class AzureServiceBusSender : IServiceBusSender
   {
       private ServiceBusClient _client;
       private ServiceBusSender _sender;

       public AzureServiceBusSender(string connectionString, string topicName)
       {
           _client = new ServiceBusClient(connectionString);
           _sender = _client.CreateSender(topicName);
       }

       public async Task DisposeAsync()
       {
           await _sender.DisposeAsync();
           await _client.DisposeAsync();
       }

       public async Task SendMessageAsync(CustomMessage message, string action)
       {
           string json = JsonConvert.SerializeObject(message);

           var topicMessage = new ServiceBusMessage(json)
           {
               ContentType = "application/json"
           };
           topicMessage.Settings.Add("Action", action);

           await _sender.SendMessageAsync(topicMessage);
       }
   }

Of course, the option to create a separate service for the Azure Queue is on the table, just I want to understand if there is approved method, which can be an answer for my question.


Solution

  • I do agree with @Andrew B and will also think of creating a new class with azure queue service , which sends the messages to azure storage queue as below:

    Program.cs:

        var cho = WebApplication.CreateBuilder(args);
        
        cho.Services.AddRazorPages();
        cho.Services.AddSingleton<IMessageSender>(provider =>
            new Rith_Q_Sender(
                cho.Configuration["rithcon"],
                cho.Configuration["qname"]
            ));
        
        var app = cho.Build();
        using (var sc = app.Services.CreateScope())
        {
            var messageSender = sc.ServiceProvider.GetRequiredService<IMessageSender>();
            var rith = new Rithwik_MSG
            {
                Id = 8,
                Name = "R"
            };
            await messageSender.SendMessageAsync(rith);
        }
        -------
        -------
        app.Run();
    

    Rith_Q_Sender.cs:

    using System.Text.Json;
    using Azure.Storage.Queues;
    
    public class Rith_Q_Sender : IMessageSender
    {
        private readonly QueueClient ri_qc;
    
        public Rith_Q_Sender(string cs, string ri_qn)
        {
            var rqsc = new QueueServiceClient(cs);
            ri_qc = rqsc.GetQueueClient(ri_qn);
            ri_qc.CreateIfNotExists();
        }
    
        public async Task SendMessageAsync(Rithwik_MSG message)
        {
            string val = JsonSerializer.Serialize(message);
            string con_msg = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(val)); ;
            await ri_qc.SendMessageAsync(con_msg);
        }
    }
    
    public class Rithwik_MSG
    {
        public string Name { get; set; }
        public int Id { get; set; }
    }
    public interface IMessageSender
    {
        Task SendMessageAsync(Rithwik_MSG message);
    }
    
    

    Output:

    enter image description here