這是我一起去的。我喜歡這種技術,因爲我沒有將東西添加到不屬於模型的JSON中。
IBasicProperties props = model.CreateBasicProperties();
props.Headers = new Dictionary<string, object>();
props.Headers.Add("RequestType", "CancelRunRequest");
然後,在接收端,我這樣做(我養的一個自定義EventArg OBJ事件):
// Raise message received event
var args = new MessageReceivedArgs();
args.CorrelationId = response.BasicProperties.CorrelationId;
args.Message = Encoding.UTF8.GetString(response.Body);
args.Exchange = response.Exchange;
args.RoutingKey = response.RoutingKey;
if (response.BasicProperties.Headers != null && response.BasicProperties.Headers.ContainsKey("RequestType"))
{
args.RequestType = Encoding.UTF8.GetString((byte[])response.BasicProperties.Headers["RequestType"]);
}
MessageReceived(this, args);
model.BasicAck(response.DeliveryTag, false);
在亞洲其他項目:
private void NewRunIdReceived(object p, MessageReceivedArgs e)
{
if(e.RequestType.ToUpper() == "CANCELRUNREQUEST")
{
// This is a cancellation request
CancelRun(e);
}
else
{
// Default to startrun request for backwards compatibility.
StartRun(e);
}
}
我不知道爲什麼沒有人標記這個正確的答案,它是最好的方式,保持消息體乾淨,並使用元數據。正確的答案對我來說。幹得好 – sacha
@sacha謝謝!如果沒有其他的輸入,我不想接受我自己的答案。我很欣賞反饋! –