WCF 第四章 綁定 netMsmqBinding

MSMQ 為使用隊列創建分布式應用程序提供支持。WCF支持將MSMQ隊列作為netMsmqBinding綁定的底層傳輸協議的通信。 netMsmqBinding綁定允許客戶端直接把消息提交到一個隊列中同時服務端從隊列中讀取消息。客戶端和服務端之間沒有直接通信過程;因此,通信本 質是斷開的。也意外著所有的通信必須是單向的。因此,所有的操作必須要在操作契約上設置IsOneWay=true屬性。

提示 動態創建隊列

使用netMsmqBinding時動態創建MSMQ隊列是很普通的。當創建一個離線客戶端應用而且隊列在一個用戶的桌面時使用netMsmqBinding綁定更加平常。這可以通過創建System.MessageQueue類的靜態方法來實現。

下面的代碼顯示了netMsmqBinding綁定的地址格式:

??net.msmq:{hostname}/[private/|[public/]]{query name}

??MSMQ默認端口是1801而且沒有配置解決方案。注意地址格式中的public和private.你可以顯式的確定是否隊列名字指向一個私有的或者公有的隊列。默認情況下,隊列名字假設指向一個公共隊列。

表4.11 netMsmqBinding 綁定屬性

?

??我們在列表4.2到4.4使用的StockQuoteService樣例程序需要被修改以便于與netMsmqBinding綁定一起工作。 netMsmqBinding綁定僅支持單向操作(查看表4.2).我們之前的操作契約使用一個請求回復消息交換模式(查看列表4.4).我們將修改 StockQuoteService例子來顯示基于netMsmqBinding綁定的雙向通信而不是顯示一個不同的例子。

??我們需要使用兩個單向操作契約來維護服務端和客戶端的雙向通信。這意味著我們需要重新定義我們的契約以便于使用netMsmqBinding綁 定。列表4.24顯示了寫來與netMsmqBinding綁定一起使用的stock quote 契約。首先,注意我們把請求和回復契約轉換成兩個獨立的服務契約:IStockQuoteRequest和IStockQuoteResponse.每個 契約上的操作都是單向的。IStockQuoteRequest契約將被客戶端用來向服務端發送消息。IStockQuoteResponse契約將被服 務端用來發送一條消息給客戶端。這意味著客戶端和服務端都將寄宿服務來接收消息。

列表 4.24?IStockQuoteRequest,IStockQuoteResponse和StockQuoteRequestService

01using System;
02using System.Collections.Generic;
03using System.Linq;
04using System.Text;
05using System.ServiceModel;
06using System.Transactions;
07?
08namespace EssentialWCF
09{
10????[ServiceContract]
11????public interface IStockQuoteRequest
12????{
13????????[OperationContract(IsOneWay=true)]
14????????void SendQuoteRequest(string symbol);
15????}
16?
17????[ServiceContract]
18????public interface IStockQuoteResponse
19????{
20????????[OperationContract(IsOneWay = true)]
21????????void SendQuoteResponse(string symbol, double price);
22????}
23?
24????public class StockQuoteRequestService : IStockQuoteRequest
25????{
26????????public void SendQuoteRequest(string symbol)
27????????{
28????????????double value;
29????????????if (symbol == "MSFT")
30????????????????value = 31.15;
31????????????else if (symbol == "YHOO")
32????????????????value = 28.10;
33????????????else if (symbol == "GOOG")
34????????????????value = 450.75;
35????????????else
36????????????????value = double.NaN;
37?
38????????????//Send response back to client over separate queue
39????????????NetMsmqBinding msmqResponseBinding = new NetMsmqBinding();
40????????????using (ChannelFactory<IStockQuoteResponse> cf = new ChannelFactory<IStockQuoteResponse>("NetMsmqResponseClient"))
41????????????{
42????????????????IStockQuoteResponse client = cf.CreateChannel();
43????????????????using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required))
44????????????????{
45????????????????????client.SendQuoteResponse(symbol, value);
46????????????????????scope.Complete();
47????????????????}
48????????????????cf.Close();
49????????????}
50????????}
51????}
52}

?? netMsmqBinding下一個要考慮的就是使用ServiceHost類。先前的例子可以在不同的綁定上重用相同的ServiceHost代碼。這 因為服務契約可以保持一樣。而不是因為使用了netMsmqBinding。更新的用來寄宿StockServiceRequestService服務的 ServiceHost代碼在列表4.25中顯示。我們已經更新代碼來動態創建一個在基于配置文件中queueName的MSMQ隊列。這有助于通過簡單 配置允許程序部署而不需要額外的MSMQ配置。

列表 4.25 StockQuoteRequestService ServiceHost 服務

01using System;
02using System.Collections.Generic;
03using System.Linq;
04using System.Text;
05using System.ServiceModel;
06using System.Configuration;
07using System.Messaging;
08?
09namespace EssentialWCF
10{
11????class Program
12????{
13????????static void Main(string[] args)
14????????{
15????????????MyServiceHost.StartService();
16????????????Console.WriteLine("Service is Started, press Enter to terminate.");
17????????????Console.ReadLine();
18????????????MyServiceHost.StopService();
19????????}
20????}
21?
22????internal class MyServiceHost
23????{
24????????internal static string queryName = string.Empty;
25????????internal static ServiceHost myServiceHost = null;
26?
27????????internal static void StartService()
28????????{
29????????????queryName = ConfigurationManager.AppSettings["queueName"];
30????????????if (!MessageQueue.Exists(queryName))
31????????????????MessageQueue.Create(queryName, true);
32????????????myServiceHost = new ServiceHost(typeof(EssentialWCF.StockQuoteRequestService));
33????????????myServiceHost.Open();
34????????}
35????????internal static void StopService()
36????????{
37????????????if (myServiceHost.State != CommunicationState.Closed)
38????????????????myServiceHost.Close();
39????????}
40????}
41}

?? 列表4.26的配置信息使用netMsmqBinding綁定暴露StockQuoteRequestService服務。它也為IStockQuoteResponse契約配置一個客戶端終結點以便于回復可以發送給客戶端。

列表 4.26 netMsmqBinding 宿主 配置

01<?xml version="1.0" encoding="utf-8" ?>
02<configuration>
03????<system.serviceModel>
04????????<client>
05????????????<endpoint address="net.msmq://localhost/private/stockquoteresponse"
06????????????????binding="netMsmqBinding" bindingConfiguration="NoMsmqSecurity"
07????????????????contract="EssentialWCF.IStockQuoteResponse" name="NetMsmqResponseClient" />
08????????</client>
09????????<bindings>
10????????????<netMsmqBinding>
11????????????????<binding name="NoMsmqSecurity">
12????????????????????<security mode="None" />
13????????????????</binding>
14????????????</netMsmqBinding>
15????????</bindings>
16????????<services>
17????????????<service name="EssentialWCF.StockQuoteRequestService">
18????????????????<endpoint address="net.msmq://localhost/private/stockquoterequest"
19????????????????????binding="netMsmqBinding" bindingConfiguration="NoMsmqSecurity"
20????????????????????name="" contract="EssentialWCF.IStockQuoteRequest" />
21????????????</service>
22????????</services>
23????</system.serviceModel>
24??<appSettings>
25????<add key="queueName" value=".\private$\stockquoterequest"/>
26??</appSettings>
27</configuration>

??客戶端應用程序必須使用netMsmqBinding寄宿一個服務來接受回復且配置一個終結點來發送請求給服務端。列表4.27 顯示了客戶端用來寄宿一個實現了IStockQuoteResponse契約的ServiceHost類。我們添加代碼來動態創建一個客戶端監聽的隊列。 再次,這有助于通過簡單配置允許程序部署而不需要額外的MSMQ配置。

列表 4.27 StockQuoteResponseService ServiceHost 客戶端

01using System;
02using System.Collections.Generic;
03using System.Linq;
04using System.Text;
05using System.ServiceModel;
06using System.Configuration;
07using System.Messaging;
08?
09namespace EssentialWCF
10{
11????internal class MyServiceHost
12????{
13????????internal static ServiceHost myServiceHost = null;
14?
15????????internal static void StartService()
16????????{
17????????????string queryName = ConfigurationManager.AppSettings["queueName"];
18????????????if (!MessageQueue.Exists(queryName))
19????????????????MessageQueue.Create(queryName, true);
20????????????myServiceHost = new ServiceHost(typeof(EssentialWCF.Program));
21????????????myServiceHost.Open();
22????????}
23????????internal static void StopService()
24????????{
25????????????if (myServiceHost.State != CommunicationState.Closed)
26????????????????myServiceHost.Close();
27????????}
28????}
29}

?? 列表4.28 顯示了IStockQuoteResponse接口的客戶端實現。客戶端實現了接口,接下來被服務端當作發送回復的回調端。這不是使用了WCF中的雙向能力。相反的,回調使用一個單獨的單向綁定實現。

01using System;
02using System.Collections.Generic;
03using System.Linq;
04using System.Text;
05using System.Threading;
06using System.ServiceModel;
07using System.Transactions;
08?
09namespace EssentialWCF
10{
11????public class Program : IStockQuoteResponse
12????{
13????????private static AutoResetEvent waitForResponse;
14????????static void Main(string[] args)
15????????{
16????????????//Start response service host
17????????????MyServiceHost.StartService();
18????????????try
19????????????{
20????????????????waitForResponse = new AutoResetEvent(false);
21????????????????//Send request to the server
22????????????????using (ChannelFactory<IStockQuoteRequest> cf = new ChannelFactory<IStockQuoteRequest>("NetMsmqRequestClient"))
23????????????????{
24????????????????????IStockQuoteRequest client = cf.CreateChannel();
25????????????????????using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required))
26????????????????????{
27????????????????????????client.SendQuoteRequest("MSFT");
28????????????????????????scope.Complete();
29????????????????????}
30????????????????????cf.Close();
31????????????????}
32????????????????waitForResponse.WaitOne();
33????????????}
34????????????finally
35????????????{
36????????????????MyServiceHost.StopService();
37????????????}
38????????????Console.ReadLine();
39????????}
40?
41????????public void SendQuoteResponse(string symbol, double price)
42????????{
43????????????Console.WriteLine("{0}@${1}", symbol, price);
44????????????waitForResponse.Set();
45????????}
46????}
47}

?? 讓netMsmqBinding Stock Quote 樣例工作起來的最后一步是客戶端配置文件。列表4.29 顯示了客戶端配置,包含了寄宿IStockQuoteResponse服務實現的信息,調用IStockQuoteRequest服務的終結點配置。

列表 4.29 netMsmqBinding 客戶端配置

view sourceprint?
01<?xml version="1.0" encoding="utf-8" ?>
02<configuration>
03????<system.serviceModel>
04????????<bindings>
05????????????<netMsmqBinding>
06????????????????<binding name="NoMsmqSecurity">
07????????????????????<security mode="None" />
08????????????????</binding>
09????????????</netMsmqBinding>
10????????</bindings>
11????????<client>
12????????????<endpoint address="net.msmq://localhost/private/stockquoterequest"
13????????????????binding="netMsmqBinding" bindingConfiguration="NoMsmqSecurity"
14????????????????contract="EssentialWCF.IStockQuoteRequest" name="NetMsmqRequestClient" />
15????????</client>
16????????<services>
17????????????<service name="EssentialWCF.StockQuoteRequestService">
18????????????????<endpoint address="net.msmq://localhost/private/stockquoteresponse"
19????????????????????binding="netMsmqBinding" bindingConfiguration="NoMsmqSecurity"
20????????????????????contract="EssentialWCF.IStockQuoteResponse" />
21????????????</service>
22????????</services>
23????</system.serviceModel>
24??<appSettings>
25????<add key="queueName" value=".\private$\stockquoteresponse"/>
26??</appSettings>
27</configuration>


===========

轉載自

作者:DanielWise
出處:http://www.cnblogs.com/danielWise/
?

轉載于:https://www.cnblogs.com/llbofchina/archive/2011/06/29/2093025.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/274845.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/274845.shtml
英文地址,請注明出處:http://en.pswp.cn/news/274845.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

React 18 RC 版本發布啦,生產環境用起來!

大家好&#xff0c;我是若川。持續組織了6個月源碼共讀活動&#xff0c;感興趣的可以點此加我微信 ruochuan12 參與&#xff0c;每周大家一起學習200行左右的源碼&#xff0c;共同進步。同時極力推薦訂閱我寫的《學習源碼整體架構系列》 包含20余篇源碼文章。歷史面試系列今天給…

阿拉伯語排版設計_針對說阿拉伯語的用戶的測試和設計

阿拉伯語排版設計Let me start off with some data to put things into perspective “Why?”讓我從一些數據入手&#xff0c;以透視“為什么&#xff1f;”的觀點。 Arabic is the 5th most spoken language worldwide, with 420 million speakers, and is an official lang…

CMMI簡介

CMMI&#xff08;Capability Maturity Model Integration&#xff09;即能力成熟度模型集成 什么是CMMI CMMI是CMM模型的最新版本。早期的CMMI&#xff08;CMMI-SE/SW/IPPD&#xff09;1.02版本是應用于軟件業項目的管理方法&#xff0c;SEI在部分國家和地區開始推廣和試用。隨…

SVN:“SVN”不是內部命令,解決方法

1、安裝完TortoiseSVN-1.6.16.21511-x64-svn-1.6.17.msi 2、在運行窗口cmd---svn&#xff0c;提示&#xff1a; “SVN” 不是內部命令 郁悶&#xff0c;小有糾結 解決方法&#xff1a;安裝Slik-Subversion-1.6.17-x64.msi 命令行窗口關閉&#xff0c;再次打開命令行窗口&#x…

7個月,4000+人,500+源碼筆記,誠邀你參加源碼共讀~

大家好&#xff0c;我是若川。按照從易到難的順序&#xff0c;前面幾期&#xff08;比如&#xff1a;validate-npm-package-name、axios工具函數&#xff09;很多都只需要花2-3小時就能看完&#xff0c;并寫好筆記。但收獲確實很大。開闊視野、查漏補缺、升職加薪。已經有400筆…

火焰和煙霧的訓練圖像數據集_游戲開發者是煙霧和鏡子的大師

火焰和煙霧的訓練圖像數據集Video games are incredible. They transport us to new worlds, allow us to partake in otherwise impossible situations, and empower us in our every day lives. Games can make us feel like a part of something bigger than ourselves, per…

平衡樹SPLAY

一個比線段樹代碼還要又臭又長的數據結構&#xff0c;各式各樣的函數&#xff0c;咱也不知道別人怎么記住的&#xff0c;咱也不敢問 SPLAY的性質 1.某個節點的左子樹全部小于此節點&#xff0c;右子樹全部大于此節點 2.中序遍歷splay輸出的序列是按從小到大的順序 &#xff08;…

POJ 2696 計算表達式的值

時間限制: 1000ms內存限制:65536kB描述有些語言中表達式的運算符使用字符串表示&#xff0c;例如用mul代表*&#xff0c;用div代表/&#xff0c;用add代表&#xff0c;用sub代表-&#xff0c;用mod代表%。輸入第一行為表達式的個數n。其余n行每行一個表達式&#xff0c;表達式由…

為支持兩個語言版本,我基于谷歌翻譯API寫了一款自動翻譯的 webpack 插件

大家好&#xff0c;我是若川。持續組織了6個月源碼共讀活動&#xff0c;感興趣的可以點此加我微信 ruochuan12 參與&#xff0c;每周大家一起學習200行左右的源碼&#xff0c;共同進步。同時極力推薦訂閱我寫的《學習源碼整體架構系列》 包含20余篇源碼文章。歷史面試系列本文來…

全球 化 化_全球化設計

全球 化 化重點 (Top highlight)Designing for a global audience can feel daunting. Do you localize your product? Or, do you internationalize your product? And what does that even entail?為全球觀眾設計可能會令人生畏。 您是否將產品本地化&#xff1f; 還是您將…

springMVC_數據的處理過程

1、DispatcherServlet&#xff1a;作為前端控制器&#xff0c;負責分發客戶的請求到 Controller 其在web.xml中的配置如下&#xff1a; <servlet><servlet-name>dispatcherServlert</servlet-name><servlet-class>org.springframework.web.servlet.Dis…

面試體驗:Facebook 篇(轉)

http://www.cnblogs.com/cathsfz/archive/2012/11/05/facebook-interview-experience.html 2012-11-05 08:20 by Cat Chen, 23266閱讀, 121評論, 收藏, 編輯 Google、Microsoft 和 Yahoo 都是去年的事情了&#xff0c;接下來說說今年…

JavaScript 新增兩個原始數據類型

大家好&#xff0c;我是若川。持續組織了6個月源碼共讀活動&#xff0c;感興趣的可以點此加我微信 ruochuan12 參與&#xff0c;每周大家一起學習200行左右的源碼&#xff0c;共同進步。同時極力推薦訂閱我寫的《學習源碼整體架構系列》 包含20余篇源碼文章。歷史面試系列JavaS…

axure低保真原型_如何在Google表格中創建低保真原型

axure低保真原型Google Sheets is a spreadsheet, just like Microsoft Excel.Google表格是一個電子表格&#xff0c;就像Microsoft Excel一樣。 Most people associate it with calculating numbers. But Google Sheets is actually great for organizing your ideas, making…

Weblogic EJB 學習筆記(3)精

編輯實體bean的高級課程 1. 怎樣開發主健類 ejb的主健類主要用做持久存儲和ejb容器中的唯一標識符. 通常主健類的字段直接映射到數據庫中的主健字段. 如果主健只是由單個實體bean字段組成.且其數據類型是基本的java類.如string,則bean作者不必開發自定義的主健類. 只需要在配置…

Lerna 運行流程剖析

大家好&#xff0c;我是若川。持續組織了6個月源碼共讀活動&#xff0c;感興趣的可以點此加我微信 ruochuan12 參與&#xff0c;每周大家一起學習200行左右的源碼&#xff0c;共同進步。同時極力推薦訂閱我寫的《學習源碼整體架構系列》 包含20余篇源碼文章。歷史面試系列Lerna…

手動創建線程池 效果會更好_創建更好的,可訪問的焦點效果

手動創建線程池 效果會更好Most browsers has their own default, outline style for the :focus psuedo-class.大多數瀏覽器對于&#xff1a;focus psuedo-class具有其默認的輪廓樣式。 Chrome’s default outline styleChrome瀏覽器的默認輪廓樣式 This outline style is cr…

C++builder enum類型

C/C code #pragmaoption push -b-enumTThreadPriority { tpIdle, tpLowest, tpLower, tpNormal, tpHigher, tpHighest, tpTimeCritical }; //這是字節型的.理論上說這是可能的最小整形.可以是1Byte, 2Bytes, 4Bytes...#pragmaoption pop#pragmaoption push -benumTThreadPriori…

chrome瀏覽器世界之窗瀏覽器的收藏夾在哪?

今天心血來潮&#xff0c;用一個查重軟件刪除重復文件&#xff0c;結果把chrome瀏覽器和世界之窗瀏覽器的收藏夾給刪除了&#xff0c;導致我保存的好多網頁都沒有了&#xff0c;在瀏覽器本身和網上都沒有找到這兩個瀏覽器默認的收藏夾在哪個位置&#xff0c;只好用DiskGenius 把…

Vue3究竟好在哪里 等推薦

話不多說&#xff0c;這一次花了幾小時精心為大家挑選了30余篇好文&#xff0c;供大家閱讀學習&#xff0c;提升自己的技術視野以及擴展自己的知識儲備。本文閱讀技巧&#xff0c;先粗看標題&#xff0c;感興趣可以都關注一波&#xff0c;一起共同進步。前端從進階到入院框架原…