我有一个添加msmq消息的wcf服务 我还有一个Windows侦听器,它从私有队列中读取消息并处理消息。
protected override void OnStart(string[] args)
{
// initialises the queue and sets QueueAccessMode.Receive
MessageQueue queue...;
queue.ReceiveCompleted += ProcessEvent;
queue.BeginReceive();
}
public void ProcessEvent(Object source, ReceiveCompletedEventArgs asyncResult)
{
MessageQueue queue = (MessageQueue)source;
try
{
Message msg = queue.EndReceive(asyncResult.AsyncResult);
msg.Formatter = new XmlMessageFormatter(new[] { typeof(MyEvent) });
try
{
MyEvent e = (MyEvent) msg.Body;
Process(e);
}catch(exception e)
{
HandleMessageFault(msg, queue);
// how can i put this message back in the queue or another queue at this point?
// will the msmq put it back in the queue automatically ?
}
}
catch()
{
// or how to handle here ?
}
}
我已经尝试将消息放回到同一个队列中,但它似乎不起作用但不会出现错误
private void HandleMessageFault(Message message, MessageQueue currentQueue)
{
message.AppSpecific++;
using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required))
{
currentQueue.Send(message);
scope.Complete();
}
}