//From Windows Service, use this code
MessageQueue messageQueue = null;
if (MessageQueue.Exists(@".Private$SomeTestName"))
{
messageQueue = new MessageQueue(@".Private$SomeTestName");
messageQueue.Label = "Testing Queue";
}
else
{
// Create the Queue
MessageQueue.Create(@".Private$SomeTestName");
messageQueue = new MessageQueue(@".Private$SomeTestName");
messageQueue.Label = "Newly Created Queue";
}
messageQueue.Send("First ever Message is sent to MSMQ", "Title");
//From Windows application
MessageQueue messageQueue = new MessageQueue(@".Private$SomeTestName");
System.Messaging.Message[] messages = messageQueue.GetAllMessages();
foreach (System.Messaging.Message message in messages)
{
//Do something with the message.
}
// after all processing, delete all the messages
messageQueue.Purge();
For more complex scenario, you could use Message objects to send the message, wrap your own class object inside it, and mark your class as serializable. Also be sure that MSMQ is installed on your system
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…