[rabbitmq-discuss] how to close a connection in dotnet?

Geoffrey Anderson mrcoder at yahoo.com
Mon Aug 11 23:51:54 BST 2008


Just a basic question on opening and closing the connection...

When I call this line of C# code using the .net library 1.4.0 :

mqConnection.Close();

I quickly get this exception:

System.ObjectDisposedException was unhandled
  Message="Cannot access a disposed object.\r\nObject name:
'System.Net.Sockets.Socket'."
  Source="System"
  ObjectName="System.Net.Sockets.Socket"
  StackTrace:
       at System.Net.Sockets.TcpClient.set_ReceiveTimeout(Int32 value)
       at RabbitMQ.Client.Impl.SocketFrameHandler_0_9.set_Timeout(Int32 value)
       at RabbitMQ.Client.Impl.ConnectionBase.ClosingLoop()
       at RabbitMQ.Client.Impl.ConnectionBase.MainLoop()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext,
ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
       
       
       
Here is the code which established my connection before I called the Close method.
 
Is there a mistake in the way I set up the connection which might cause this socket
error?  It seems to run fine and there are no exceptions except for the later Close
statement.


        void OpenOutputQueue()
        {
            mqFactory = new ConnectionFactory();
            mqFactory.Parameters.UserName = Cfg("MessageQueueUserName");
            mqFactory.Parameters.Password = Cfg("MessageQueuePassword");
            mqFactory.Parameters.VirtualHost = Cfg("MessageQueueVirtualHost", "/");
            mqFactory.Parameters.RequestedHeartbeat = 0;
            //mqFactory.Parameters.RequestedHeartbeat = 20;
            IProtocol mqProtocol = Protocols.FromEnvironment();
            mqHost = Cfg("MessageQueueHost", "localhost");
            mqConnection = mqFactory.CreateConnection(mqProtocol, mqHost);
            mqModel = mqConnection.CreateModel();
            mqTicket = mqModel.AccessRequest(Cfg("MessageQueueRealm"));
            mqExchange = Cfg("MessageQueueExchange");
            mqModel.ExchangeDeclare(mqTicket, mqExchange, ExchangeType.Direct);
            mqQueueName = Cfg("MessageQueueName");
            mqModel.QueueDeclare(mqTicket, mqQueueName, false);
            mqRoutingKey = Cfg("MessageQueueRoutingKey");
            mqModel.QueueBind(mqTicket, mqQueueName, mqExchange, mqRoutingKey,
false, null);
            mqConnection.AutoClose = false;
        }



Sorry to waste time if I missed something in the manuals.

Thanks for reading.

Geoffrey Anderson




More information about the rabbitmq-discuss mailing list