[rabbitmq-discuss] How to setup dead-letter queue in C# ?

Johan Klijn johanklijn1976 at gmail.com
Fri Dec 7 09:04:40 GMT 2012


I am trying to setup a dead-letter queue, but when I reject a message it's 
not passed to the dead-letter queue. 

Code I use:

static void Main(string[] args)
{
    const string deadLetterExchangeName = "DeadLetterExchange";
    const string deadLetterQueueName = "DeadLetterQueue";
    const string deadLetterRoutingKey = "MessageQueue";
    const string defaultExchangeName = "MessagesExchange";
    const string defaultQueueName = "MessageQueue";
 
    ConnectionFactory connectionFactory = new ConnectionFactory();
    connectionFactory.HostName = "10.5.0.184";
 
    IConnection connection = connectionFactory.CreateConnection();
    IModel channel = connection.CreateModel();
 
    //Declare dead-letter exchange
    channel.ExchangeDeclare(deadLetterExchangeName, ExchangeType.Direct);
    channel.QueueDeclare(deadLetterQueueName, true, false, false, null);
    channel.QueueBind(deadLetterQueueName, deadLetterExchangeName, deadLetterRoutingKey, null);
 
    //Declare default exchange
    channel.ExchangeDeclare(defaultExchangeName, ExchangeType.Direct);
    channel.QueueDeclare(defaultQueueName, true, false, false, null);
    Hashtable arguments = new Hashtable()
                                {
                                    {"x-dead-letter-exchange", "DeadLetterExchange"},
                                    {"x-dead-letter-routing-key", deadLetterRoutingKey}
                                };
    channel.QueueBind(defaultQueueName, defaultExchangeName, defaultQueueName, arguments);
 
    //Queue message
    byte[] messageBody;
    MyMessage message = new MyMessage("ID-1");
    DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(MyMessage));
    using (MemoryStream memoryStream = new MemoryStream())
    {
        serializer.WriteObject(memoryStream, message);
        messageBody = memoryStream.ToArray();
    }
 
    IBasicProperties basicProperties = channel.CreateBasicProperties();
    basicProperties.ContentType = "application/json";
    basicProperties.DeliveryMode = 2;
 
    channel.BasicPublish(defaultExchangeName, defaultQueueName, basicProperties, messageBody);
 
    //DeQueue
    BasicGetResult result = channel.BasicGet(defaultQueueName, false);
 
    //Negative Acknowledge
    channel.BasicNack(result.DeliveryTag, false, false);
}
}
 
[DataContract]public class MyMessage
{public MyMessage(string messageId)
{
    MessageId = messageId;
}
 
[DataMember]public string MessageId { get; set; }        
}



Any ideas?

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rabbitmq.com/pipermail/rabbitmq-discuss/attachments/20121207/e1e1e80f/attachment.htm>


More information about the rabbitmq-discuss mailing list