We have Windows Service which reads messages from MSMQ and process it one by one. To improve performance / throughput we moved to TPL. We have couple of database call and WCF call while processing messages. We observed that, while doing WCF call first couple of calls are faster but subsequent call are taking longer time. Here is the sample code.
public class TestWcf
{
public void Process(string option)
{
// container for perellel tasks and WaitAll after 50 count
TaskContainer batch = new TaskContainer(50, true);
for (int i = 1; i <= 100; i++)
{
if(option.Equals("s"))
{
batch.Add(ProcessMessage);
}
else
{
batch.Add(ProcessMessageAsync);
}
}
}
public void ProcessMessage()
{
Stopwatch sw = Stopwatch.StartNew();
PostingServiceClient client = new PostingServiceClient();
client.UpdateArchiveStatus(Guid.Parse("B6D5C77C-330A-4B00-96BF-E91A2B5970E3"),"1234567890");
sw.Stop();
Console.WriteLine("Thread {0}, Elapsed Time: {1} s", Thread.CurrentThread.ManagedThreadId, sw.Elapsed.Seconds);
//client.Close();
}
public void ProcessMessageAsync()
{
Stopwatch sw = Stopwatch.StartNew();
PostingServiceClient client = new PostingServiceClient();
var task = Task.Factory.FromAsync(
client.BeginUpdateArchiveStatus(Guid.Parse("B6D5C77C-330A-4B00-96BF-E91A2B5970E3"), "1234567890", null,
null), client.EndUpdateArchiveStatus);
task.Wait();
sw.Stop();
Console.WriteLine("Thread {0}, Elapsed Time: {1} s", Thread.CurrentThread.ManagedThreadId, sw.Elapsed.Seconds);
//client.Close();
}
}
We are using external team WCF and it's hosted in IIS Server, App Pool targeting .Net 2.0. WCF Service side, serviceThrottling is 500 and there are NO ServiceBehavior attribute define on Service Contract. We ran above program same time with two console instance one with Sync option and second with Async option. Sync option completed much faster then Async option. My assumption, system is not releasing task from ThreadPool while doing async operation. Question is, which approach is better (Sync vs. Async WCF call) or is there any different approach to deal with WCF call in this scenario.