I am trying to setup a dead-letter queue, but when I reject a message it's not passed to the dead-letter queue. <div><br></div><div>Code I use:</div><div><br></div><div><pre style="font-family: Consolas; color: black; background-position: initial initial; background-repeat: initial initial;"><span style="color:blue;">static</span> <span style="color:blue;">void</span> Main(<span style="color:blue;">string</span>[] args)
{
<span style="color:blue;">const</span> <span style="color:blue;">string</span> deadLetterExchangeName = <span style="color:#a31515;">"DeadLetterExchange"</span>;
<span style="color:blue;">const</span> <span style="color:blue;">string</span> deadLetterQueueName = <span style="color:#a31515;">"DeadLetterQueue"</span>;
<span style="color:blue;">const</span> <span style="color:blue;">string</span> deadLetterRoutingKey = <span style="color:#a31515;">"MessageQueue"</span>;
<span style="color:blue;">const</span> <span style="color:blue;">string</span> defaultExchangeName = <span style="color:#a31515;">"MessagesExchange"</span>;
<span style="color:blue;">const</span> <span style="color:blue;">string</span> defaultQueueName = <span style="color:#a31515;">"MessageQueue"</span>;
<span style="color:#2b91af;">ConnectionFactory</span> connectionFactory = <span style="color:blue;">new</span> <span style="color:#2b91af;">ConnectionFactory</span>();
connectionFactory.HostName = <span style="color:#a31515;">"10.5.0.184"</span>;
<span style="color:#2b91af;">IConnection</span> connection = connectionFactory.CreateConnection();
<span style="color:#2b91af;">IModel</span> channel = connection.CreateModel();
<span style="color:green;">//Declare dead-letter exchange</span>
channel.ExchangeDeclare(deadLetterExchangeName, <span style="color:#2b91af;">ExchangeType</span>.Direct);
channel.QueueDeclare(deadLetterQueueName, <span style="color:blue;">true</span>, <span style="color:blue;">false</span>, <span style="color:blue;">false</span>, <span style="color:blue;">null</span>);
channel.QueueBind(deadLetterQueueName, deadLetterExchangeName, deadLetterRoutingKey, <span style="color:blue;">null</span>);
<span style="color:green;">//Declare default exchange</span>
channel.ExchangeDeclare(defaultExchangeName, <span style="color:#2b91af;">ExchangeType</span>.Direct);
channel.QueueDeclare(defaultQueueName, <span style="color:blue;">true</span>, <span style="color:blue;">false</span>, <span style="color:blue;">false</span>, <span style="color:blue;">null</span>);
<span style="color:#2b91af;">Hashtable</span> arguments = <span style="color:blue;">new</span> <span style="color:#2b91af;">Hashtable</span>()
{
{<span style="color:#a31515;">"x-dead-letter-exchange"</span>, <span style="color:#a31515;">"DeadLetterExchange"</span>},
{<span style="color:#a31515;">"x-dead-letter-routing-key"</span>, deadLetterRoutingKey}
};
channel.QueueBind(defaultQueueName, defaultExchangeName, defaultQueueName, arguments);
<span style="color:green;">//Queue message</span>
<span style="color:blue;">byte</span>[] messageBody;
<span style="color:#2b91af;">MyMessage</span> message = <span style="color:blue;">new</span> <span style="color:#2b91af;">MyMessage</span>(<span style="color:#a31515;">"ID-1"</span>);
<span style="color:#2b91af;">DataContractJsonSerializer</span> serializer = <span style="color:blue;">new</span> <span style="color:#2b91af;">DataContractJsonSerializer</span>(<span style="color:blue;">typeof</span>(<span style="color:#2b91af;">MyMessage</span>));
<span style="color:blue;">using</span> (<span style="color:#2b91af;">MemoryStream</span> memoryStream = <span style="color:blue;">new</span> <span style="color:#2b91af;">MemoryStream</span>())
{
serializer.WriteObject(memoryStream, message);
messageBody = memoryStream.ToArray();
}
<span style="color:#2b91af;">IBasicProperties</span> basicProperties = channel.CreateBasicProperties();
basicProperties.ContentType = <span style="color:#a31515;">"application/json"</span>;
basicProperties.DeliveryMode = 2;
channel.BasicPublish(defaultExchangeName, defaultQueueName, basicProperties, messageBody);
<span style="color:green;">//DeQueue</span>
<span style="color:#2b91af;">BasicGetResult</span> result = channel.BasicGet(defaultQueueName, <span style="color:blue;">false</span>);
<span style="color:green;">//Negative Acknowledge</span>
channel.BasicNack(result.DeliveryTag, <span style="color:blue;">false</span>, <span style="color:blue;">false</span>);
}
}
[<span style="color:#2b91af;">DataContract</span>]
<span style="color:blue;">public</span> <span style="color:blue;">class</span> <span style="color:#2b91af;">MyMessage</span>
{
<span style="color:blue;">public</span> MyMessage(<span style="color:blue;">string</span> messageId)
{
MessageId = messageId;
}
[<span style="color:#2b91af;">DataMember</span>]
<span style="color:blue;">public</span> <span style="color:blue;">string</span> MessageId { <span style="color:blue;">get</span>; <span style="color:blue;">set</span>; }
}</pre><pre style="font-family: Consolas; color: black; background-position: initial initial; background-repeat: initial initial;"><br></pre><pre style="font-family: Consolas; color: black; background-position: initial initial; background-repeat: initial initial;"><br></pre><pre><font color="#000000" face="Consolas">Any ideas?<br></font></pre></div>