如何找到我在msmq中添加的消息

时间:2013-02-10 18:55:56

标签: c# msmq

我浏览了this,除了我将它添加到像这样的

这样的Windows服务中
public partial class TriggerHostProcesses : ServiceBase
{
    private const string MESSAGE_QUEUE = @".\Private$\Sample Queue";
    private MessageQueue _queue;
    public TriggerHostProcesses()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
        SendMessage("Hope This Works");
    }

    protected override void OnStop()
    {
    }

    internal void start()
    {
        OnStart(null);
    }


    private void SendMessage(string message)
    {
        _queue = new MessageQueue(MESSAGE_QUEUE);
        Message msg = new Message();
        msg.Body = message;
        msg.Label = "Testing " + DateTime.Now.ToString();
        _queue.Send(msg,new MessageQueueTransaction());
    }
}

并获取消息

 partial class HostListener : ServiceBase
{
      private const string MESSAGE_QUEUE = @".\Private$\Sample Queue";
    private MessageQueue _queue;

    public HostListener()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
        try
        {
            var myTransaction = new MessageQueueTransaction();

            var queue = new MessageQueue(MESSAGE_QUEUE);
            var message = queue.Receive(new TimeSpan(0, 0, 20),myTransaction);
            message.Formatter = new XmlMessageFormatter(
                                new String[] { "System.String,mscorlib" });
            Console.WriteLine(message.Body.ToString());
        }
        catch(Exception ex)
        {
            Console.WriteLine("No Message");
        }
    }

    protected override void OnStop()
    {
        // TODO: Add code here to perform any tear-down necessary to stop your service.
    }

    internal void start()
    {
        OnStart(null);
    }
}

在我的主要内容中我添加了这个

  var ServiceToRun1 = new TriggerHostProcesses();
        var ServiceToRun2 = new HostListener();

        if (Environment.UserInteractive)
        {
            // This used to run the service as a console (development phase only)

            ServiceToRun1.start();
            ServiceToRun2.start();

            Console.WriteLine("Press Enter to terminate ...");
            Console.ReadLine();

            ServiceToRun1.Stop();
            ServiceToRun2.Stop();
        }
        else
        {
            ServiceBase.Run(ServiceToRun1);
        }

我收到例外Timeout for the requested operation has expired.

有人可以检查一下他们是否能看出问题所在吗?

2 个答案:

答案 0 :(得分:2)

我不相信你正确使用交易。例如,在发送消息时使用:

_queue.Send(msg,new MessageQueueTransaction());

但是,这不会开始或提交事务。查看MSDN示例使用以下代码(由我编辑):

var myTransaction = new MessageQueueTransaction();
myTransaction.Begin();
myQueue.Send("hello world", myTransaction);
myTransaction.Commit();

我不相信您的邮件已发送,因此Receive超时。

同样,您的接收逻辑似乎没有正确使用事务:

myTransaction.Begin();
var myMessage = myQueue.Receive(myTransaction); 
var body myOrder = (string)myMessage.Body;
myTransaction.Commit();

如果发生异常处理您的邮件,您应该Rollback,以便将它们放回队列。

答案 1 :(得分:0)

这是我的最终产品。我在Windows服务中使用它。每次20秒,看我是否有消息然后做我的过程。

public class MSMQueue:IQueue
    {

        public MSMQueue(string queueName)
        {
            Message_Queue = queueName;
        }

        public string Message_Queue { get; private set; }

        public string Pop()
        {
            MessageQueue queue = new MessageQueue(Message_Queue);

            if (queue.Transactional)
                return popTransactionalQueue(queue, new TimeSpan(0, 0, 1));
            else
                return popNormalQueue(queue, new TimeSpan(0, 0, 1));
        }

        public string Pop(TimeSpan timeSpan)
        {
            MessageQueue myQueue = new MessageQueue(Message_Queue);

            if (myQueue.Transactional)
                return popTransactionalQueue(myQueue, timeSpan);
            else
                return popNormalQueue(myQueue, timeSpan);
        }

        public void Add(string message)
        {
            // Connect to a queue on the local computer.
            MessageQueue myQueue = new MessageQueue(Message_Queue);

            // Send a message to the queue.
            if (myQueue.Transactional)
            {
                var myTransaction = new MessageQueueTransaction();
                myTransaction.Begin();
                myQueue.Send(message, myTransaction);
                myTransaction.Commit();
            }
            else
                myQueue.Send(message);
        }

        #region private methods

        private string popNormalQueue(MessageQueue queue, TimeSpan timeOut)
        {
            var message = queue.Receive(timeOut);
            message.Formatter = new XmlMessageFormatter(
                                new String[] { "System.String,mscorlib" });
            return message.Body.ToString();
        }

        private string popTransactionalQueue(MessageQueue queue, TimeSpan timeOut)
        {

            // Set the formatter.
            queue.Formatter = new XmlMessageFormatter(new Type[]
                {typeof(String)});

            // Create a transaction.
            MessageQueueTransaction myTransaction = new 
                MessageQueueTransaction();

            String message=string.Empty;

            try
            {
                myTransaction.Begin();

                Message myMessage = queue.Receive(timeOut, myTransaction); 
                message = (String)myMessage.Body;

                myTransaction.Commit();

            }

            catch (MessageQueueException e)
            {
                myTransaction.Abort();
                throw e;
            }

            return message;
        }

        #endregion
    }