1.异步接收的例子2.格式化消息的例子3.使用各种路径名语法类型创建新的 MessageQueue 对象例子4.创建一个类,并发送和接收类中的值 例子 //异步接收 ReceiveAsynchronously#region ReceiveAsynchronously private void ReceiveAsynchronously() { //创建一个MessageQueue实例,并设为Path and Formatter MessageQueue mq = new MessageQueue(".\\MyQueue"); ((XmlMessageFormatter)mq.Formatter).TargetTypeNames = new String[] { "System.String" }; //设置事件句柄(利用委托机制) mq.ReceiveCompleted += new ReceiveCompletedEventHandler(OnReceiveCompleted); } public void OnReceiveCompleted(object sender, ReceiveCompletedEventArgs e) { MessageQueue mq = (MessageQueue)sender; Message message = mq.EndReceive(e.AsyncResult); Console.WriteLine("Message:" + (String)m.Body); //调用BeginReceive方法开始进行异步接收的操作 mq.BeginReceive(); } //以上BeginReceive只能接收一条消息,如果想要持续的接收消息的话,你必须做一个线程,然后进行间隔的调用(OnReceiveCompleted).:) #endregion //用于格式化消息 FormatterMessage#region FormatterMessage public static void FormatterMessage(MessageQueue mq, String Msg_Content, Msg_Type Msg_Type_Instance) { switch (Msg_Type_Instance) { case Msg_Type.XML: //XmlMessageFormatter 是 MessageQueue 的实例用来序列化写入队列的消息的默认格式化程序。 //当创建 MessageQueue 的实例时,会为您创建 XmlMessageFormatter 的实例,并将此实例与 MessageQueue 关联。 //通过在代码中创建格式化程序并将其分配给 MessageQueue 的 Formatter 属性,可以指定其他格式化程序。 mq.Formatter =new XmlMessageFormatter(); break; case Msg_Type.Binary: //BinaryMessageFormatter 是非常有效的并且可用于序列化大多数对象。 //结果非常紧凑并且可以进行快速分析,但不允许像 XmlMessageFormatter 所做的那样进行松耦合消息处理。 //松耦合意味着客户端和服务器可以独立控制发送和接收的类型的版本。 //当应用程序使用 MessageQueue 类的实例将消息发送到队列时,格式化程序将对象序列化到流中并且将其插入到消息体。 //在使用 MessageQueue 从队列中读取时,格式化程序将消息数据反序列化到 Message 的 Body 属性中。 //BinaryMessageFormatter 提供比 XmlMessageFormatter 更快的吞吐速度。 //当需要纯速度而非松耦合消息处理时使用 BinaryMessageFormatter。 mq.Formatter =new BinaryMessageFormatter(); break; case Msg_Type.ActiveX: //ActiveX 序列化非常紧凑,这使得使用 ActiveXMessageFormatter 和消息队列 COM 控件成为非常快的序列化方法。 mq.Formatter =new ActiveXMessageFormatter(); break; } mq.Send(Msg_Content); } public enum Msg_Type { XML, Binary, ActiveX } #endregion //使用各种路径名语法类型创建新的 MessageQueue 对象CommonReference#region CommonReference //引用公用队列 public void SendPublic() { MessageQueue myQueue = new MessageQueue(".\\myQueue"); myQueue.Send("Public queue by path name."); } //引用专用队列 public void SendPrivate() { MessageQueue myQueue = new MessageQueue(".\\Private$\\myQueue"); myQueue.Send("Private queue by path name."); } //引用label Queue public void SendByLabel() { MessageQueue myQueue = new MessageQueue("Label:TheLabel"); myQueue.Send("Queue by label."); } //References queues by format name. public void SendByFormatName() { MessageQueue myQueue = new MessageQueue("FormatName:Public=5A5F7535-AE9A-41d4-935C-845C2AFF7112"); myQueue.Send("Queue by format name."); } //References computer journal queues. public void MonitorComputerJournal() { MessageQueue computerJournal = new MessageQueue(".\\Journal$"); while(true) { Message journalMessage = computerJournal.Receive(); } } //引用日记队列 public void MonitorQueueJournal() { MessageQueue queueJournal = new MessageQueue(".\\myQueue\\Journal$"); while(true) { Message journalMessage = queueJournal.Receive(); } } //引用死信队列 public void MonitorDeadLetter() { MessageQueue deadLetter = new MessageQueue(".\\DeadLetter$"); while(true) { Message deadMessage = deadLetter.Receive(); } } // References transactional dead-letter queues. public void MonitorTransactionalDeadLetter() { MessageQueue TxDeadLetter = new MessageQueue(".\\XactDeadLetter$"); while(true) { Message txDeadLetter = TxDeadLetter.Receive(); } } #endregion //发送和接收[Order类]SendObjectAndReceiveObject#region SendObjectAndReceiveObject //创建一个类 public class Order { public int orderId; public DateTime orderTime; } /**//// <summary> /// 发送消息 /// </summary> private static void SendMessage() { Order sendOrder = new Order(); sendOrder.orderId = 3; sendOrder.orderTime = DateTime.Now; //手动建立公用队列 MessageQueue myQueue = new MessageQueue(".\\myQueue"); myQueue.Send(sendOrder); } /**//// <summary> /// 接收消息 /// </summary> private static void ReceiveMessage() { //手动建立公用队列 MessageQueue myQueue = new MessageQueue(".\\myQueue"); myQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(Order) }); try { System.Messaging.Message myMessage = myQueue.Receive(); Order myOrder = (Order)myMessage.Body; Console.WriteLine("Order id:" + myOrder.orderId.ToString()); Console.WriteLine("Sent:" + myOrder.orderTime.ToString()); } catch (MessageQueueException) { } catch (InvalidOperationException e) { Console.WriteLine(e.Message); } } #endregion