<div dir="ltr">Hi<div><br></div><div>keep getting this error </div><div><div>System.ObjectDisposedException was unhandled</div><div>Message: An unhandled exception of type 'System.ObjectDisposedException' occurred in System.dll</div>
<div><br></div></div><div><br></div><div>here is my code taken from here <a href="http://freshbrewedcode.com/davidneal/2011/11/30/scale-windows-services-with-rabbitmq/">http://freshbrewedcode.com/davidneal/2011/11/30/scale-windows-services-with-rabbitmq/</a></div>
<div><br></div><div><div>static void Main()</div><div><span class="" style="white-space:pre">               </span>{</div><div><span class="" style="white-space:pre">                  </span>// Set up the RabbitMQ connection and channel</div><div><span class="" style="white-space:pre">                      </span>var connectionFactory = new ConnectionFactory</div>
<div><span class="" style="white-space:pre">                    </span>{</div><div><span class="" style="white-space:pre">                          </span>HostName = "localhost",</div><div><span class="" style="white-space:pre">                          </span>Port = 5672,</div>
<div><span class="" style="white-space:pre">                            </span>UserName = "guest",</div><div><span class="" style="white-space:pre">                              </span>Password = "guest",</div><div><span class="" style="white-space:pre">                              </span>Protocol = Protocols.AMQP_0_9_1,</div>
<div><span class="" style="white-space:pre">                            </span>RequestedFrameMax = UInt32.MaxValue,</div><div><span class="" style="white-space:pre">                               </span>RequestedHeartbeat = UInt16.MaxValue</div><div><span class="" style="white-space:pre">                       </span>};</div>
<div><br></div><div><span class="" style="white-space:pre">                   </span>using (var connection = connectionFactory.CreateConnection())</div><div><span class="" style="white-space:pre">                      </span>using (var channel = connection.CreateModel())</div>
<div><span class="" style="white-space:pre">                    </span>{</div><div><span class="" style="white-space:pre">                          </span>// This instructs the channel not to prefetch more than one message</div><div><span class="" style="white-space:pre">                                </span>channel.BasicQos(0, 1, false);</div>
<div><br></div><div><span class="" style="white-space:pre">                           </span>// Create a new, durable exchange</div><div><span class="" style="white-space:pre">                          </span>channel.ExchangeDeclare("sample-ex", ExchangeType.Direct, true, false, null);</div>
<div><span class="" style="white-space:pre">                            </span>// Create a new, durable queue</div><div><span class="" style="white-space:pre">                             </span>channel.QueueDeclare("sample-queue", true, false, false, null);</div>
<div><span class="" style="white-space:pre">                            </span>// Bind the queue to the exchange</div><div><span class="" style="white-space:pre">                          </span>channel.QueueBind("sample-queue", "sample-ex", "optional-routing-key");</div>
<div><br></div><div><span class="" style="white-space:pre">                           </span>using (var subscription = new Subscription(channel, "sample-queue", false))</div><div><span class="" style="white-space:pre">                              </span>{</div>
<div><span class="" style="white-space:pre">                                    </span>Console.WriteLine("Waiting for messages...");</div><div><span class="" style="white-space:pre">                                    </span>var encoding = new UTF8Encoding();</div><div><span class="" style="white-space:pre">                                 </span>while (channel.IsOpen)</div>
<div><span class="" style="white-space:pre">                                    </span>{</div><div><span class="" style="white-space:pre">                                          </span>BasicDeliverEventArgs eventArgs;</div><div><span class="" style="white-space:pre">                                           </span>var success = subscription.Next(2000, out eventArgs);</div>
<div><span class="" style="white-space:pre">                                            </span>if (success == false) continue;</div><div><span class="" style="white-space:pre">                                            </span>var msgBytes = eventArgs.Body;</div><div><span class="" style="white-space:pre">                                             </span>var message = encoding.GetString(msgBytes);</div>
<div><span class="" style="white-space:pre">                                            </span>Console.WriteLine(message);</div><div><span class="" style="white-space:pre">                                                </span>channel.BasicAck(eventArgs.DeliveryTag, false);</div><div><span class="" style="white-space:pre">                                    </span>}</div>
<div><span class="" style="white-space:pre">                            </span>}</div><div><span class="" style="white-space:pre">                  </span>}</div><div><span class="" style="white-space:pre">          </span>}</div></div><div><br></div><div>/++++++++++++++++++++++++++++++++++++++++++++++++/</div>
<div><br></div><div><div><span class="" style="white-space:pre">        </span>private static void Main()</div><div><span class="" style="white-space:pre">         </span>{</div><div><span class="" style="white-space:pre">                  </span>// Set up the RabbitMQ connection and channel</div>
<div><span class="" style="white-space:pre">                    </span>var connectionFactory = new ConnectionFactory</div><div><span class="" style="white-space:pre">                      </span>                        <span class="" style="white-space:pre">  </span>{</div>
<div><span class="" style="white-space:pre">                    </span>                        <span class="" style="white-space:pre">          </span>HostName = "localhost",</div><div><span class="" style="white-space:pre">                  </span>                        <span class="" style="white-space:pre">          </span>Port = 5672,</div>
<div><span class="" style="white-space:pre">                    </span>                        <span class="" style="white-space:pre">          </span>UserName = "guest",</div><div><span class="" style="white-space:pre">                      </span>                        <span class="" style="white-space:pre">          </span>Password = "guest",</div>
<div><span class="" style="white-space:pre">                    </span>                        <span class="" style="white-space:pre">          </span>Protocol = Protocols.AMQP_0_9_1,</div><div><span class="" style="white-space:pre">                   </span>                        <span class="" style="white-space:pre">          </span>RequestedFrameMax = UInt32.MaxValue,</div>
<div><span class="" style="white-space:pre">                    </span>                        <span class="" style="white-space:pre">          </span>RequestedHeartbeat = UInt16.MaxValue</div><div><span class="" style="white-space:pre">                       </span>                        <span class="" style="white-space:pre">  </span>};</div>
<div><br></div><div><span class="" style="white-space:pre">                   </span>using (var connection = connectionFactory.CreateConnection())</div><div><span class="" style="white-space:pre">                      </span>using (var channel = connection.CreateModel())</div>
<div><span class="" style="white-space:pre">                    </span>{</div><div><span class="" style="white-space:pre">                          </span>// Create a new, durable exchange</div><div><span class="" style="white-space:pre">                          </span>channel.ExchangeDeclare("sample-ex", ExchangeType.Direct, true, false, null);</div>
<div><span class="" style="white-space:pre">                            </span>// Create a new, durable queue</div><div><span class="" style="white-space:pre">                             </span>channel.QueueDeclare("sample-queue", true, false, false, null);</div>
<div><span class="" style="white-space:pre">                            </span>// Bind the queue to the exchange</div><div><span class="" style="white-space:pre">                          </span>channel.QueueBind("sample-queue", "sample-ex", "optional-routing-key");</div>
<div><br></div><div><span class="" style="white-space:pre">                           </span>// Set up message properties</div><div><span class="" style="white-space:pre">                               </span>var properties = channel.CreateBasicProperties();</div><div><span class="" style="white-space:pre">                          </span>properties.DeliveryMode = 2; // Messages are persistent and will survive a server restart</div>
<div><br></div><div><span class="" style="white-space:pre">                           </span>// Ready to start publishing</div><div><span class="" style="white-space:pre">                               </span>// The message to publish can be anything that can be serialized to a byte array, </div>
<div><span class="" style="white-space:pre">                            </span>// such as a serializable object, an ID for an entity, or simply a string</div><div><span class="" style="white-space:pre">                          </span>var encoding = new UTF8Encoding();</div>
<div><span class="" style="white-space:pre">                            </span>for (var i = 0; i < 10; i++)</div><div><span class="" style="white-space:pre">                            </span>{</div><div><span class="" style="white-space:pre">                                  </span>var msg = string.Format("This is message derek #{0}?", i+1);</div>
<div><span class="" style="white-space:pre">                                    </span>var msgBytes = encoding.GetBytes(msg);</div><div><span class="" style="white-space:pre">                                     </span>channel.BasicPublish("sample-ex", "optional-routing-key", false, false, properties, msgBytes);</div>
<div><br></div><div><span class="" style="white-space:pre">                           </span>}</div><div><span class="" style="white-space:pre">                          </span>channel.Close();</div><div><span class="" style="white-space:pre">                   </span>}</div><div><span class="" style="white-space:pre">                  </span>Console.WriteLine("Messages published");</div>
<div><span class="" style="white-space:pre">            </span>    Console.ReadKey(true);</div><div><span class="" style="white-space:pre">         </span>}</div></div><div><br></div></div>