Three ways to do WCF instance management
Three ways to do WCF instance management (Per call, Per session, and Single).
Introduction
Very often we would like to control the way WCF service objects are instantiated on a WCF server. You would want to control how long the WCF instances should be residing on the server.
The WCF framework has provided three ways by which we can control WCF instance creation. In this article, we will first try to understand those three ways of WCF service instance control with simple code samples of how to achieve them. Finally, we will compare when to use them and under what situations.
There is a small ebook for all my .NET friends which covers topics like WCF, WPF, WWF, AJAX, Core .NET, SQL, etc., which you can download from here or you can catch me on my daily free training from here.
WCF service object instancing basics
In normal WCF request and response communication, the following sequence of actions takes place:
- WCF client makes a request to a WCF service object.
- WCF service object is instantiated.
- WCF service instance serves the request and sends the response to the WCF client.
Following is a pictorial representation of how WCF requests and responses work.
Following are different ways by which you can create WCF instances:
- Create a new WCF service instance on every WCF client method call.
- Only one WCF service instance should be created for every WCF client session.
- Only one global WCF service instance should be created for all WCF clients.
To meet the above scenarios, WCF has provided three ways by which you can control WCF service instances:
- Per call
- Per session
- Single instance
Per call instance mode
When we configure a WCF service as per call, new service instances are created for every method call you make via a WCF proxy client. The image below shows this in a pictorial format:
- The WCF client makes the first method call (method call 1).
- A new WCF service instance is created on the server for this method call.
- The WCF service serves the request and sends the response and the WCF instance is destroyed and given to the garbage collector for clean up.
- Now let’s say the WCF client makes a second method call, a new instance is created, the request is served, and the WCF instance is destroyed.
In other words, for every WCF client method call, a WCF service instance is created, and destroyed once the request is served.
How to implement WCF per call instancing
In order to specify the instancing mode, we need to provide the InstanceContextMode value in the ServiceBehavior attribute as shown below. This attribute needs to specified on the Service class. In the below code snippet, we have specified intCounter as a class level variable and the class counter is incremented by one when the Increment method is called.
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Percall)]
public class Service : IService
{
private int intCounter; public int Increment()
{
intCounter++
return intCounter;
}
}
At the client, we consume the WCF client and we call the Increment
method twice.
ServiceReference1.ServiceClient obj = new ServiceReference1.ServiceClient();
MessageBox.Show(obj.Increment().ToString());
MessageBox.Show(obj.Increment().ToString());
Even though we have called the Increment
method twice, we get the value ‘1’.
In other words, the WCF service instance is created for every method call made to the WCF service instance so the value will always be one.
Per session instance mode
Very often we need to maintain state between method calls or for a particular session.
For those kinds of scenarios, we will need to configure the service per session.
In per session, only one instance of a WCF service object is created for a session interaction.
The figure below explains this in pictorial format.
- The client creates the proxy of the WCF service and makes method calls.
- A WCF service instance is created which serves the method response.
- The client makes one more method call in the same session.
- The same WCF service instance serves the method call.
- When the client finishes its activity, the WCF instance is destroyed and served to the garbage collector for clean up.
How to implement per session instancing
To configure service as per session, we need to configure the ServiceBehavior attribute with a PerSession value in the InstanceContextMode object.
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
public class Service : IService
{
private int intCounter;
public int Increment()
{
intCounter++
return intCounter;
}
}
At the client side, when we run the below client code, you should see the value ‘2’ after the final client code is executed.
We have called the method twice so the value will be seen as two.
ServiceReference1.ServiceClient obj = new ServiceReference1.ServiceClient();
MessageBox.Show(obj.Increment().ToString());
MessageBox.Show(obj.Increment().ToString());
Single instance mode
Often we would like to create one global WCF instance for all WCF clients. To create a single instance of a WCF service, we need to configure the WCF service as Single instance mode. Below is a simple pictorial notation of how the single instance mode will operate:
- WCF client 1 requests a method call on the WCF service.
- A WCF service instance is created and the request is served. The WCF service instance is not destroyed, the service instance is persisted to server other requests.
- Now let’s say some other WCF client, e.g., client 2, requests a method call.
- The same WCF instance which was created by WCF client 1 is used to serve the request for WCF client 2. In other words, only one global WCF server service instance is created to serve all client requests.
How to implement single instance mode
In order to create a single instance of a WCF service, we need to specify InstanceContextMode as Single.
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class Service : IService
{
}
If you call the WCF service from a different client, you will see the counter incrementing. The counter becomes a global variable.
When should you use per call, per session, and single mode?
Per call
- You want a stateless services.
- Your service holds intensive resources like connection objects and huge memory objects.
- Scalability is a prime requirement. You would like to have a scaled out architecture.
- Your WCF functions are called in a single threaded model.
Per session
- You want to maintain states between WCF calls.
- You a scaled up architecture.
- Light resource references.
Single
- You want share global data through your WCF service.
- Scalability is not a concern.
References
- MSDN link for WCF instances: http://msdn.microsoft.com/en-us/library/ms733040.aspx.
- Do not miss this post which covers end to end about WCF sessions:http://codeidol.com/csharp/wcf/Instance-Management/.
- Great blog by Rick rain on WCF instancing: http://blogs.msdn.com/b/rickrain/archive/2009/06/15/wcf-instancing-concurrency-and-throttling-part-1.aspx.
Three ways to do WCF instance management的更多相关文章
- WCF学习系列二---【WCF Interview Questions – Part 2 翻译系列】
http://www.topwcftutorials.net/2012/09/wcf-faqs-part2.html WCF Interview Questions – Part 2 This WCF ...
- WCF Concurrency (Single, Multiple, and Reentrant) and Throttling
http://www.codeproject.com/Articles/89858/WCF-Concurrency-Single-Multiple-and-Reentrant-and Introduc ...
- (WCF) 多线程 (Multi-threading) 和 并发性 (Concurency)
问题:WCF 有个Server端,还有个Client端,他们之间是如何进行并发,多线程通信的呢?多个Client端同时访问Server,如何保证Server端的操作线程安全呢? 在理解WCF Conc ...
- WCF学习系列汇总
最近在学习WCF,打算把一整个系列的文章都”写“出来,包括理论和实践,这里的“写”是翻译,是国外的大牛写好的,我只是搬运工外加翻译.翻译的不好,大家请指正,谢谢了.如果觉得不错的话,也可以给我点赞,这 ...
- WCF学习笔记(一)
WCF是什么? 官方解释: Windows Communication Foundation (WCF) 是用于构建面向服务的应用程序的框架.借助 WCF,可以将数据作为异步消息从一个服务终结点发送至 ...
- WCF 简单示例
WCF(Windows Communication Foundation,WCF)是基于Windows平台下开发和部署服务的软件开发包(Software Development Kit,SDK).WC ...
- 《WCF技术剖析》博文系列汇总[持续更新中]
原文:<WCF技术剖析>博文系列汇总[持续更新中] 近半年以来,一直忙于我的第一本WCF专著<WCF技术剖析(卷1)>的写作,一直无暇管理自己的Blog.在<WCF技术剖 ...
- Low overhead memory space management
Methods, apparatus, and systems, including computer programs encoded on a computer storage medium, m ...
- WCF vs ASMX WebService
This question comes up a lot in conversations I have with developers. “Why would I want to switch to ...
随机推荐
- C#创建自定义配置节点
转载:http://www.educity.cn/develop/495003.html 在.Net应用程序中我们经常看到VS为我们生成的项目工程中都会含有app.config或者web.connfi ...
- oracle 定义临时表
创建Oracle 临时表,可以有两种类型的临时表: 会话级的临时表 事务级的临时表 . 1) 会话级的临时表因为这这个临时表中的数据和你的当前会话有关系, 当你当前SESSION不退出的情况下,临时表 ...
- swift-闭包和类的声明
//闭包:类似Oc中的block 反向传值引起代码的回调 func hasClosureMathes(arr : [Int],value:Int,cb:(num:Int,value : Int)-&g ...
- 摘录android工具类
import android.content.Context; import android.content.pm.PackageInfo; import android.content.pm.Pac ...
- NET异步调用Webserver
之前,有个同事跑来问我一堆的什么多线程异步进行调用Sap的服务再突然把进程关闭,还说要设置一个循环判断调用的结果,搞得我听的一头雾水,但是我明显感觉到他的设计思路已经渐行渐远了...已经再偏远的山区中 ...
- eclipse下使用maven配置库托管jar包
1.项目是通过maven配置库托管jar包 首先要保证maven配置库中有相应的jar包才能通过这个方法来添加jar包.maven的有点就是把要用到的jar包统一放在一个配置库中,在某个项目需要用到这 ...
- SQL的经典操作——批量复制同行的其它列数据到其它列数据
看图说话比较直观: 对比复制前后的数据表: 使用SQL语句:UPDATE OR ROLLBACK Content SET YINBIAO = YINBIAO2, GESHU = GESHU2 WHER ...
- (转)IOS学习笔记-2015-03-29 int、long、long long取值范围
unsigned - - unsigned - - unsigned __int64的最大值: __int64的最小值:- unsigned __int64的最大值:
- linux 文件操作编程
Linux中所有的设备和文件的操作都使用文件描述符来进行. 文件描述符是一个非负的整数,它是一个索引值,指向内核中每个进程打开的记录表. 当打开一个文件或者创建一个新文件时,内核就向进程返回一个文件描 ...
- redis 安装及配置
一.安装Redis 1.到官网下载redis最新版本,我下载的是 http://redis.io/ 2.拷贝redis-3.0.3到/usr/local目录 3.解压缩sudo tar -zxf re ...