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
02 | using System.Collections.Generic; |
05 | using System.ServiceModel; |
06 | using System.Transactions; |
11 | public interface IStockQuoteRequest |
13 | [OperationContract(IsOneWay= true )] |
14 | void SendQuoteRequest( string symbol); |
18 | public interface IStockQuoteResponse |
20 | [OperationContract(IsOneWay = true )] |
21 | void SendQuoteResponse( string symbol, double price); |
24 | public class StockQuoteRequestService : IStockQuoteRequest |
26 | public void SendQuoteRequest( string symbol) |
31 | else if (symbol == "YHOO" ) |
33 | else if (symbol == "GOOG" ) |
38 | //Send response back to client over separate queue |
39 | NetMsmqBinding msmqResponseBinding = new NetMsmqBinding(); |
40 | using (ChannelFactory<IStockQuoteResponse> cf = new ChannelFactory<IStockQuoteResponse>( "NetMsmqResponseClient" )) |
42 | IStockQuoteResponse client = cf.CreateChannel(); |
43 | using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required)) |
45 | client.SendQuoteResponse(symbol, value); |
netMsmqBinding下一个要考虑的就是使用ServiceHost类。先前的例子可以在不同的绑定上重用相同的ServiceHost代码。这 因为服务契约可以保持一样。而不是因为使用了netMsmqBinding。更新的用来寄宿StockServiceRequestService服务的 ServiceHost代码在列表4.25中显示。我们已经更新代码来动态创建一个在基于配置文件中queueName的MSMQ队列。这有助于通过简单 配置允许程序部署而不需要额外的MSMQ配置。
列表 4.25 StockQuoteRequestService ServiceHost 服务
02 | using System.Collections.Generic; |
05 | using System.ServiceModel; |
06 | using System.Configuration; |
07 | using System.Messaging; |
13 | static void Main( string [] args) |
15 | MyServiceHost.StartService(); |
16 | Console.WriteLine( "Service is Started, press Enter to terminate." ); |
18 | MyServiceHost.StopService(); |
22 | internal class MyServiceHost |
24 | internal static string queryName = string .Empty; |
25 | internal static ServiceHost myServiceHost = null ; |
27 | internal static void StartService() |
29 | queryName = ConfigurationManager.AppSettings[ "queueName" ]; |
30 | if (!MessageQueue.Exists(queryName)) |
31 | MessageQueue.Create(queryName, true ); |
32 | myServiceHost = new ServiceHost( typeof (EssentialWCF.StockQuoteRequestService)); |
35 | internal static void StopService() |
37 | if (myServiceHost.State != CommunicationState.Closed) |
38 | myServiceHost.Close(); |
列表4.26的配置信息使用netMsmqBinding绑定暴露StockQuoteRequestService服务。它也为IStockQuoteResponse契约配置一个客户端终结点以便于回复可以发送给客户端。
列表 4.26 netMsmqBinding 宿主 配置
01 | <? xml version = "1.0" encoding = "utf-8" ?> |
05 | < endpoint address = "net.msmq://localhost/private/stockquoteresponse" |
06 | binding = "netMsmqBinding" bindingConfiguration = "NoMsmqSecurity" |
07 | contract = "EssentialWCF.IStockQuoteResponse" name = "NetMsmqResponseClient" /> |
11 | < binding name = "NoMsmqSecurity" > |
12 | < security mode = "None" /> |
17 | < service name = "EssentialWCF.StockQuoteRequestService" > |
18 | < endpoint address = "net.msmq://localhost/private/stockquoterequest" |
19 | binding = "netMsmqBinding" bindingConfiguration = "NoMsmqSecurity" |
20 | name = "" contract = "EssentialWCF.IStockQuoteRequest" /> |
23 | </ system.serviceModel > |
25 | < add key = "queueName" value = ".\private$\stockquoterequest" /> |
客户端应用程序必须使用netMsmqBinding寄宿一个服务来接受回复且配置一个终结点来发送请求给服务端。列表4.27 显示了客户端用来寄宿一个实现了IStockQuoteResponse契约的ServiceHost类。我们添加代码来动态创建一个客户端监听的队列。 再次,这有助于通过简单配置允许程序部署而不需要额外的MSMQ配置。
列表 4.27 StockQuoteResponseService ServiceHost 客户端
02 | using System.Collections.Generic; |
05 | using System.ServiceModel; |
06 | using System.Configuration; |
07 | using System.Messaging; |
11 | internal class MyServiceHost |
13 | internal static ServiceHost myServiceHost = null ; |
15 | internal static void StartService() |
17 | string queryName = ConfigurationManager.AppSettings[ "queueName" ]; |
18 | if (!MessageQueue.Exists(queryName)) |
19 | MessageQueue.Create(queryName, true ); |
20 | myServiceHost = new ServiceHost( typeof (EssentialWCF.Program)); |
23 | internal static void StopService() |
25 | if (myServiceHost.State != CommunicationState.Closed) |
26 | myServiceHost.Close(); |
列表4.28 显示了IStockQuoteResponse接口的客户端实现。客户端实现了接口,接下来被服务端当作发送回复的回调端。这不是使用了WCF中的双向能力。相反的,回调使用一个单独的单向绑定实现。
02 | using System.Collections.Generic; |
05 | using System.Threading; |
06 | using System.ServiceModel; |
07 | using System.Transactions; |
11 | public class Program : IStockQuoteResponse |
13 | private static AutoResetEvent waitForResponse; |
14 | static void Main( string [] args) |
16 | //Start response service host |
17 | MyServiceHost.StartService(); |
20 | waitForResponse = new AutoResetEvent( false ); |
21 | //Send request to the server |
22 | using (ChannelFactory<IStockQuoteRequest> cf = new ChannelFactory<IStockQuoteRequest>( "NetMsmqRequestClient" )) |
24 | IStockQuoteRequest client = cf.CreateChannel(); |
25 | using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required)) |
27 | client.SendQuoteRequest( "MSFT" ); |
32 | waitForResponse.WaitOne(); |
36 | MyServiceHost.StopService(); |
41 | public void SendQuoteResponse( string symbol, double price) |
43 | Console.WriteLine( "{0}@${1}" , symbol, price); |
44 | waitForResponse.Set(); |
让netMsmqBinding Stock Quote 样例工作起来的最后一步是客户端配置文件。列表4.29 显示了客户端配置,包含了寄宿IStockQuoteResponse服务实现的信息,调用IStockQuoteRequest服务的终结点配置。
列表 4.29 netMsmqBinding 客户端配置
01 | <? xml version = "1.0" encoding = "utf-8" ?> |
06 | < binding name = "NoMsmqSecurity" > |
07 | < security mode = "None" /> |
12 | < endpoint address = "net.msmq://localhost/private/stockquoterequest" |
13 | binding = "netMsmqBinding" bindingConfiguration = "NoMsmqSecurity" |
14 | contract = "EssentialWCF.IStockQuoteRequest" name = "NetMsmqRequestClient" /> |
17 | < service name = "EssentialWCF.StockQuoteRequestService" > |
18 | < endpoint address = "net.msmq://localhost/private/stockquoteresponse" |
19 | binding = "netMsmqBinding" bindingConfiguration = "NoMsmqSecurity" |
20 | contract = "EssentialWCF.IStockQuoteResponse" /> |
23 | </ system.serviceModel > |
25 | < add key = "queueName" value = ".\private$\stockquoteresponse" /> |
===========
转载自
作者:DanielWise
出处:http://www.cnblogs.com/danielWise/