说明WCF消息队列具体问题

开发 后端
信息对于消息来说是非常重要的,在WCF分为四种主要的类型,下面我就对下WCF消息队列进行详细的介绍吧,希望对大家有所帮助。

希望我对WCF消息队列的一点经验能给大家带来帮助,导致WebDeployment出错的原因也许还有很多,不过在你遇到错误时,可以先检查一下你程序中的字符串,暂时把他们置为””,试试看。没准就是他引起的问题啊。

MessageQueue.Create参数是存放消息队列的位置.这个基本就完成了创建和发送消息的主程序.下面我们来建立一个客户端,来访问消息队列,获取消息,同样建立一个控制台应用程序,

WCF消息队列添加引用和代码:

  1. 1namespace MSMQClient     
  2. class Program     
  3. {     
  4. static void Main(string[] args)     
  5. {     
  6. //Get public queue message     
  7. if (MessageQueue.Exists(@".FrankMSMQ"))//判断是否存在消息队列     
  8. {     
  9.     
  10. using(MessageQueue mq = new MessageQueue(@".FrankMSMQ"))//创建消息队列对象     
  11. {     
  12. mq.Formatter = new XmlMessageFormatter(new string[] { "System.String" });//设置消息队列的格式化器     
  13. //mq.Send("Sample Message", ":Label");     
  14. Message msg = mq.Receive();//从队列接受消息     
  15. Console.WriteLine("Received MSMQ Message is :{0}", msg.Body);//输出消息     
  16. }     
  17. //Console.Read();     
  18. }     
  19. //Get private queue message     
  20. if (MessageQueue.Exists(@".Private$FrankMSMQ"))//判断私有消息是否存在     
  21. {     
  22. using (MessageQueue mq = new MessageQueue(@".Private$FrankMSMQ"))     
  23. {     
  24. mq.Formatter = new XmlMessageFormatter(new string[] { "System.String" });//设置消息队列格式化器     
  25. //mq.Send("Sample Message", ":Label");     
  26. Message msg = mq.Receive();//接收消息     
  27. Console.WriteLine("Received MSMQ Private Message is: {0}", msg.Body);//输出消息     
  28. }     
  29. }     
  30. Console.Read();     
  31. }     
  32. }     
  33. }  

消息接收同样需要实例化一个WCF消息队列对象, using(MessageQueue mq = new MessageQueue(@".FrankMSMQ"))负责创建WCF消息队列对象.其次这行代码负责设置消息队列的格式化器,因为消息的传递过程中存在格式化的问题.我们接收消息的时候必须指定消息队列的格式化属性Formatter, 队列才能接受消息。 #t#

XmlMessageFormatter的作用是进行消息的XML串行化.BinaryMessageFormatter则把消息格式化为二进制数据进行传输.ActiveXMessageFormatter把消息同样进行二进制格式化,区别是可以使用COM读取队列中的消息。当然消息队列还可以发送复杂的对象,前提是这个对象要可串行化,具体的格式取决与队列的格式化器设置.此外消息队列还支持事务队列来确保消息只发送一次和发送的顺序.最近在研究SOA,所以系统系统学习一下WCF消息队列及其相关的技术,以上就是这个消息队列的基本的概念和简单的编程实现.

责任编辑:chenqingxiang 来源: 51CTO.com
相关推荐

2010-02-22 17:29:47

WCF跨域

2009-12-07 09:23:05

2009-11-09 11:15:06

WCF消息队列

2009-12-08 10:21:17

WCF地址

2009-12-08 16:09:02

WCF消息

2009-12-07 16:57:39

WCF安全性

2010-04-01 15:58:55

CentOS操作系统

2010-03-09 17:32:45

Python数组

2009-10-16 13:04:18

网络综合布线

2009-11-09 11:31:47

WCF消息队列

2017-10-11 15:08:28

消息队列常见

2009-12-07 17:46:52

WCF框架

2010-02-22 10:01:11

WCF消息处理

2010-02-25 14:26:48

WCF特点

2009-12-08 10:35:29

WCF地址

2009-12-07 18:06:46

WCF框架

2009-12-08 09:00:14

WCF线程

2009-12-08 09:33:53

WCF服务

2009-12-07 10:46:08

WCF框架

2009-12-21 16:04:45

WCF Dispose
点赞
收藏

51CTO技术栈公众号