wsDualHttpBinding绑定类似于wsHttpBinding绑定,它额外支持双向通信但不支持传输级别的安全。双向通信通过两个形状改变绑定元素完成: OneWayBindingElement和CompositeDuplexBindingElement绑定元素。CompositeDuplexBindingElement绑定元素在两个单向信道上加了一个双向通信信道。wsDualHttpBinding绑定使用HttpTransportBindingElement绑定元素。这是传输仅支持请求-回复消息交换模式。OneWayBindingElement 绑定元素允许HttpTransportBindingElement绑定元素与CompositeDuplexBindingElement绑定元素一起使用。
wsDualHttpBinding绑定不支持传输层次的安全。这意味着在使用wsDualHttpBinding绑定时不可以使用SSL/TLS加密。
wsDualHttpBinding绑定类似于wsHttpBinding绑 定,它额外支持双向通信但不支持传输级别的安全。双向通信通过两个形状改变绑定元素完成: OneWayBindingElement和CompositeDuplexBindingElement绑定元素。 CompositeDuplexBindingElement绑定元素在两个单向信道上加了一个双向通信信道。wsDualHttpBinding绑定使 用HttpTransportBindingElement绑定元素。这是传输仅支持请求-回复消息交换模式。OneWayBindingElement 绑定元素允许HttpTransportBindingElement绑定元素与CompositeDuplexBindingElement绑定元素一 起使用。
wsDualHttpBinding绑定不支持传输层次的安全。这意味着在使用wsDualHttpBinding绑定时不可以使用SSL/TLS加密。
下面的代码显示了wsDualHttpBinding绑定的地址格式
http://{hostname}:{port}/{service location}
http的默认端口是80。这是任何基于HttpTransportBindingElement绑定元素的绑定的情形,包括wsDualHttpBinding绑定。
表4.10 列出了wsDualHttpBinding绑定可以设定的绑定属性。
表4.10 wsDualHttpBinding绑定属性
我们已经为wsDualHttpBinding绑定修改了 StockQuoteService应用来支持双向通信。列表4.17显示了StockQuoteDualService实现。服务支持使用由 IStockQuoteDuplexService契约确定的IStockQuoteCallback契约来进行双向消息交换模式。
列表4.17 IStockQuoteDuplexService, IStockQuoteCallback,and StockQuoteDuplexService
03 | public interface IStockQuoteCallback |
05 | [OperationContract(IsOneWay = true)] |
06 | void SendQuoteResponse(string symbol, double price); |
08 | [ServiceContract(CallbackContract=typeof(IStockQuoteCallback), SessionMode= SessionMode.Required)] |
09 | public interface IStockQuoteDuplexService |
11 | [OperationContract(IsOneWay = true)] |
12 | void SendQuoteRequest(string symbol); |
15 | [ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession)] |
16 | public class StockQuoteDuplexService : IStockQuoteDuplexService |
19 | #region IStockQuoteDuplexService Members |
21 | public void SendQuoteRequest(string symbol) |
29 | else if (symbol == "YHOO") |
33 | else if (symbol == "GOOG") |
40 | OperationContext ctx = OperationContext.Current; |
41 | IStockQuoteCallback callback = ctx.GetCallbackChannel<IStockQuoteCallback>(); |
42 | callback.SendQuoteResponse(symbol, value); |
我们必须为我们的例子改变自我寄宿代码因为我们改变了我们用来支持双向消息的实现。列表4.18 显示了为StockQuoteDuplexService服务的寄宿代码。
列表4.18 StockQuoteDuplexService ServiceHost 服务
01 | internal class MyServiceHost |
03 | internal static ServiceHost myServiceHost = null; |
04 | internal static void StartService() |
06 | myServiceHost = new ServiceHost(typeof(EssentialWCF.StockQuoteDuplexService)); |
09 | internal static void StopService() |
11 | if (myServiceHost.State != CommunicationState.Closed) |
13 | myServiceHost.Close(); |
列表4.19的配置信息使用wsDualHttpBinding绑定暴露StockQuoteDuplexService服务。
列表4.19 wsDualHttpBinding 宿主配置
01 | <?xml version="1.0" encoding="utf-8" ?> |
06 | <behavior name="NewBehavior"> |
07 | <serviceMetadata httpGetEnabled="true" /> |
12 | <service behaviorConfiguration="NewBehavior" name="EssentialWCF.StockQuoteDuplexService"> |
13 | <endpoint address="" binding="wsDualHttpBinding" bindingConfiguration="" |
14 | contract="EssentialWCF.IStockQuoteDuplexService" /> |
22 | </system.serviceModel> |
列表4.20显示了配置信息是客户端用来调用使用wsDualHttpBinding绑定并实现IStockQuoteDuplexService契约的服务的。clientBaseAddress确定了客户端要监听回调消息的终结点。
列表4.20 wsDualHttpBinding客户端配置
01 | <?xml version="1.0" encoding="utf-8" ?> |
07 | <binding name="WSDualHttpBinding_IStockQuoteDuplexService" closeTimeout="00:01:00" |
08 | openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" |
09 | bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard" |
10 | maxBufferPoolSize="524288" maxReceivedMessageSize="65536" |
11 | messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"> |
12 | <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" |
13 | maxBytesPerRead="4096" maxNameTableCharCount="16384" /> |
14 | <reliableSession ordered="true" inactivityTimeout="00:10:00" /> |
15 | <security mode="Message"> |
16 | <message clientCredentialType="Windows" negotiateServiceCredential="true" |
17 | algorithmSuite="Default" /> |
24 | bindingConfiguration="WSDualHttpBinding_IStockQuoteDuplexService" |
25 | contract="ServiceReference.IStockQuoteDuplexService" name="WSDualHttpBinding_IStockQuoteDuplexService"> |
27 | <userPrincipalName value="Administrator@base.com" /> |
31 | </system.serviceModel> |
客户端应用程序在列表4.21显示。客户端实现 IStockQuoteDuplexServiceCallback接口来接收服务端的回调消息。客户端应用程序使用InstantContext类向 IStockQuoteDuplexServiceCallback接口传递一个引用。InstantContext类被传递给客户端代理的构造函数。
列表4.21 wsDualHttpBinding客户端应用
01 | class Program : IStockQuoteDuplexServiceCallback |
03 | private static AutoResetEvent waitForResponse; |
04 | static void Main(string[] args) |
06 | string symbol = "MSFT"; |
07 | waitForResponse = new AutoResetEvent(false); |
08 | InstanceContext callbackInstance = new InstanceContext(new Program()); |
09 | using (StockQuoteDuplexServiceClient client = new StockQuoteDuplexServiceClient(callbackInstance)) |
11 | client.SendQuoteRequest(symbol); |
12 | waitForResponse.WaitOne(); |
17 | #region IStockQuoteCallback Members |
19 | public void SendQuoteResponse(string symbol, double price) |
21 | Console.WriteLine("{0} @ ${1}", symbol, price); |
22 | waitForResponse.Set(); |
列表4.2显示了由svcutil.exe生成的客户端代理。客户端代理和先前实现的最大不同时客户端继承自DuplexClientBase类而不是ClientBase类。DuplexClientBase类添加了对双向通信的支持。
列表4.2 wsDualHttpBinding客户端代理
01 | namespace Client.ServiceReference { |
04 | [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")] |
05 | [System.ServiceModel.ServiceContractAttribute(ConfigurationName="ServiceReference.IStockQuoteDuplexService", CallbackContract=typeof(Client.ServiceReference.IStockQuoteDuplexServiceCallback), SessionMode=System.ServiceModel.SessionMode.Required)] |
06 | public interface IStockQuoteDuplexService { |
09 | void SendQuoteRequest(string symbol); |
12 | [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")] |
13 | public interface IStockQuoteDuplexServiceCallback { |
16 | void SendQuoteResponse(string symbol, double price); |
19 | [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")] |
20 | public interface IStockQuoteDuplexServiceChannel : Client.ServiceReference.IStockQuoteDuplexService, System.ServiceModel.IClientChannel { |
23 | [System.Diagnostics.DebuggerStepThroughAttribute()] |
24 | [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")] |
25 | public partial class StockQuoteDuplexServiceClient : System.ServiceModel.DuplexClientBase<Client.ServiceReference.IStockQuoteDuplexService>, Client.ServiceReference.IStockQuoteDuplexService { |
27 | public StockQuoteDuplexServiceClient(System.ServiceModel.InstanceContext callbackInstance) : |
28 | base(callbackInstance) { |
31 | public StockQuoteDuplexServiceClient(System.ServiceModel.InstanceContext callbackInstance, string endpointConfigurationName) : |
32 | base(callbackInstance, endpointConfigurationName) { |
35 | public StockQuoteDuplexServiceClient(System.ServiceModel.InstanceContext callbackInstance, string endpointConfigurationName, string remoteAddress) : |
36 | base(callbackInstance, endpointConfigurationName, remoteAddress) { |
39 | public StockQuoteDuplexServiceClient(System.ServiceModel.InstanceContext callbackInstance, string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) : |
40 | base(callbackInstance, endpointConfigurationName, remoteAddress) { |
43 | public StockQuoteDuplexServiceClient(System.ServiceModel.InstanceContext callbackInstance, System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) : |
44 | base(callbackInstance, binding, remoteAddress) { |
47 | public void SendQuoteRequest(string symbol) { |
48 | base.Channel.SendQuoteRequest(symbol); |
==============
转载自