WCF系列教程之WCF实例化
本文参考自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实例化的更多相关文章
- WCF系列教程之WCF服务协定
本文参考自:http://www.cnblogs.com/wangweimutou/p/4422883.html,纯属读书笔记,加深记忆 一.服务协定简介: 1.WCF所有的服务协定层里面的服务接口, ...
- WCF系列教程之WCF服务宿主与WCF服务部署
本文参考自http://www.cnblogs.com/wangweimutou/p/4377062.html,纯属读书笔记,加深记忆. 一.简介 任何一个程序的运行都需要依赖一个确定的进程中,WCF ...
- WCF系列教程之WCF服务配置工具
本文参考自http://www.cnblogs.com/wangweimutou/p/4367905.html Visual studio 针对服务配置提供了一个可视化的配置界面(Microsoft ...
- WCF系列教程之WCF服务配置
文本参考自:http://www.cnblogs.com/wangweimutou/p/4365260.html 简介:WCF作为分布式开发的基础框架,在定义服务以及消费服务的客户端时可以通过配置文件 ...
- WCF系列教程之WCF消息交换模式之单项模式
1.使用WCF单项模式须知 (1).WCF服务端接受客户端的请求,但是不会对客户端进行回复 (2).使用单项模式的服务端接口,不能包含ref或者out类型的参数,至于为什么,请参考C# ref与out ...
- WCF系列教程之WCF中的会话
本文参考自http://www.cnblogs.com/wangweimutou/p/4516224.html,纯属读书笔记,加深记忆 一.WCF会话简介 1.在WCF应用程序中,回话将一组消息相互关 ...
- WCF系列教程之WCF客户端异常处理
本文参考自:http://www.cnblogs.com/wangweimutou/p/4414393.html,纯属读书笔记,加深记忆 一.简介 当我们打开WCF基础客户通道,无论是显示打开还是通过 ...
- WCF系列教程之WCF客户端调用服务
1.创建WCF客户端应用程序需要执行下列步骤 (1).获取服务终结点的服务协定.绑定以及地址信息 (2).使用该信息创建WCF客户端 (3).调用操作 (4).关闭WCF客户端对象 二.操作实例 1. ...
- WCF系列教程之WCF操作协定
一.简介 1.在定义服务协定时,在它的操作方法上都会加上OperationContract特性,此特性属于OperationContractAttribute 类,将OperationContract ...
随机推荐
- OpenCV的配置
系统配置:win7 64位系统,编译器 vs2013 一.下载OpenCV安装包(版本2.4.13) https://excellmedia.dl.sourceforge.net/project/op ...
- [转]WCF Data Services OData
http://martinwilley.com/net/data/wcfds.html WCF Data Services About OData Server code Client For .ne ...
- Oracle FND API–Create User
--API - fnd_user_pkg.createuser----Example -- -- ---------------------------------------- API to CRE ...
- Sqler-Cmd
针对于sqler 工具cmd 部分 做了如下整理 1 Cluster Model 2 Regedit Model $servers= '192.168.25.xx','192.168.25.yy' W ...
- Creative Cloud 应用程序 | 直接下载
https://helpx.adobe.com/cn/download-install/kb/creative-cloud-apps-download.html
- [Elixir004]通过环境变量(Environment Variables)来管理config
在elixir的config中我们有时会使用的到一些不想暴露出来的配置项,常用的作法是如Phoenix #config/prod.exs use Mix.Config ... # Finally im ...
- mysql 删表引出的问题
背景 将测试环境的表同步到另外一个数据库服务器中,但有些表里面数据巨大,(其实不同步该表的数据就行,当时没想太多),几千万的数据!! 步骤 1. 既然已经把数据同步过来的话,那就直接delete掉就行 ...
- JavaScript数组(参考资料)
JavaScript数组的长度和元素类型都是非固定的.因为数组的长度可随时改变,并且其数据在内存中也可以不连续,所以 JavaScript 数组不一定是密集型的,这取决于它的使用方式.1.Array. ...
- 纸壳CMS 3.0升级.Net Core 2.1性能大提升
微软发布了.Net Core 2.1正式版,纸壳CMS也在第一时间做了升级,并做了一系列的优化和调整,性能大幅提升,并解决了一些历史遗留问题,添加了一些新功能. Github https://gith ...
- 类似gitlab代码提交的热力图怎么做?
本文由 网易云发布. 作者:张淞(本篇文章仅限知乎内部分享,如需转载,请取得作者同意授权.) 昨夜,网易有数产品经理路过开发的显示屏前见到了类型这样的一张图: 于是想到有数能不能做出这样的图来?作为 ...