Message Queue Windows Service

对着背影说爱祢 提交于 2019-12-03 21:45:17

There are a few different ways you can do the above. I would recommend setting up an event on the message queue so that you are notified when a message is available rather than polling for it.

Simple example of using message queues is http://www.codeproject.com/KB/cs/mgpmyqueue.aspx and the MSDN documentation for attaching events etc can be found at http://msdn.microsoft.com/en-us/library/system.messaging.messagequeue_events.aspx

Microsoft example from here:

           ....
           // Create an instance of MessageQueue. Set its formatter.
           MessageQueue myQueue = new MessageQueue(".\\myQueue");
            myQueue.Formatter = new XmlMessageFormatter(new Type[]
                {typeof(String)});

            // Add an event handler for the ReceiveCompleted event.
            myQueue.ReceiveCompleted += new 
                ReceiveCompletedEventHandler(MyReceiveCompleted);

            // Begin the asynchronous receive operation.
            myQueue.BeginReceive();
            ....


        private static void MyReceiveCompleted(Object source, 
            ReceiveCompletedEventArgs asyncResult)
        {
            // Connect to the queue.
            MessageQueue mq = (MessageQueue)source;

            // End the asynchronous Receive operation.
            Message m = mq.EndReceive(asyncResult.AsyncResult);

            // Display message information on the screen.
            Console.WriteLine("Message: " + (string)m.Body);

            // Restart the asynchronous Receive operation.
            mq.BeginReceive();

            return; 
        }

Check out the WCF examples at http://msdn.microsoft.com/en-us/library/ms751514.aspx.

EDIT: Note that my answer was given before the edit to specify using .Net 2.0. I still think WCF is the way to go, but it would require .NET 3.0, at least.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!