說明WCF消息隊列具體問題
希望我對WCF消息隊列的一點經(jīng)驗?zāi)芙o大家?guī)韼椭瑢е耊ebDeployment出錯的原因也許還有很多,不過在你遇到錯誤時,可以先檢查一下你程序中的字符串,暫時把他們置為””,試試看。沒準就是他引起的問題啊。
MessageQueue.Create參數(shù)是存放消息隊列的位置.這個基本就完成了創(chuàng)建和發(fā)送消息的主程序.下面我們來建立一個客戶端,來訪問消息隊列,獲取消息,同樣建立一個控制臺應(yīng)用程序,
WCF消息隊列添加引用和代碼:
- 1namespace MSMQClient
- class Program
- {
- static void Main(string[] args)
- {
- //Get public queue message
- if (MessageQueue.Exists(@".FrankMSMQ"))//判斷是否存在消息隊列
- {
- using(MessageQueue mq = new MessageQueue(@".FrankMSMQ"))//創(chuàng)建消息隊列對象
- {
- mq.Formatter = new XmlMessageFormatter(new string[] { "System.String" });//設(shè)置消息隊列的格式化器
- //mq.Send("Sample Message", ":Label");
- Message msg = mq.Receive();//從隊列接受消息
- Console.WriteLine("Received MSMQ Message is :{0}", msg.Body);//輸出消息
- }
- //Console.Read();
- }
- //Get private queue message
- if (MessageQueue.Exists(@".Private$FrankMSMQ"))//判斷私有消息是否存在
- {
- using (MessageQueue mq = new MessageQueue(@".Private$FrankMSMQ"))
- {
- mq.Formatter = new XmlMessageFormatter(new string[] { "System.String" });//設(shè)置消息隊列格式化器
- //mq.Send("Sample Message", ":Label");
- Message msg = mq.Receive();//接收消息
- Console.WriteLine("Received MSMQ Private Message is: {0}", msg.Body);//輸出消息
- }
- }
- Console.Read();
- }
- }
- }
消息接收同樣需要實例化一個WCF消息隊列對象, using(MessageQueue mq = new MessageQueue(@".FrankMSMQ"))負責創(chuàng)建WCF消息隊列對象.其次這行代碼負責設(shè)置消息隊列的格式化器,因為消息的傳遞過程中存在格式化的問題.我們接收消息的時候必須指定消息隊列的格式化屬性Formatter, 隊列才能接受消息。 #t#
XmlMessageFormatter的作用是進行消息的XML串行化.BinaryMessageFormatter則把消息格式化為二進制數(shù)據(jù)進行傳輸.ActiveXMessageFormatter把消息同樣進行二進制格式化,區(qū)別是可以使用COM讀取隊列中的消息。當然消息隊列還可以發(fā)送復雜的對象,前提是這個對象要可串行化,具體的格式取決與隊列的格式化器設(shè)置.此外消息隊列還支持事務(wù)隊列來確保消息只發(fā)送一次和發(fā)送的順序.最近在研究SOA,所以系統(tǒng)系統(tǒng)學習一下WCF消息隊列及其相關(guān)的技術(shù),以上就是這個消息隊列的基本的概念和簡單的編程實現(xiàn).