[rabbitmq-discuss] System.ObjectDisposedException

Derek Johnston derekejohnston at gmail.com
Wed Feb 19 15:21:51 GMT 2014


Hi

keep getting this error
System.ObjectDisposedException was unhandled
Message: An unhandled exception of type 'System.ObjectDisposedException'
occurred in System.dll


here is my code taken from here
http://freshbrewedcode.com/davidneal/2011/11/30/scale-windows-services-with-rabbitmq/

static void Main()
{
// Set up the RabbitMQ connection and channel
var connectionFactory = new ConnectionFactory
{
HostName = "localhost",
Port = 5672,
UserName = "guest",
Password = "guest",
Protocol = Protocols.AMQP_0_9_1,
RequestedFrameMax = UInt32.MaxValue,
RequestedHeartbeat = UInt16.MaxValue
};

using (var connection = connectionFactory.CreateConnection())
using (var channel = connection.CreateModel())
{
// This instructs the channel not to prefetch more than one message
channel.BasicQos(0, 1, false);

// Create a new, durable exchange
channel.ExchangeDeclare("sample-ex", ExchangeType.Direct, true, false,
null);
// Create a new, durable queue
channel.QueueDeclare("sample-queue", true, false, false, null);
// Bind the queue to the exchange
channel.QueueBind("sample-queue", "sample-ex", "optional-routing-key");

using (var subscription = new Subscription(channel, "sample-queue", false))
{
Console.WriteLine("Waiting for messages...");
var encoding = new UTF8Encoding();
while (channel.IsOpen)
{
BasicDeliverEventArgs eventArgs;
var success = subscription.Next(2000, out eventArgs);
if (success == false) continue;
var msgBytes = eventArgs.Body;
var message = encoding.GetString(msgBytes);
Console.WriteLine(message);
channel.BasicAck(eventArgs.DeliveryTag, false);
}
}
}
}

/++++++++++++++++++++++++++++++++++++++++++++++++/

private static void Main()
{
// Set up the RabbitMQ connection and channel
var connectionFactory = new ConnectionFactory
                         {
                         HostName = "localhost",
                         Port = 5672,
                         UserName = "guest",
                         Password = "guest",
                         Protocol = Protocols.AMQP_0_9_1,
                         RequestedFrameMax = UInt32.MaxValue,
                         RequestedHeartbeat = UInt16.MaxValue
                         };

using (var connection = connectionFactory.CreateConnection())
using (var channel = connection.CreateModel())
{
// Create a new, durable exchange
channel.ExchangeDeclare("sample-ex", ExchangeType.Direct, true, false,
null);
// Create a new, durable queue
channel.QueueDeclare("sample-queue", true, false, false, null);
// Bind the queue to the exchange
channel.QueueBind("sample-queue", "sample-ex", "optional-routing-key");

// Set up message properties
var properties = channel.CreateBasicProperties();
properties.DeliveryMode = 2; // Messages are persistent and will survive a
server restart

// Ready to start publishing
// The message to publish can be anything that can be serialized to a byte
array,
// such as a serializable object, an ID for an entity, or simply a string
var encoding = new UTF8Encoding();
for (var i = 0; i < 10; i++)
{
var msg = string.Format("This is message derek #{0}?", i+1);
var msgBytes = encoding.GetBytes(msg);
channel.BasicPublish("sample-ex", "optional-routing-key", false, false,
properties, msgBytes);

}
channel.Close();
}
Console.WriteLine("Messages published");
    Console.ReadKey(true);
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rabbitmq.com/pipermail/rabbitmq-discuss/attachments/20140219/373fedba/attachment.html>


More information about the rabbitmq-discuss mailing list