WCF Concurrency (Single, Multiple, and Reentrant) and Throttling
http://www.codeproject.com/Articles/89858/WCF-Concurrency-Single-Multiple-and-Reentrant-and
Introduction and goal
In this article, we will concentrate on WCF concurrency and throttling.
We will first try to understand what is WCF concurrency and the three important types of WCF concurrency.
We will then see a small sample of WCF concurrency with Single and Multiple.
We will then go through 9 combinations of WCF concurrency and instancing.
Finally we will try to understand how to configure throttling using the WCF ‘web.config’ file.

Pre-requisites
In order to understand WCF concurrency well, it’s important to understand the concepts around WCF instancing.
So before moving ahead with this article, please do read about WCF instancing from here.
Why do we need concurrency in WCF?
If you search on the web for the dictionary meaning of concurrency, you will find the following definition:
“Happening at the same time”
WCF concurrency helps us configure how WCF service instances can serve multiple requests at the same time. You will need WCF concurrency for the below prime reasons; there can be other reasons as well but these stand out as important reasons:
- Increase throughput: Many times you want to increase the amount of work your WCF service instance does at any moment of time. In other words, you would like to increase the throughput. Throughput means how much work a given thing can do.
By default, a WCF service handles only one request at a given moment of time.
- Integration with a legacy system: Many times your WCF services interact with legacy systems like VB6, COM, etc. It’s very much possible that these systems are not multithreaded, in other words they handle only one request at any given time. So even though your WCF service has concurrent capabilities, you would still like to handle one request at a time. This is achieved by using throttling in combination with WCF concurrency capabilities.

WCF concurrency and instancing – Two different things
In our previous article, we had discussed about WCF instances. WCF instancing and WCF concurrency are two different things. WCF instance dictates how objects are created while WCF concurrency dictates how many requests can be handled by WCF objects.
I do understand that many of our developer friends know this difference, but as you go deeper into WCF concurrency, there is lot of connection with WCF instancing also, so I wanted to just emphasize the differences.
WCF instancing gives three levels of WCF object instance controls: per call, per session, and single. In case you are not aware of this, read my article: 3 way to do WCF instance management. In similar lines, WCF also has three ways of implementing WCF concurrency.

Three types of WCF concurrency
There are three ways by which you can handle concurrency in WCF: single, multiple, and reentrant. To specify WCF concurrency, we need to use the ServiceBehavior tag as shown below, with the appropriate ‘ConCurrencyMode’ property value.

Single: A single request has access to the WCF service object at a given moment of time. So only one request will be processed at any given moment of time. The other requests have to wait until the request processed by the WCF service is completed.
Multiple: In this scenario, multiple requests can be handled by the WCF service object at any given moment of time. In other words, requests are processed at the same time by spawning multiple threads on the WCF server object. So you have great throughput here but you need to ensure concurrency issues related to WCF server objects.
Reentrant: A single request thread has access to the WCF service object, but the thread can exit the WCF service to call another WCF service or can also call a WCF client through callback and reenter without deadlock.
Sample code demonstration
In order to demonstrate this, let’s create a simple sample code as shown below. We will create a simple WCF service with a method name Call. When a client calls this WCF service, it will display the following details:
- Client name that made the call. This value will be provided as an input when the client wants to make a call to the WCF service.
- Instance number, this will represent the number of WCF instances which are serving the request.
- Thread number which is executing the method.
- Time when the
Callmethod was actually called.
Below is the service contract of the Call method. Please note that OperationContract is defined withIsOneWay as true.
[ServiceContract]
public interface IHelloWorldService
{
[OperationContract(IsOneWay=true)]
void Call(string ClientName);
}
Below is a simple implementation of the service contract IHelloWorldService interface. It has an instance variable i which helps us maintain the instance counter and a simple Console.Writeline which displays the client name, instance number, thread number, and time when the method was called.
This method waits for 5 seconds using the ‘Thread.Sleep’ function.
public class HelloWorldService : IHelloWorldService
{
// maintain instance count
public int i;
public void Call(string ClientName)
{
// increment instance counts
i++; // display client name, instance number , thread number and time when
// the method was called Console.WriteLine("Client name :" + ClientName + " Instance:" +
i.ToString() + " Thread:" + Thread.CurrentThread.ManagedThreadId.ToString() +
" Time:" + DateTime.Now.ToString() + "\n\n"); // Wait for 5 seconds
Thread.Sleep();
}
}
This WCF service will be self hosted using WsHttpBinding as shown below. We have chosen self hosting so that we can monitor the time, threads, and number of instances of the WCF service.
static void Main(string[] args)
{
//Create a URI to serve as the base address
Uri httpUrl = new Uri("http://localhost:8010/MyService/HelloWorld"); //Create ServiceHost
ServiceHost host = new ServiceHost(typeof(ClassLibrary1.HelloWorldService), httpUrl); //Add a service endpoint
host.AddServiceEndpoint(typeof(ClassLibrary1.IHelloWorldService), new WSHttpBinding(), ""); //Enable metadata exchange
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
host.Description.Behaviors.Add(smb); //Start the Service
host.Open(); Console.WriteLine("Service is host at " + DateTime.Now.ToString());
Console.WriteLine("Host is running... Press <Enter> key to stop");
Console.ReadLine();
}
From the client side, we will make five continuous calls simultaneously on the WCF service. Below is the code snippet for this:
Console.WriteLine("Enter Client name");
string str = Console.ReadLine();
ServiceReference1.HelloWorldServiceClient obj =
new ServiceReference1.HelloWorldServiceClient();
for(int i=;i<;i++)
{
obj.Call(str);
}
If you run the above project and monitor your WCF server service, you will see the screenshot. There are two important points to note:
- When you run using per call new instances are created every time the service runs. Instance 1 indicates new instances created for every method call.
- By default the concurrency is single so the same thread is used to service all five requests which are sent from the WCF client side. You can see the value of the thread is always 4.
- The most important factor is time. As concurrency is configured as single, it will be called one after another sequentially. You can notice this from the time difference of method calls of 5 seconds.

Let’s go and change the concurrency mode to multiple. In order to change the concurrency mode to multiple, we need to specify Multiple as the concurrency mode as shown in the below code snippet.

If you run the client now, you can see different threads (i.e., thread 4, 5, 6, 7, and 8) are spawned to serve the request and the time of the method calls are almost same. In other words, the methods are executed concurrently on different threads.
In short, you are now having a higher throughput in less amount of time.

9 combinations of Instancing and Concurrency
There are 9 combinations of concurrency and instancing as shown in the below table. In the sections below, we will discuss this in more detail.
| InstanceContext Mode |
ConcurrencyMode | ||
| Single (Default) | Multiple | Reentrant | |
| Single (Single instance for all clients) |
Single thread for all clients. | Multiple threads for all clients. | Single threads for all clients, locks are released when calls diverted to other WCF services. |
| PerSession (Default) (Multiple instance per client) |
Single thread for every client. | Multiple threads for every request. | Single threads for all clients, locks are released when calls diverted to other WCF services. |
| PerCall (Multiple instances for every method call) | Single thread for every client. | Single thread for every client | Single threads for all clients, locks are released when calls diverted to other WCF services. |
Instance mode = Per Call and Concurrency = Single[每一次请求,都创建一个服务实例。但是只允许使用一个线程来处理所有的请求]
With Per Call, new WCF instances are created for every method call made to the WCF server service. The default concurrency is Single so only one thread will be used to serve all instances.
Below is a simple pictorial representation of what will happen in Per Call and Single concurrency scenarios:
- For every client instance, a single thread will be allocated.
- For every method call, a new service instance will be created.
- A single thread will be used to serve all WCF instances generated from a single client instance.

If you refer the previous sample, you can see how threads are the same and the halt of 5 seconds on the WCF service.

instance的计数没有增加,说明每一次服务请求,都新建了一个服务实例
Instance mode = Per Call and Concurrency = Multiple[每一次请求,都创建一个服务实例。可以使用多个线程来处理请求]
[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerCall,
ConcurrencyMode = ConcurrencyMode.Multiple)]
public class HelloWorldService : IHelloWorldService
{
}
In this combination, multiple instances are created for every call but multiple threads serve every method call to a WCF service instance. You can see in the below figure we have two WCF service instances and every instance has multiple WCF service objects created for every method call. Every method call is handled by multiple threads.

In the above sample, if you remember, we have multiple threads serving every method call and the time difference between every method call is reduced considerably. The method calls are fired approximately at the same time.

Instance mode = Per Session and Concurrency = Single[每一次会话,创建一个服务实例。只允许用一个线程来处理所有的请求]
[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession,
ConcurrencyMode = ConcurrencyMode.Single)]
public class HelloWorldService : IHelloWorldService
{
}
In this combination, one WCF service instance is created for every WCF client session because the WCF instance mode is set to per session. All the method calls are executed in a sequential manner one by one. In other words, only one thread is available for all method calls for a particular service instance.
If you run the sample code with Per Session and Single combination mode, you will find the same thread executes all requests with the same WCF instance per session.

instance的计数在增加,说明每一次的服务请求,都是在同一个服务实例上处理的
Instance mode = Per Session and Concurrency = Multiple
[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession,
ConcurrencyMode = ConcurrencyMode.Multiple)]
public class HelloWorldService : IHelloWorldService
{
}
Instance mode ‘PerSession’ concurrency is the default setting in WCF
In this combination, one WCF instance is created for every WCF client session and every method call is run over multiple threads. Below is a pictorial representation:

If you run the sample code attached with this article, you will find the same instance with every method call running on different methods.

o get a better idea, you can run different client exe instances with different names as shown below.
You will notice how every client gets its own WCF service instance with every method allocated to run on different threads.

WCF Concurrency (Single, Multiple, and Reentrant) and Throttling的更多相关文章
- Learning WCF Chapter1 Exposing Multiple Service Endpoints
So far in this chapter,I have shown you different ways to create services,how to expose a service en ...
- salesforce lightning零基础学习(十三) 自定义Lookup组件(Single & Multiple)
上一篇简单的介绍了自定义的Lookup单选的组件,功能为通过引用组件Attribute传递相关的sObject Name,捕捉用户输入的信息,从而实现搜索的功能. 我们做项目的时候,可能要从多个表中获 ...
- Web service stop after running serveral hours
Error Message: 1. Error:Web service call "Test" execution failed 2. Error:<CENTER>&l ...
- WCF Throttling 限流的三道闸口
WCF Throttling 限流的三道闸口 一.WCF Throttling 流量限制简介 我们期望WCF服务端能够处理尽可能多的并发请求,但是资源是有限的,服务不可能同时处理无限多的并发请求,如 ...
- Service Oriented Architecture and WCF 【转】
http://www.codeproject.com/Articles/515253/Service-Oriented-Architecture-and-WCF Introduction This a ...
- 【WCF】WCF中的InstanceContext与ConcurrencyMode【转】
一.前言 最近忙于公司的在线升级项目,一个人要负责公司四大产品的在线升级,这四个产品是在Revit中以插件形式存在的,目前基于WCF来实现.等客户总量突破5万了,再重新用socket实现. 由于有服务 ...
- 【转】一个简单的WCF回调实例
代码下载:http://files.cnblogs.com/AlwinXu/CallbackService-master.zip 本文转自: http://adamprescott.net/2012/ ...
- WCF学习笔记之并发与限流
最近一直在学习WCF相关知识:本文章将针对并发与限流知识进行一个梳理,由于很多理论的知识所以做一个简单的记录,为今后回顾做一个记录: 一:并发知识 WCF将服务实例封装在一个称为实例上下文的对象中,并 ...
- WCF:并发处理
当多个线程同时访问相同的资源的时候就会产生并发,WCF缺省情况下会保护并发访问.对并发访问需要恰当处理,控制不好不仅会大大降低WCF服务的吞吐量和性能,而且还有可能会导致WCF服务的死锁.一.WCF并 ...
随机推荐
- android测试分析1
Android测试框架,开发环境中集成的一部分,提供一个架构和强有力的工具 可以帮助测试你的应用从单元到框架的每个方面. 测试框架有这些主要特征: 1.Android测试组件基于Junit.你可以使用 ...
- MVC小系列(十一)【Html.BeginForm与Ajax.BeginForm】
Html.BeginForm与Ajax.BeginForm都是mvc的表单元素,前者是普通的表单提交,而后者是支持异步的表单提交,直接用mvc自带的Ajax.BeginForm就可以很容易完成一个异步 ...
- VS2010 测试 -普通单元测试
http://www.cnblogs.com/rhythmK/archive/2012/04/20/2458832.html
- kettle中通过 时间戳(timestamp)方式 来实现数据库的增量同步操作(一)
这个实验主要思想是在创建数据库表的时候, 通过增加一个额外的字段,也就是时间戳字段, 例如在同步表 tt1 和表 tt2 的时候, 通过检查那个表是最新更新的,那个表就作为新表,而另外的表最为旧表被新 ...
- ORACLE 11g R2数据库安装硬件环境要求
物理内存要求:最小1G,在windows7,windows8,windows8.1上最小2G. 虚拟内存(或分页空间)容量要求: Available RAM Swap Space Required B ...
- iOS开发——短信验证码
作为iOS开发者,大家都应该知道ShareSDK这个比较出名的第三方分享(带统计功能)开发工具包. 他的官网今年年初发布了短信验证码的SDK.本文介绍它的短信验证码SDK.(部分过程参考官网如何集成的 ...
- Cordova5 -- iOS实战(一)
由于最近公司的项目要求用Cordova来进行开发,便开始了对Cordova的学习.由于本人之前也是做iOS开发,因此相关内容主要从iOS平台的角度来写.刚开始学习Cordova这个平台,希望以此总 ...
- C# 简单的图像边缘提取
博主做的很简单,大家看一看就好了...... 用到的算法是robert算子,这是一种比较简单的算法: f(x,y)=sqrt((g(x,y)-g(x+1,y+1))^2+(g(x+1,y)-g(x,y ...
- Java实战之01Struts2-05contextMAP、EL、OGNL
十五.contextMap 1.动作类的生命周期 明确:动作类是多例的,每次动作访问,动作类都会实例化.所以是线程安全的.与Struts1的区别是,struts1的动作类是单例的. 2.请求动作的数据 ...
- JamCam创业故事:辞掉工作,去开发一个应用
编者按:这是JamCam创始人的自述.这家初创公司提供的应用很简单,但是极为成功:有了JamCam,你所录制的视频会自动添加你正在iPhone中聆听的音乐,作为视频的背景音乐.和朋友分享时是不是方便多 ...