那些年,我们一起学WCF--(9)会话行为

      WCF会话行为在客户端与服务器端进行通信会话时,对传输层会话方式的设置。在WCF中,通过SessionMode设置会话行为,总共有三种会话方式

       allowed=0 允许进行会话,此为默认值

       Required=1 必须进行会话

       NotAllowed=2 不允许进行会话

      并不是所有通信协议都支持会话,只有nettcpbinding,netnamepipebinding,wshttpbinding,wsfederationhttpbinding,wsdualhttpbinding,wshttpbinding等通信协议支持传输层会话。

      allowed表示可以使用传输层会话,要依据所使用的通信协议而定

      required表示必须使用传输层会话,如果所使用的通信协议不支持传输层会话,服务将报异常。

     notallowed表示禁用传输层会话。

    接下来,我们看看三种实例模式怎样使用会话模式.

    1.PerCall

            1.1   Allowed + wsHttpBinding

                我们使用支持会话的通信协议wsHttpBinding,并采用allowed会话模式,来看下演示效果,在客户端我们创建两个客户端代理实例,发现PerCall模式每次客户端调用结束后,立刻释放服务实例,但是同一个客户端代理多次调用,都是同一个sessionID,这说明,虽然PerCall模式调用结束后,立即销毁服务实例,但是客户端代理仍然保持与服务器端的连接。

            客户端代码  

     private void button1_Click(object sender, EventArgs e)
        {
            ChannelFactory<IPerCall> channelFactory = new ChannelFactory<IPerCall>("WSHttpBinding_IPerCall");  
             IPerCall client = channelFactory.CreateChannel();
             client.AddCount();
             client.AddCount();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ChannelFactory<IPerCall> channelFactory = new ChannelFactory<IPerCall>("WSHttpBinding_IPerCall");
            IPerCall client = channelFactory.CreateChannel();
            client.AddCount();
            client.AddCount();
        }

  执行结果:

    

                1.2  Allowed + basicHttpBinding

                     由于basicHttpBinding协议不支持会话,所以客户端调用服务器端的时候,不会产生会话session

                   客户端代码       

   private void button1_Click(object sender, EventArgs e)
        {
            ChannelFactory<IPerCall> channelFactory = new ChannelFactory<IPerCall>("WSHttpBinding_IPerCall");  
             IPerCall client = channelFactory.CreateChannel();
             client.AddCount();
             client.AddCount();
        }

                    执行结果

                     

                  
              1.3  Required + basicHttpBinding

                     Required要求必须使用会话模式,但是basicHttpBinding协议不支持传输层会话,这时候启动服务器端就会出现异常。

                    

            1.4 Required + wsHttpBinding

                   Required要求必须使用会话,wsHttpBinding是支持会话的通信协议,所以此种搭配,客户端调用服务器端后,服务实例会立即释放,但是客户端的代理仍然与服务器端保持着连接,同一个代理多次调用,会话产生的sessionID一样。

               

           1.5 NotAllowed + wsHttpBinding

              由于NotAllowed表示禁止使用会话,wsHttpBinding表示支持传输层会话,即使wsHttpBinding是支持会话的协议,但是在客户端调用的过程中,还是不会产生会话的,此时sessionid为空。

         
 

         1.6  NotAllowed + basicHttpBinding

             NotAllowed表示禁止使用会话,basicHttpBinding是不支持会话的绑定,所以此时通信过程中不会产生会话SESSION

             

 

    2. PerSession

        PerSession是支持会话的实例模式,但是并不一定只要把实例模式设置为PerSession就一定支持会话行为。还要视会话设置和通信协议情况而定。

       2.1  NotAllowed+ basicHttpBinding

              NotAllowed表示禁止使用会话,basicHttpBinding是不支持会话的通信协议,所有此时即使把实例模式设置为PerSession,仍不支持会话。

             下面的例子演示了,同一个代理调用两次服务器方法,每次调用服务器端的变量值并没有累加,sessionID为空,类似于percall模式

             客户端代码

    private void button3_Click(object sender, EventArgs e)
        {
            //persession调用
            ChannelFactory<IPerSession> channelFactory = new ChannelFactory<IPerSession>("BasicHttpBinding_IPerSession");
            IPerSession sessionClient =channelFactory.CreateChannel();
            sessionClient.AddCount();
            sessionClient.AddCount();

        }

         执行结果

            

      2.2 NotAllowed + wsHttpBinding

          NotAllowed是禁止使用会话行为,wsHttpBinding是支持会话的通信协议,此时即使使用的是PerSession实例行为,但是仍然禁止使用会话。

    下面的例子演示了客户端同一个代理调用服务器端方法两次后,变量值并未增加,sessionID为空.

         执行结果如下

          

        2.3 Allowed + wsHttpBinding

            Allowed表示允许会话,wsHttpBinding是支持传输层会话的协议,所以此时是支持会话模式的,同一个客户端代理调用多次服务器端的方法,变量值会一直递增。

         俩次执行的sessionID是一样的。

           执行结果:

         

       2.4 Allowed + basicHttpBinding

              Allowed表示允许使用会话,basicHttpBinding是不支持会话的通信协议,所以即使设置为Persession实例,仍然不支持会话,sessionID为空。

       和PerCall模式下变量的执行结果一样。

           执行结果:

          

     2.5 Required + basicHttpBinding

         Required表示必须使用会话行为,basicHttpBinding是不支持传输层会话的通信方式,这是启动服务器实例的时候就会出现异常。

          

       2.6 Required + wsHttpBinding

            Required表示必须使用会话,wsHttpBinding是支持传输层会话的通信协议,所有此时在一个实例的会话期内变量的值会一直增加。

            执行结果如下,一个客户端代理调用两次方法,SESSIONID一样。

           

 

   3. Single实例

         3.1  Allowed + wsHttpBinding

            Allowed表示允许会话,wsHttpBinding是支持传输层会话的协定,所以此时是支持会话的。下面的代码演示了客户端调用服务器端的方法,一个局部客户端代理和一个全局客户端代理,每次调用,服务器端的变量值会递增,同一个客户端代理的的多次调用sessionid是一样的。

    

rivate void button4_Click(object sender, EventArgs e)
        {
            //single调用,一个代理多次调用在服务器端的sessionid是一样的
            ChannelFactory<ISingle> channelFactory = new ChannelFactory<ISingle>("WSHttpBinding_ISingle");
            ISingle Client = channelFactory.CreateChannel();
            Client.AddCount();
            Client.AddCount();
        }

        ISingle singleClient = null;//全局客户端代理
        private void Form1_Load(object sender, EventArgs e)
        {
            ChannelFactory<ISingle> channelFactory = new ChannelFactory<ISingle>("WSHttpBinding_ISingle");
            singleClient = channelFactory.CreateChannel();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            //全局客户端代理
            singleClient.AddCount();
        }


   执行结果如下:

      

     3.2  Allowed + basicHttpBinding

         Allowed表示允许会话,  basicHttpBinding是不支持会话的通信协议,所以此时是不支持会话的。客户端调用服务器端的时候,sessionID为空

        执行结果如下

        

     3.3  Required + basicHttpBinding

           Required是表示必须使用会话,basicHttpBinding是不支持会话的绑定,所以此时服务器端允许会出现异常,如下图

           

       3.4 Required + wsHttpBinding

            Required表示必须支持会话,wsHttpBinding是支持会话的通信协议,所以采用此种方式会生成会话sessionid,

          

      3.5 NotAllowed + wsHttpBinding

              NotAllowed表示禁止使用会话,wsHttpBinding是支持会话的绑定,但是此时是不会产生会话session的,sessionid为空

            

      3.6 NotAllowed + basicHttpBinding

            NotALLowed表示禁止使用会话,basicHttpBinding是不支持会话的绑定,所以此时是不会产生会话信息的,sessionid为空,执行结果如下

          

     demo下载:http://download.csdn.net/detail/zx13525079024/4600066

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值