0

我把它弄湿了,只是我把它添加到这样的 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.

有人可以检查他们是否可以看到问题所在吗?

4

2 回答 2

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处理您的消息,以便将它们放回队列中。

于 2013-02-10T19:28:06.483 回答
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
    }
于 2013-02-11T09:02:42.460 回答