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

一、理解WCF实例化机制

1、WCF实例化,是指对用户定义的服务对象以及与其相关的实例上下文对象的生存期的控制,也就是说每一个服务类其实就是一个服务实例,客户端在调用服务端中的服务方法时,需要实例化一个服务端代理类对象,实例化就是对这个对象的生命周期的管理。

2、实例化行为(使用 System.ServiceModel.ServiceBehaviorAttribute.InstanceContextMode 属性进行设置),InstanceContextMode属性有两个作用:

(1)、控制如何创建 InstanceContext 以响应传入的消息

(2)、每个 InstanceContext 都与一个用户定义服务对象相关联,因此设置 InstanceContextMode 属性也可以控制用户定义服务对象的实例化

默认情况下,InstanceContextMode 枚举定义了实例化模式。可以使用下列实例化模式:

(1)、PerCall:单调模式,为每个客户端请求创建一个新的 InstanceContext(以及相应的服务对象)。

(2)、PerSession:会话模式,这是InstanceContextMode的默认值,为每个新的客户端会话创建一个新的 InstanceContext(以及相应的服务对象),并在该会话的生存期内对其进行维护(这需要使用支持会话的绑定).

(3)、Single:单例模式,单个 InstanceContext(以及相应的服务对象)处理应用程序生存期内的所有客户端请求。

二、实例化模式PerCall、PerSession、Single详解

1、单调模式

单调模式中,WCF总是创建一个新的服务实例上下文来处理请求对象,即客户端每调用一次方法就会创建一个实例上下文对象,调用完成靠GC完成释放对象(但是GC的回收具有不确定性,所以会有延迟),在调用下一个方法会创建下一个实例上下文,因此,一个会话通道可能会出现多个实例上下文对象。

代码实例:

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

IPerCall.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 IPerCall
{
[OperationContract]
int AddMethod(int a, int b);
[OperationContract]
int AddMethod1(int a, int b);
[OperationContract]
string GetInstanceId();//获取实例Id
[OperationContract]
int GetInstanceCount(); //获取实例数
[OperationContract]
int GetOperationCount(); //获取调用操作方法的计数器 }
}

PerCall.cs

using IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks; namespace Service
{
[ServiceBehavior
(
InstanceContextMode=InstanceContextMode.PerCall//设置当前服务类的实例化模式为单调模式,每调用一次方法,生成一个上下文实例
)]
public class PerCall:IPerCall
{
int operationCount;
static int instanceCount;
string instanceId;
public PerCall() {
instanceCount++;
instanceId = Guid.NewGuid().ToString();
}
public string GetInstanceId()
{
return instanceId;
} public int GetOperationCount()
{
return operationCount;
} public int AddMethod(int a, int b)
{
operationCount++;
return a + b;
} public int AddMethod1(int a, int b)
{
operationCount++;;
return a + b;
} public int GetInstanceCount()
{
return instanceCount;
}
}
}

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks; namespace Host
{ class Program
{
static void Main(string[] args)
{
using (ServiceHost host = new ServiceHost(typeof(Service.PerCall)))
{
host.Open();
Console.WriteLine("服务已启动,按任意键中止...");
Console.ReadKey(true);
host.Close();
}
}
}
}

App.Config

<?xml version="1.0"?>
<configuration>
<system.serviceModel> <services>
<service name="Service.PerCall" behaviorConfiguration="mexBehavior">
<host>
<baseAddresses>
<add baseAddress="http://localhost:1234/PerCall/"/>
</baseAddresses>
</host>
<endpoint address="" binding="wsHttpBinding" contract="IService.IPerCall" />
<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的客户端控制台应用程序,添加对http://localhost:1234/PerCall/的引用

(1)、单调模式

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)
{
Client.PerCallClientNS.PerCallClient client = new PerCallClientNS.PerCallClient();
Console.WriteLine("1+2={0},这是第{1}个实例上下文对象,实例ID是:{2},方法的操作数:{3}", client.AddMethod(, ),client.GetInstanceCount(),client.GetInstanceId(),client.GetOperationCount());
Console.WriteLine("3+3={0},这是第{1}个实例上下文对象,实例ID是:{2},方法的操作数:{3}", client.AddMethod1(, ), client.GetInstanceCount(), client.GetInstanceId(), client.GetOperationCount());
Console.ReadKey();
}
}
}

通过结果分析,客户端通过会话通过每调用一次服务端的方法,就会创建一个服务端上下文实例,通过这个变量就可以看出

所以除非当前通道关闭,要不然只要调用一次当前服务端的方法,那么实例树还会继续上升。

(2)、单例模式(Single)

在单例模式中,WCF只会创建一个实例上下文来处理服务的所有的请求调用对象,即PerCall只会被实例化一次,不管调用的请求对象是在同一个客户端还是在不同的客户端.

代码实例,在上面代码的基础上将PerCall类的ServiceBehavior属性

 [ServiceBehavior
(
InstanceContextMode=InstanceContextMode.PerCall//设置当前服务类的实例化模式为单调模式,每调用一次方法,生成一个上下文实例
)]

修改成

[ServiceBehavior
(
InstanceContextMode=InstanceContextMode.Single//设置当前服务类的实例化模式为单例模式,只生成一个上下文实例,所以客户端共享这一个实例
)]

重新部署工程

不管执行多少次,当前总是第一个实例,说明当前上下文只生成了一个,应为没有执行构造函数,所以只能是1,但是操作数的增加,说明了,客户端共享了这一个实例,只要当前通道不关闭,那么操作数会一直增加。

(3)、会话模式(PerSession)

在会话模式(PerSession)中,WCF会对客户端与服务端的每一个会话通道创建一个实例上下文。即不同的客户端在各自的会话通道的实例上下文中处理请求对象。

在上面代码的基础上将PerCall类的ServiceBehavior属性的InstanceContextMode,修改成

[ServiceBehavior
(
InstanceContextMode=InstanceContextMode.PerSession//设置当前服务类的实例化模式为会话模式,WCF会为每一个会话创建一个上下文实例
)]

重新部署工程

每调用一次客户端,生成一个上下文实例,每次都是新的实例

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/4516224.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. Ansible 笔记 (1) - 安装和配置

    本文参考 <Ansible 自动化运维和最佳实践>,这两天刚读这本书,写写总结.主控机环境是 centos 7,被控机均是 centos 6.8 . 确保 python 版本大于 2.6 ...

  2. Google Tango Java SDK开发:Configure and Connect 配置和连接

    Configure and Connect 配置和连接 Note: This section assumes you are familiar with the Android Activity Li ...

  3. .NET基础 (12)时间的操作System.DateTime

    时间的操作System.DateTime1 DateTime如何存储时间2 如何在DateTime对象和字符串对象之间进行转换3 什么是UTC时间,如何转换到UTC时间 时间的操作System.Dat ...

  4. Array对象的创建及其操作方法

    一.创建数组,即实例化数组对象      有三种方式:1. new Array();                          2.new Array(size);               ...

  5. 解决linux下tomcat停止进程任存在问题

    解决linux下tomcat停止进程任存在问题 在Linux下(之所以强调linux下,是因为在windows下正常),执行tomcat ./shutdown.sh 后,虽然tomcat服务不能正常访 ...

  6. 企业搜索引擎开发之连接器connector(二十八)

    通常一个SnapshotRepository仓库对象对应一个DocumentSnapshotRepositoryMonitor监视器对象,同时也对应一个快照存储器对象,它们的关联是通过监视器管理对象D ...

  7. 第二周leetcode

    4/4 这周莫名得忙,一天是做编译,一天是做模式识别作业,(一天刷魔兽皮肤),周末玩了两天,总的来说还是松懈了,大概只做了两天的leetcode,刷了10道题,羞愧羞愧. 决定每次把代码附上在这个总结 ...

  8. CAS实战の获取多用户信息

    先列出版本号: 服务端版本:cas server 4.0.0 客户端版本:cas client 3.3.3 cas server step1:先将primaryPrincipalResolver be ...

  9. CentOS6.5中使用 iperf 检测主机间网络带宽

    安装方式 # yum install http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm# yum inst ...

  10. angular 响应式表单指令

    响应式表单都是以 form开头的指令 第一列指令(不以name结尾)在html模版中,用 [ ] 第二列指令(以name结尾)在html模版中,不用 [ ]