There is a simple WCF service in the console application and the same client. The client simultaneously (in different threads) sends N requests to the server. It is expected that the server for processing concurrent requests simultaneously allocates several threads and after a second downtime (specially made by Thread.Sleep (1000)) simultaneously returns the answers to the client, but this does not happen. The service processes all requests in one thread (this is seen in ThreadId on the screenshot), although the ServiceBehavior attribute is set to ConcurrencyMode.Multiple.
Service app code:
namespace Server
{
using System;
using System.ServiceModel;
using System.Threading;
[ServiceContract]
public interface IServer
{
[OperationContract]
int GetResult(int value);
}
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple,
InstanceContextMode = InstanceContextMode.Single)]
public class Server: IServer
{
public int GetResult(int value)
{
Console.WriteLine("In: {0}, Time: {1}, ThreadId: {2}",
value, DateTime.Now.TimeOfDay, Thread.CurrentThread.ManagedThreadId);
Thread.Sleep(1000);
Console.WriteLine("Out: {0}, Time: {1}, ThreadId: {2}",
value, DateTime.Now.TimeOfDay, Thread.CurrentThread.ManagedThreadId);
return value;
}
}
class Program
{
static void Main()
{
var host = new ServiceHost(new Server());
host.Open();
Console.WriteLine("Service started");
Console.ReadLine();
}
}
}
Client app code:
using System;
namespace Server
{
using System.ServiceModel;
[ServiceContract]
public interface IServer
{
[OperationContract]
int GetResult(int value);
}
}
namespace Client
{
using System.ServiceModel;
using System.Threading;
using Server;
class Program
{
static object lockObj = new object();
static void Main()
{
ChannelFactory<IServer> factory = new ChannelFactory<IServer>("defaultEndPoint");
IServer channel = factory.CreateChannel();
const int threadCount = 6;
int value = 0;
for (int i = 0; i < threadCount; i++)
{
new Thread(state =>
{
int n;
lock (lockObj)
{
value++;
n = value;
}
Console.WriteLine("Send value = {0}, Time = {1}", n, DateTime.Now.TimeOfDay);
n = channel.GetResult(n);
Console.WriteLine("Response value = {0}, Time = {1}", n, DateTime.Now.TimeOfDay);
}).Start();
}
Console.ReadLine();
}
}
}
Service config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="DefaultBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceThrottling maxConcurrentCalls="1000" maxConcurrentInstances="1000" maxConcurrentSessions="1000"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="Server.Server" behaviorConfiguration="DefaultBehavior">
<endpoint contract="Server.IServer" binding="basicHttpBinding" address=""/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
<host>
<baseAddresses>
<add baseAddress="http://localhost:7803/"/>
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/></startup></configuration>
Console output on client side:
Send value = 1, Time = 11:20:13.1335555
Send value = 3, Time = 11:20:13.1335555
Send value = 2, Time = 11:20:13.1335555
Send value = 4, Time = 11:20:13.1335555
Send value = 5, Time = 11:20:13.1335555
Send value = 6, Time = 11:20:13.1335555
Response value = 3, Time = 11:20:14.6191583
Response value = 1, Time = 11:20:15.6184362
Response value = 2, Time = 11:20:16.6342291
Response value = 4, Time = 11:20:17.6497805
Response value = 5, Time = 11:20:18.6657260
Response value = 6, Time = 11:20:19.6820159
Console output on service side:
In: 3, Time: 11:20:13.5030783, ThreadId: 12
Out: 3, Time: 11:20:14.5184547, ThreadId: 12
In: 1, Time: 11:20:14.6035310, ThreadId: 12
Out: 1, Time: 11:20:15.6184362, ThreadId: 12
In: 2, Time: 11:20:15.6184362, ThreadId: 12
Out: 2, Time: 11:20:16.6342291, ThreadId: 12
In: 4, Time: 11:20:16.6342291, ThreadId: 12
Out: 4, Time: 11:20:17.6497805, ThreadId: 12
In: 5, Time: 11:20:17.6497805, ThreadId: 12
Out: 5, Time: 11:20:18.6657260, ThreadId: 12
In: 6, Time: 11:20:18.6657260, ThreadId: 12
Out: 6, Time: 11:20:19.6820159, ThreadId: 12