1

I am currently working with the rabbitMQ server, As when i try working in c# console application, the publish exchange working and successfully save in the server and the message will lively appear in the console but when i apply my source code in the C# windows form, it will not get all the message sent by the publisher. I put the method in the constructor event but no happen at all it will not receive any message.

Please see source code below

using Publisher;
using RabbitMQ.Client;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Timers;
using RabbitMQ.Client.Events;
using System.Diagnostics;

namespace Consumer
{
    public partial class Consumer : Form
    {

        private EventingBasicConsumer consumer;
        ConnectionFactory factory;
        public Consumer()
        {
            factory = new ConnectionFactory() { HostName = Definition.HOSTNAME };
            using (var connection = factory.CreateConnection())
            using (var channel = connection.CreateModel())
            {
                channel.ExchangeDeclare(exchange: Definition.EXCHANGE, type: ExchangeType.Fanout);

                var queueName = channel.QueueDeclare().QueueName;
                channel.QueueBind(queue: queueName,
                                  exchange: Definition.EXCHANGE,
                                  routingKey: "");




                Debug.WriteLine(" [*] Waiting for Exchange ARexchange.");
                var consumer = new EventingBasicConsumer(channel);
                consumer.Received += (sender, ea) =>
                {
                   var body = ea.Body;
                   var message = Encoding.UTF8.GetString(body);
                   Debug.WriteLine(" [x] {0}", message);
                 };
                channel.BasicConsume(
                                queueName,
                                 autoAck: false,
                                 consumer: consumer);
            }

            InitializeComponent();


        }

        private void Consumer_Load(object sender, EventArgs e)
        {

        }
        private void setExchange()
        {
            lblExchange.Text = Definition.EXCHANGE;
        }

    }
}
  • Is the Form serving any purpose here? – Robert Harvey Nov 20 '19 at 04:45
  • Hi @RobertHarvey i updated my source code, for testing purposes i just Write down Debug.Writeline in my code to test if it will return the message. The form have no serving yet here. – Hard Harry Nadela Nov 20 '19 at 04:51
  • Well, the form constructor isn't a place where you can hold persistent variables, like `connection` and `channel`. Once the constructor finishes executing, those variables vanish. – Robert Harvey Nov 20 '19 at 05:19
  • Ah yup i tried to put it in the constructor to test if it work but sad to say it didn't work, i tried also put into the form_load method but same noting happened . – Hard Harry Nadela Nov 20 '19 at 05:25
  • @RobertHarvey when i create a console application same with the above source code it will receive message and working properly but when i apply it on the windows form application i didn't work. – Hard Harry Nadela Nov 20 '19 at 05:28

1 Answers1

2

Please read this: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-statement

using (var connection = factory.CreateConnection())
using (var channel = connection.CreateModel())

When those using statements exit, the channel and connection will be closed. Change your code to save those as instance variables in the Consumer class. Then, when your form exits, you can clean up those two instances.

If you provide your code in a repository that can be cloned, compiled and run people could assist you by submitting pull requests to improve your code.


NOTE: the RabbitMQ team monitors the rabbitmq-users mailing list and only sometimes answers questions on StackOverflow.

Luke Bakken
  • 8,993
  • 2
  • 20
  • 33