如何在发送前更新消息

How to update message before it's delivered

我正在排队一堆消息,队列最终将消息作为记录传送到 sql server 数据库中。我需要更新消息以显示将发送消息的 datetime。考虑到这一点,我的要求听起来不合逻辑;但是,有没有办法或有我可以订阅的事件,以便在消息传递之前立即获得 datetime

var msgQ = new MessageQueue(_myque);
msgTx.Begin();
msgQ.Send(message, msgTx);
msgTx.Commit();

请注意,我无法对数据库进行任何更改,需要通过 .NET 代码解决此问题。

下面的示例会将消息推送到消息队列,然后在稍有延迟后接收它:

首先要发送一条消息:

public class Order
{
    public int orderId;
    public DateTime orderDate;
    public DateTime receivedDate;
};

发送新的Order,稍等片刻,然后从队列中接收:

// a private queue - set to applicable queue path.
var myQueue = new MessageQueue(".\private$\myQueue");

myQueue.Send(new Order { orderId = 1, orderDate = DateTime.Now});

// a 10 second delay to demonstrate an infrastructure 
// delay between the send and receive   
Thread.Sleep(new TimeSpan(0, 0, 10));

var message = myQueue.Receive();

var receivedOrder = (Order)message.Body;
receivedOrder.receivedDate = DateTime.Now;  

Console.WriteLine(string.Format("Order {0} sent at {1}, received at {2}", receivedOrder.orderId, receivedOrder.orderDate, receivedOrder.receivedDate));