本文整理汇总了C#中MessageProperties.SetHeader方法的典型用法代码示例。如果您正苦于以下问题:C# MessageProperties.SetHeader方法的具体用法?C# MessageProperties.SetHeader怎么用?C# MessageProperties.SetHeader使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MessageProperties
的用法示例。
在下文中一共展示了MessageProperties.SetHeader方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestReplyToThreeDeep
public void TestReplyToThreeDeep()
{
var mockConnectionFactory = new Mock<ConnectionFactory>();
var mockConnection = new Mock<IConnection>();
var mockChannel = new Mock<IModel>();
mockConnectionFactory.Setup(m => m.CreateConnection()).Returns(mockConnection.Object);
mockConnection.Setup(m => m.IsOpen).Returns(true);
mockConnection.Setup(m => m.CreateModel()).Returns(mockChannel.Object);
mockChannel.Setup(m => m.CreateBasicProperties()).Returns(() => new BasicProperties());
var template = new RabbitTemplate(new SingleConnectionFactory(mockConnectionFactory.Object));
var replyQueue = new Queue("new.replyTo");
template.ReplyQueue = replyQueue;
var messageProperties = new MessageProperties();
messageProperties.ReplyTo = "replyTo2";
messageProperties.SetHeader(RabbitTemplate.STACKED_REPLY_TO_HEADER, "replyTo1");
messageProperties.SetHeader(RabbitTemplate.STACKED_CORRELATION_HEADER, "a");
var message = new Message(Encoding.UTF8.GetBytes("Hello, world!"), messageProperties);
var props = new List<IBasicProperties>();
var count = new AtomicInteger();
var nestedReplyTo = new List<string>();
var nestedReplyStack = new List<string>();
var nestedCorrelation = new List<string>();
mockChannel.Setup(m => m.BasicPublish(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>(), It.IsAny<bool>(), It.IsAny<IBasicProperties>(), It.IsAny<byte[]>())).Callback<string, string, bool, bool, IBasicProperties, byte[]>
(
(a1, a2, a3, a4, a5, a6) =>
{
var basicProps = a5;
props.Add(basicProps);
var springProps = new DefaultMessagePropertiesConverter().ToMessageProperties(basicProps, null, "UTF-8");
var replyMessage = new Message(Encoding.UTF8.GetBytes("!dlrow olleH"), springProps);
if (count.IncrementValueAndReturn() < 2)
{
var anotherMessage = new Message(Encoding.UTF8.GetBytes("Second"), springProps);
replyMessage = template.SendAndReceive(anotherMessage);
nestedReplyTo.Add(replyMessage.MessageProperties.ReplyTo);
nestedReplyStack.Add((string)replyMessage.MessageProperties.Headers[RabbitTemplate.STACKED_REPLY_TO_HEADER]);
nestedCorrelation.Add((string)replyMessage.MessageProperties.Headers[RabbitTemplate.STACKED_CORRELATION_HEADER]);
}
template.OnMessage(replyMessage);
});
var reply = template.SendAndReceive(message);
Assert.IsNotNull(reply);
Assert.AreEqual(2, props.Count);
var basicProperties = props[0];
Assert.AreEqual("new.replyTo", basicProperties.ReplyTo);
Assert.AreEqual("replyTo2:replyTo1", basicProperties.Headers[RabbitTemplate.STACKED_REPLY_TO_HEADER]);
Assert.IsTrue(((string)basicProperties.Headers[RabbitTemplate.STACKED_CORRELATION_HEADER]).EndsWith(":a"));
basicProperties = props[1];
Assert.AreEqual("new.replyTo", basicProperties.ReplyTo);
Assert.AreEqual("new.replyTo:replyTo2:replyTo1", basicProperties.Headers[RabbitTemplate.STACKED_REPLY_TO_HEADER]);
Assert.IsTrue(((string)basicProperties.Headers[RabbitTemplate.STACKED_CORRELATION_HEADER]).EndsWith(":a"));
Assert.AreEqual("replyTo1", reply.MessageProperties.Headers[RabbitTemplate.STACKED_REPLY_TO_HEADER]);
Assert.AreEqual("a", reply.MessageProperties.Headers[RabbitTemplate.STACKED_CORRELATION_HEADER]);
Assert.AreEqual(1, nestedReplyTo.Count);
Assert.AreEqual(1, nestedReplyStack.Count);
Assert.AreEqual(1, nestedCorrelation.Count);
Assert.AreEqual("replyTo2:replyTo1", nestedReplyStack[0]);
Assert.IsTrue(nestedCorrelation[0].EndsWith(":a"));
}