本文参考自http://www.cnblogs.com/wangweimutou/p/4516224.html,纯属读书笔记,加深记忆

一、WCF会话简介

1、在WCF应用程序中,回话将一组消息相互关联,从而形成一个回话(回话可以理解为一段时间内的通话,有开始,有结束),会话是服务端和客户端的终结点在在开始回话和结束回话这段时间内的所有消息的一个集合。

2、WCF中的回话机制通过设置服务协定ServiceContract上的SessionMode的枚举值来设置服务协定是否要求、允许或者拒绝基于回话的绑定.枚举值有以下三种:

(1)、Allowed:允许回话,这是SessionMode的默认值,当前协定允许使用会话,则客户端可以进行连接,并选择建立回话或者不建立回话,但是如果回话结束,然后还在当前回话通道发送消息,将会引起异常.

(2)、Required:要求回话,即所有调用(支持调用的基础消息交换)都必须是同一个会话的一部分,如果回话结束,然后还在当前回话通道发送消息,则会重新开一个通道,进行和服务端的通话

(3)、NotAllowed:禁止会话,即服务端不会与客户端进行消息交换。

3、影响WCF会话机制的因素

(1)、设置了SessionMode的值为Required,当采用的BasicHttpBinding时,因为BasicHttpBinding不支持会话,所以程序报错.

(2)、对于WSHttpBinding和WS2007HttpBinding,如果我们将安全模式设置为None(关闭安全会话)并且关闭可靠会话,他们也无法提供会话支持

(3)、对于NetTcpBinding和NetNamedPipeBinding来说,由于其传输类型本身具有支持会话的特性,所以采用了这两种绑定类型的终结点服务协定的会话模式不能设置为NotAllowed,即使关闭了安全会话和可靠会话也不行。

二、WCF中的回话和Asp.Net中的回话

1、WCF中回话的主要功能有以下:

(1)、他们由调用程序显示启动或者关闭

(2)、会话期间传递的消息按照接收消息的顺序进行处理。

(3)、会话将一组消息相互关联,从而形成对话。该关联的含义是抽象的。例如,一个基于会话的通道可能会根据共享网络连接来关联消息,而另一个基于会话的通道可能会根据消息正文中的共享标记来关联消息。可以从会话派生的功能取决于关联的性质。

(4)、不存在与 WCF 会话相关联的常规数据存储区。

2、Asp.Net中的回话由System.Web.SessionState.HttpSessionState 类提供功能,它的主要功能如下:

(1)、Asp.Net的回话是由服务器启动的

(2)、Asp.Net的回话原本是无序的

(3)、ASP.NET 会话提供了一种跨请求的常规数据存储机制。

三、代码示例

工程结构如下图所示:

1、WCF服务层搭建:新建契约层、服务层、和WCF宿主,添加必须的引用(这里不会的参考本人前面的随笔),配置宿主,生成解决方案,打开Host.exe,开启服务。具体的代码如下:

ICalculate.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks; namespace IService
{
[ServiceContract
(
SessionMode = SessionMode.Required
)
]
public interface ICalculate
{
[OperationContract
(
IsTerminating = true//客户端调用Add后会话通道就会关闭会话
)
]
int Add(int a, int b); [OperationContract]
int Divide(int value1, int value2);
}
}

IUserInfo.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks; namespace IService
{
[ServiceContract]
public interface IUserInfo
{
[OperationContract]
User[] GetInfo(int? id);
} [DataContract]
public class User
{
[DataMember]
public int ID { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public int Age { get; set; }
[DataMember]
public string Nationality { get; set; }
}
}

注:必须引入System.Runtime.Serialization命名空间,应为User类在被传输时必须是可序列化的,否则将无法传输

Calculate.cs

using IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel; namespace Service
{
public class Calculate : ICalculate
{
public int Add(int a, int b)
{
return a + b;
} public int Divide(int a, int b)
{
try
{
return a / b;
}
catch (DivideByZeroException) {
throw new FaultException("除数不能为0");//FaultException需要引用System.ServiceModel命名空间
}
}
}
}

UserInfo.cs

using IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Service
{
public class UserInfo : IUserInfo
{
public User[] GetInfo(int? id)
{
List<User> Users = new List<User>();
Users.Add(new User { ID = 1, Name = "张三", Age = 11, Nationality = "China" });
Users.Add(new User { ID = 2, Name = "李四", Age = 12, Nationality = "English" });
Users.Add(new User { ID = 3, Name = "王五", Age = 13, Nationality = "American" }); if (id != null)
{
return Users.Where(x => x.ID == id).ToArray();
}
else
{
return Users.ToArray();
}
}
}
}

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Service;
using System.ServiceModel; namespace Host
{
class Program
{
static void Main(string[] args)
{
using (ServiceHost host = new ServiceHost(typeof(Calculate)))
{
host.Opened += delegate { Console.WriteLine("服务已经启动,按任意键终止!"); };
host.Open();
Console.Read();
}
}
}
}

App.Config

<?xml version="1.0"?>
<configuration>
<system.serviceModel> <services>
<service name="Service.Calculate" behaviorConfiguration="mexBehavior">
<host>
<baseAddresses>
<add baseAddress="http://localhost:1234/Calculate/"/>
</baseAddresses>
</host>
<endpoint address="" binding="wsHttpBinding" contract="IService.ICalculate" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services> <behaviors>
<serviceBehaviors>
<behavior name="mexBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>

ok,打开Host.exe

服务开启成功!

2、创建一个名为Client的客户端控制台应用程序

Program.cs代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Client
{
class Program
{
static void Main(string[] args)
{
try
{
Client.ServiceReference1.CalculateClient client = new Client.ServiceReference1.CalculateClient();
Console.WriteLine("1+2={0}", client.Add(, ));
Console.WriteLine("1+2={0}", client.Add(, ));
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}

通过给ICalculate的Add方法加上了IsTerminating=true,所以当客户端调用了一次Add方法之后,其与服务端的会话通道就会被关闭,所以第二次调用就会报错。

注意:因为默认的服务实例化模型(InstanceContextMode)采用PerSession,即每个服务实例都各自创建了一个会话通道,当Client调用Add后会话关闭,但Client1的会话通道并没有关闭,所以还是可以调用Add。但是如果将InstanceContextMode设置为单例模式,当一个客户端调用完Add方法之后,那么这个通道就被关闭了,另外一个客户端也无法调用了。

WCF系列教程之WCF中的会话的更多相关文章

  1. WCF系列教程之WCF服务协定

    本文参考自:http://www.cnblogs.com/wangweimutou/p/4422883.html,纯属读书笔记,加深记忆 一.服务协定简介: 1.WCF所有的服务协定层里面的服务接口, ...

  2. WCF系列教程之WCF服务宿主与WCF服务部署

    本文参考自http://www.cnblogs.com/wangweimutou/p/4377062.html,纯属读书笔记,加深记忆. 一.简介 任何一个程序的运行都需要依赖一个确定的进程中,WCF ...

  3. WCF系列教程之WCF服务配置工具

    本文参考自http://www.cnblogs.com/wangweimutou/p/4367905.html Visual studio 针对服务配置提供了一个可视化的配置界面(Microsoft ...

  4. WCF系列教程之WCF服务配置

    文本参考自:http://www.cnblogs.com/wangweimutou/p/4365260.html 简介:WCF作为分布式开发的基础框架,在定义服务以及消费服务的客户端时可以通过配置文件 ...

  5. WCF系列教程之WCF消息交换模式之单项模式

    1.使用WCF单项模式须知 (1).WCF服务端接受客户端的请求,但是不会对客户端进行回复 (2).使用单项模式的服务端接口,不能包含ref或者out类型的参数,至于为什么,请参考C# ref与out ...

  6. WCF系列教程之WCF实例化

    本文参考自http://www.cnblogs.com/wangweimutou/p/4517951.html,纯属读书笔记,加深记忆 一.理解WCF实例化机制 1.WCF实例化,是指对用户定义的服务 ...

  7. WCF系列教程之WCF客户端异常处理

    本文参考自:http://www.cnblogs.com/wangweimutou/p/4414393.html,纯属读书笔记,加深记忆 一.简介 当我们打开WCF基础客户通道,无论是显示打开还是通过 ...

  8. WCF系列教程之WCF客户端调用服务

    1.创建WCF客户端应用程序需要执行下列步骤 (1).获取服务终结点的服务协定.绑定以及地址信息 (2).使用该信息创建WCF客户端 (3).调用操作 (4).关闭WCF客户端对象 二.操作实例 1. ...

  9. WCF系列教程之WCF操作协定

    一.简介 1.在定义服务协定时,在它的操作方法上都会加上OperationContract特性,此特性属于OperationContractAttribute 类,将OperationContract ...

随机推荐

  1. Zend_Controller_Front 研究

    如果你裸写php,一个项目就会出现很多的页面控制器(Page Controller),如果项目很大,重复代码就很多,越来越变得很难维护.有了问题,自然就有解决方案!于是前端设计模式  闪亮登场! 前端 ...

  2. 介绍自己,并介绍github注册过程和初步使用

    我是一名南通大学的学生,我叫李可,学号是1413042029,班级:网络工程141,我是一名网络工程专业的学生,我一般喜欢看看课外书. 现在我介绍一下我注册github的过程: 1.登陆https:/ ...

  3. IPA文件的自动化生成和无线分发

    1. IPA的无线分发 iOS应用开发测试过程中,通过无线网络进行IPA包的分发将是非常便捷的,于是也就有了类似testflightapp之类的平台.对于这一功能,我们也可以自己实现,只需要一个简单的 ...

  4. C++并发多线程(一)

    并发:两个或者更多的任务同时发生,一个程序同时执行多个独立的任务. 以往计算机 单核CPU 某一个时刻只能执行一个任务 由操作系统调度 每秒钟进行多次所谓的任务切换并发的假象(不是真正的并发),这种切 ...

  5. 设置TeeChart的提示文本

    使用第三方Steema的TeeChart控件,设置鼠标放在某一线条点上,显示某一点的数据标签问题(虚线型十字光标基准线,放在线上显示对应点的二维坐标轴数据数据),调用InitTeeChartTipTo ...

  6. Mycat SqlServer Do not have slave connection to use, use master connection instead

    Do not have slave connection to use, use master connection instead 很奇怪啊 都是按照配置配置的 怎么就是不通呢 有点怀疑人生了吧 其 ...

  7. show user profile synchronization tools

    C:\Program Files\Microsoft Office Servers\15.0\Synchronization Service\UIShell run miisclient.exe

  8. linux命令之用户管理及用户信息查询命令(下)

    1.visudo:编辑sudoers文件 该命令专门用来编辑/etc/sudoers文件,同时提供语法检查等功能. 示例: 1)执行visudo对普通用户授权 [root@boxiaoyuan ~]# ...

  9. jmeter5.1.1启动提示not able to find java executable or version的解决办法

    安装jmeter5.1.1完成后,启动报错not able to find java executable or version,如下图所示 解决办法: 1.在环境变量PATH的最后添加如下内容:%S ...

  10. [文文殿下]基本的DP技巧

    . 二进制状态压缩动态规划 对于某些情况,如果题目中所给的限制数目比较小,我们可以尝试状态压缩动态规划.例如,题目中给出数据范围\(n<=20\),这个一般情况下是一个状压DP的提示. 状态压缩 ...