zoukankan      html  css  js  c++  java
  • WCF 第四章 绑定 wsDualHttpBinding

    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
    01namespace EssentialWCF
    02{
    03    public interface IStockQuoteCallback
    04    {
    05        [OperationContract(IsOneWay = true)]
    06        void SendQuoteResponse(string symbol, double price);
    07    }
    08    [ServiceContract(CallbackContract=typeof(IStockQuoteCallback), SessionMode= SessionMode.Required)]
    09    public interface IStockQuoteDuplexService
    10    {
    11        [OperationContract(IsOneWay = true)]
    12        void SendQuoteRequest(string symbol);
    13    }
    14 
    15    [ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession)]
    16    public class StockQuoteDuplexService : IStockQuoteDuplexService
    17    {
    18 
    19        #region IStockQuoteDuplexService Members
    20 
    21        public void SendQuoteRequest(string symbol)
    22        {
    23            double value;
    24 
    25            if (symbol == "MSFT")
    26            {
    27                value = 31.15;
    28            }
    29            else if (symbol == "YHOO")
    30            {
    31                value = 28.10;
    32            }
    33            else if (symbol == "GOOG")
    34            {
    35                value = 450.75;
    36            }
    37            else
    38                value = double.NaN;
    39 
    40            OperationContext ctx = OperationContext.Current;
    41            IStockQuoteCallback callback = ctx.GetCallbackChannel<IStockQuoteCallback>();
    42            callback.SendQuoteResponse(symbol, value);
    43        }
    44 
    45        #endregion
    46    }
    47}
    我们必须为我们的例子改变自我寄宿代码因为我们改变了我们用来支持双向消息的实现。列表4.18 显示了为StockQuoteDuplexService服务的寄宿代码。
    列表4.18 StockQuoteDuplexService ServiceHost 服务
    01internal class MyServiceHost
    02{
    03    internal static ServiceHost myServiceHost = null;
    04    internal static void StartService()
    05    {
    06        myServiceHost = new ServiceHost(typeof(EssentialWCF.StockQuoteDuplexService));
    07        myServiceHost.Open();
    08    }
    09    internal static void StopService()
    10    {
    11        if (myServiceHost.State != CommunicationState.Closed)
    12        {
    13            myServiceHost.Close();
    14        }
    15    }
    16}
    列表4.19的配置信息使用wsDualHttpBinding绑定暴露StockQuoteDuplexService服务。
    列表4.19 wsDualHttpBinding 宿主配置
    01<?xml version="1.0" encoding="utf-8" ?>
    02<configuration>
    03    <system.serviceModel>
    04        <behaviors>
    05            <serviceBehaviors>
    06                <behavior name="NewBehavior">
    07                    <serviceMetadata httpGetEnabled="true" />
    08                </behavior>
    09            </serviceBehaviors>
    10        </behaviors>
    11        <services>
    12            <service behaviorConfiguration="NewBehavior" name="EssentialWCF.StockQuoteDuplexService">
    13                <endpoint address="" binding="wsDualHttpBinding" bindingConfiguration=""
    14                    contract="EssentialWCF.IStockQuoteDuplexService" />
    15                <host>
    16                    <baseAddresses>
    17                        <add baseAddress="http://localhost/stockquoteservice" />
    18                    </baseAddresses>
    19                </host>
    20            </service>
    21        </services>
    22    </system.serviceModel>
    23</configuration>
     
    列表4.20显示了配置信息是客户端用来调用使用wsDualHttpBinding绑定并实现IStockQuoteDuplexService契约的服务的。clientBaseAddress确定了客户端要监听回调消息的终结点。
    列表4.20 wsDualHttpBinding客户端配置
    01<?xml version="1.0" encoding="utf-8" ?>
    02<configuration>
    03    <system.serviceModel>
    04        <bindings>
    05            <wsDualHttpBinding>
    06                <binding name="SpecifyClientBaseAddress" clientBaseAddress="http://localhost:8001/client" />
    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" />
    18                    </security>
    19                </binding>
    20            </wsDualHttpBinding>
    21        </bindings>
    22        <client>
    23            <endpoint address="http://localhost/stockquoteservice" binding="wsDualHttpBinding"
    24                bindingConfiguration="WSDualHttpBinding_IStockQuoteDuplexService"
    25                contract="ServiceReference.IStockQuoteDuplexService" name="WSDualHttpBinding_IStockQuoteDuplexService">
    26                <identity>
    27                    <userPrincipalName value="Administrator@base.com" />
    28                </identity>
    29            </endpoint>
    30        </client>
    31    </system.serviceModel>
    32</configuration>
    客户端应用程序在列表4.21显示。客户端实现 IStockQuoteDuplexServiceCallback接口来接收服务端的回调消息。客户端应用程序使用InstantContext类向 IStockQuoteDuplexServiceCallback接口传递一个引用。InstantContext类被传递给客户端代理的构造函数。
    列表4.21 wsDualHttpBinding客户端应用
    01class Program : IStockQuoteDuplexServiceCallback
    02{
    03    private static AutoResetEvent waitForResponse;
    04    static void Main(string[] args)
    05    {
    06        string symbol = "MSFT";
    07        waitForResponse = new AutoResetEvent(false);
    08        InstanceContext callbackInstance = new InstanceContext(new Program());
    09        using (StockQuoteDuplexServiceClient client = new StockQuoteDuplexServiceClient(callbackInstance))
    10        {
    11            client.SendQuoteRequest(symbol);
    12            waitForResponse.WaitOne();
    13        }
    14        Console.ReadLine();
    15    }
    16 
    17    #region IStockQuoteCallback Members
    18 
    19    public void SendQuoteResponse(string symbol, double price)
    20    {
    21        Console.WriteLine("{0} @ ${1}", symbol, price);
    22        waitForResponse.Set();
    23    }
    24 
    25    #endregion
    26}
    列表4.2显示了由svcutil.exe生成的客户端代理。客户端代理和先前实现的最大不同时客户端继承自DuplexClientBase类而不是ClientBase类。DuplexClientBase类添加了对双向通信的支持。
    列表4.2 wsDualHttpBinding客户端代理
    01namespace Client.ServiceReference {
    02     
    03     
    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 {
    07         
    08        [System.ServiceModel.OperationContractAttribute(IsOneWay=true, Action="http://tempuri.org/IStockQuoteDuplexService/SendQuoteRequest")]
    09        void SendQuoteRequest(string symbol);
    10    }
    11     
    12    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
    13    public interface IStockQuoteDuplexServiceCallback {
    14         
    15        [System.ServiceModel.OperationContractAttribute(IsOneWay=true, Action="http://tempuri.org/IStockQuoteDuplexService/SendQuoteResponse")]
    16        void SendQuoteResponse(string symbol, double price);
    17    }
    18     
    19    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
    20    public interface IStockQuoteDuplexServiceChannel : Client.ServiceReference.IStockQuoteDuplexService, System.ServiceModel.IClientChannel {
    21    }
    22     
    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 {
    26         
    27        public StockQuoteDuplexServiceClient(System.ServiceModel.InstanceContext callbackInstance) :
    28                base(callbackInstance) {
    29        }
    30         
    31        public StockQuoteDuplexServiceClient(System.ServiceModel.InstanceContext callbackInstance, string endpointConfigurationName) :
    32                base(callbackInstance, endpointConfigurationName) {
    33        }
    34         
    35        public StockQuoteDuplexServiceClient(System.ServiceModel.InstanceContext callbackInstance, string endpointConfigurationName, string remoteAddress) :
    36                base(callbackInstance, endpointConfigurationName, remoteAddress) {
    37        }
    38         
    39        public StockQuoteDuplexServiceClient(System.ServiceModel.InstanceContext callbackInstance, string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) :
    40                base(callbackInstance, endpointConfigurationName, remoteAddress) {
    41        }
    42         
    43        public StockQuoteDuplexServiceClient(System.ServiceModel.InstanceContext callbackInstance, System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) :
    44                base(callbackInstance, binding, remoteAddress) {
    45        }
    46         
    47        public void SendQuoteRequest(string symbol) {
    48            base.Channel.SendQuoteRequest(symbol);
    49        }
    50    }
    51}


    ==============

    转载自

     

  • 相关阅读:
    从B树、B+树、B*树谈到R 树
    平衡二叉树、B树、B+树、B*树
    数据库事务和四种隔离级别
    python 安装surprise库解决 c++tools错误问题
    python的sorted函数
    爬虫出现gbk错误
    Windows下Python安装numpy+mkl,Scipy和statsmodels
    Flask--框架及路由
    flask常见面试题
    RE正则表达式
  • 原文地址:https://www.cnblogs.com/llbofchina/p/2093012.html
Copyright © 2011-2022 走看看