[rabbitmq-discuss] .NET Client Library Shared Queue Closed Error on Subscription When No Msgs in Queue

BillC billc at inworksol.com
Sun Mar 30 10:25:40 BST 2008


Please can you help me with the following error.  Using the examples that come with the .NET Cllient Library I have written a small program that creates a subscription and reads any published messages in the queue.  The code works and reads any messages in the queue, however when there are no more messages left to read in the Queue or when you run the program without any messages in the queue (i.e. when the queue is empty) the connection simply closes and throws the following EndOfStreamException:

System.IO.EndOfStreamException: SharedQueue closed
   at RabbitMQ.Util.SharedQueue.EnsureIsOpen()
   at RabbitMQ.Util.SharedQueue.Dequeue()
   at ConsoleApplication2.Program.Main(String[] args) in C:\rabbitmqdotnet\testw
indowsapp\ConsoleApplication2\ConsoleApplication2\Program.cs:line 47

Here is a listing of the source code.  Thanks for all your help.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RabbitMQ.Client;
using System.IO;
using RabbitMQ.Client.Content;
using RabbitMQ.Client.Events;
using RabbitMQ.Client.MessagePatterns;
using RabbitMQ.Util;


namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {

            IProtocol protocol = Protocols.FromConfiguration("my-protocol");
            string hostName = "10.10.10.25";
            int portNumber = 5678;
            string exchangeName = "testexchange";
            string queueName = "testqueue";
            string routingKey = "routingkey";
            ConnectionFactory factory = new ConnectionFactory();
            IConnection conn = factory.CreateConnection(protocol, hostName, portNumber);
            IModel channel = conn.CreateModel();
            string realm = "/data";
            ushort ticket = channel.AccessRequest(realm);
            channel.ExchangeDeclare(ticket, exchangeName, ExchangeType.Direct);
            channel.QueueDeclare(ticket, queueName);
            channel.QueueBind(ticket, queueName, exchangeName, routingKey, false, null);

            QueueingBasicConsumer consumer = new QueueingBasicConsumer(channel);
            channel.BasicConsume(ticket, queueName, null, consumer);
            Console.WriteLine("Entering Loop .. Please press Control C to exit");
            while (true)
            {
                try
                {
                    
                    else
                    {
                        RabbitMQ.Client.Events.BasicDeliverEventArgs e2 =
                        (RabbitMQ.Client.Events.BasicDeliverEventArgs)consumer.Queue.Dequeue();
                        IBasicProperties props = e2.BasicProperties;
                        byte[] body = e2.Body;
                        
                        string str = System.Text.Encoding.UTF8.GetString(body);
                        
                        Console.WriteLine(str);
                        channel.BasicAck(e2.DeliveryTag, false);
                    }
                }
                catch (EndOfStreamException ex)
                {
                    // The consumer was removed, either through
                    // channel or connection closure, or through the
                    // action of IModel.BasicCancel().
                    
                    Console.WriteLine(ex.ToString());
                  break;
                }
            }
        }
    }
}








More information about the rabbitmq-discuss mailing list