I have a WCF sample with ConcurrencyMode = Multiple
and the binding mode is WSHttpBinding
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, InstanceContextMode = InstanceContextMode.PerSession)]
public class StudentService : IStudentService
{
public string GetStudentInfo(int studentId)
{
Console.WriteLine(string.Format("{0} - [GetStudentInfo] Receive request, thread id {1}", DateTime.Now, System.Environment.CurrentManagedThreadId));
Thread.Sleep(5000); // For testing, this sleeping is used to make this function runs longer
string studentName = string.Empty;
switch (studentId)
{
case 1:
{
studentName = "Johnson";
break;
}
case 2:
{
studentName = "Henry";
break;
}
default:
{
studentName = "No student found";
break;
}
}
Console.WriteLine(string.Format("{0} - [GetStudentInfo] Leave request, thread id {1}", DateTime.Now, System.Environment.CurrentManagedThreadId));
return studentName;
}
}
I would like to send multiple requests to this service by using threading, but based on the log, it received requests sequentially
This is my client:
public static T CreateChannelFactory<T>()
{
try
{
// Create channel to connect to service
ChannelFactory<T> myChannelFactory = new ChannelFactory<T>(new WSHttpBinding());
myChannelFactory.Endpoint.Address = new EndpointAddress("http://localhost:9091/StudentService");
T channel = myChannelFactory.CreateChannel();
return channel;
}
catch (Exception ex)
{
throw ex;
}
}
static void Main(string[] args)
{
StudentService.IStudentService channel = CreateChannelFactory<StudentService.IStudentService>();
Thread[] arr = new Thread[10];
for (int i = 0; i < arr.Length; i++)
{
arr[i] = new Thread(new ThreadStart(() => channel.GetStudentInfo(1)));
}
for (int i = 0; i < arr.Length; i++)
{
arr[i].Start();
}
for (int i = 0; i < arr.Length; i++)
{
arr[i].Join();
}
Console.ReadKey();
}
Based on the log, the service received requests sequentially
-> Receive then Leave, then Receive then Leave,..., and then Receive then Leave enter image description here
But, when I tried sending the first request before sending other requests
static void Main(string[] args)
{
StudentService.IStudentService channel = CreateChannelFactory<StudentService.IStudentService>();
channel.GetStudentInfo(1); // THE FIRST REQUEST HERE
Thread[] arr = new Thread[10];
for (int i = 0; i < arr.Length; i++)
{
arr[i] = new Thread(new ThreadStart(() => channel.GetStudentInfo(1)));
}
for (int i = 0; i < arr.Length; i++)
{
arr[i].Start();
}
for (int i = 0; i < arr.Length; i++)
{
arr[i].Join();
}
Console.ReadKey();
}
And the service can receive multiple requests
-> Many Receive at the same time
The service ConcurrencyMode = Multiple
, but why do I need to send the first request? Any reason for that?
Thank you
******** Host project:
static void Main(string[] args)
{
ServiceHost studentServiceHost = null;
try
{
//Base Address for StudentService
Uri httpBaseAddress = new Uri("http://localhost:9091/StudentService");
//Instantiate ServiceHost
studentServiceHost = new ServiceHost(typeof(StudentService.StudentService),
httpBaseAddress);
//Add Endpoint to Host
studentServiceHost.AddServiceEndpoint(typeof(StudentService.IStudentService),
new WSHttpBinding(), "");
//Metadata Exchange
ServiceMetadataBehavior serviceBehavior = new ServiceMetadataBehavior();
serviceBehavior.HttpGetEnabled = true;
studentServiceHost.Description.Behaviors.Add(serviceBehavior);
//Open
studentServiceHost.Open();
Console.WriteLine("Service is live now at : {0}", httpBaseAddress);
Console.ReadKey();
studentServiceHost.Close();
}
catch (Exception ex)
{
studentServiceHost = null;
Console.WriteLine("There is an issue with StudentService:" + ex.Message);
}
}
******** Channel Factory create:
public static T CreateChannelFactory<T>()
{
try
{
// Create a channel to connect to the service
ChannelFactory<T> myChannelFactory = new ChannelFactory<T>(new WSHttpBinding());
myChannelFactory.Endpoint.Address = new EndpointAddress("http://localhost:9091/StudentService");
T channel = myChannelFactory.CreateChannel();
return channel;
}
catch (Exception ex)
{
throw ex;
}
}