Concurrence Mode Instance using WCF
Concurrence Mode Instance specifies a service class supports single/multi-threaded modes of operation or not.
WCF follows an asynchronous messaging. It makes extensive use of asynchronous I/O, and as a result, each received message may be dispatched to a receiving object by different threads. Because of this feature allows WCF to use the CPU efficiently, and as a result, allows WCF applications to scale.
Below given example make clear the concurrence mode instance, how service class works with ConcurrencyMode instance property on the ServiceBehaviorAttribute type with respect to the client requests.
Types of modes:
Single: only one thread will access the incoming object at a time
Multiple: multiple threads will access the incoming object concurrently
Reentrant: only one thread will access the incoming object at a time, but callbacks will re-enter that object on another thread
The service class specifies ConcurrencyMode as
// Single mode will use the same instance for all the clients concurrently
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple,InstanceContextMode = InstanceContextMode.Single)]
// PerCall will use the same instance for all the clients concurrently
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, InstanceContextMode = InstanceContextMode.PerCall)]
// Multiple mode will use the multiple instance for the clients concurrently
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, InstanceContextMode = InstanceContextMode.Single)]
//Reentrant mode will use to call other method of another object (if required), which is not allowed in Single
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Reentrant, InstanceContextMode = InstanceContextMode.Single)]
The service class contains the following
1. Constructor, which is useful to create new GUID for every instance of service class
2. getInfo(), returns message to the client
3. getInfo2(), returns message to the client
Implementation Steps follows as below
1) Create new WCF Service Library Project with the name WCFLib
2) Rename Iservice.cs to IServicedClass.cs from Solution Explorer
3) Rename Service.cs to ServicedClass.cs from Solution Explorer
4) Add Reference to System.ServiceModel from Solution Explorer
5) Write the following code in IServicedClass.cs
using System.ServiceModel;
namespace WCFLib
{
[ServiceContract]
public interface IServicedClass
{
[OperationContract]
string getInfo();
[OperationContract]
string getInfo2();
}
}
6) Write the following code in ServicedClass.cs
using System.ServiceModel;
using System.Threading;
namespace WCFLib
{
/* Concurrency Start */
// Single mode will use the same instance for all the clients concurrently
//to see run 2 client programs
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple,
InstanceContextMode = InstanceContextMode.Single)]
/* Concurrency End */
public class ServicedClass:IServicedClass
{
private Guid id;
public ServicedClass()
{
id = Guid.NewGuid();
}
public string getInfo()
{
Thread.Sleep(3000);
return “*** getInfo called on Object id : “ + id.ToString() + “***”;
}
[OperationBehavior]
public string getInfo2()
{
return “*** getInfo2 called on Object id : “ + id.ToString() + “***”;
}
}
}
Note : Rebuild the service (WCFLib project) after changing the ServiceBehavior for concurrence instance.
7) Build only WCFLib project
Add new Console Application with the name WCFSerCon in the solution
9) Add Reference to System.ServiceModel
10) Add Reference to WCFLib.dll
11) Write the following code in Program.cs
using System.ServiceModel;
using WCFLib;
namespace WCFSerCon
{
class Program
{
static void Main(string[] args)
{
ServiceHost host = new ServiceHost(typeof(ServicedClass));
string address = “net.tcp://localhost:1099/Myobject”;
NetTcpBinding binding = new NetTcpBinding();
host.AddServiceEndpoint(typeof(IServicedClass), binding, address);
host.Open();
Console.WriteLine(“Server is Read and Running…”);
Console.ReadLine();
}
}
}
12) Build only WCFSerCon project
13) Add new Console Application with the name WCFClientCon in the solution
14) Add Reference to System.ServiceModel
15) Add Reference to WCFLib.dll
16) Write the following code in Program.cs
using System.Threading;
using System.ServiceModel;
using WCFLib;
namespace WCFClientCon
{
class Program
{
static void Main(string[] args)
{
string address = “net.tcp://localhost:1099/Myobject”;
NetTcpBinding binding = new NetTcpBinding();
ChannelFactory<IServicedClass> cf = new ChannelFactory<IServicedClass>(binding, address);
IServicedClass obj = cf.CreateChannel();
//Concurrancy example
for (int i = 0; i < 10; i++)
{
Console.Write(DateTime.Now.ToLongTimeString());
Console.WriteLine(obj.getInfo());
Thread.Sleep(1000);
Console.WriteLine(obj.getInfo2());
}
Console.WriteLine();
Console.WriteLine(“Press any key…”);
Console.ReadLine();
}
}
}
17) Build only WCFClientCon project
18) Set WCFSerCon as start up project and Run using ctrl+F5
19) Now server is ready and you can find the following screen
20) Set WCFClientCon as start up project and Run using ctrl+F5
21) Now client application is executing and you can find the following output

WCF Concurrence- Single Output The service class specifies ConcurrencyMode as [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple,InstanceContextMode = InstanceContextMode.Single)
4 comments
4 Comments so far
Leave a reply


Yes I agree with you!
Hello, good site
Cool staff
Realy good!