Convert RabbitMQ messages into List using C# in Console Application

ruben00000
 
on Sep 09, 2022 11:01 PM
255 Views

I need parse array payload into list model for other post data send message but i still get result array not list string

var factory = new ConnectionFactory
{
    HostName = "localhost"
};
 
var connection = factory.CreateConnection(); 
using var channel = connection.CreateModel(); 
channel.QueueDeclare("email_confirmation_notifications", exclusive: false, autoDelete: false);
 
var consumer = new EventingBasicConsumer(channel); 
string[] splitItems = null; 
consumer.Received += (model, eventArgs) =>
{
    var body = eventArgs.Body.ToArray();
    var message = Encoding.UTF8.GetString(body);
    channel.BasicAck(eventArgs.DeliveryTag, false);
    Console.WriteLine($"Notification message received: {message}");
};
 
channel.BasicConsume(queue: "email_confirmation_notifications", autoAck: true, consumer: consumer); 
Console.ReadKey();

my sample list messsage array is

first message :

["test123@gmail.com","smtp.email.io","admin","2525","123","Test_200999@yahoo.com","Email Confirmation","Hello World"]

second message :

["test123@gmail.com","smtp.email.io","admin","2525","123","Test_200998@yahoo.com","Email Confirmation","Hello World"]

third message

["test123@gmail.com","smtp.email.io","admin","2525","123","Test_200997@yahoo.com","Email Confirmation","Hello World"]

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Sep 13, 2022 06:04 AM

Hi ruben00000,

Use below code to convert RabbitMQ messages into List.

var factory = new ConnectionFactory
{
    HostName = "localhost"
};

var connection = factory.CreateConnection();
using var channel = connection.CreateModel();
channel.QueueDeclare("email_confirmation_notifications", exclusive: false, autoDelete: false);
var consumer = new EventingBasicConsumer(channel);
List<EmailModel> _model = new List<EmailModel>();
consumer.Received += (model, eventArgs) =>
{
    var body = eventArgs.Body.ToArray();
    List<string> message = Newtonsoft.Json.JsonConvert.DeserializeObject<List<string>>(body);
    EmailModel data = new EmailModel();
    data.Sender = message[0];
    data.To = message[5];

    Console.WriteLine($"Notification message received: {string.Join(", ", message)}");
}