Fabio Marreco
Fabio Marreco

Reputation: 2303

Asynchronous call on WCF netTcpBinding

I am having a problem executing calls to a WCF Service asynchronously when using netTcpBinding.

For instance. I have a simple service with the following method:

 public class Service1 : IService1
    {
        public string Calculate(int value)
        {
            Thread.Sleep(1000);
            return value.ToString();
        }
    }

On the client side, I am testing it by calling the method twice asynchronously, and checking the total time. The total time should be > 1sec and < 2sec

static void Main(string[] args)
{
    var svc = new Service1Client();

    var dt = DateTime.Now;
    Console.WriteLine("Calculating 10..");
    var tsk1 = svc.CalculateAsync(10).ContinueWith(c=> Console.WriteLine ("Result of 10:.."  + c.Result.ToString()));

    Console.WriteLine("Calculating 20..");
    var tsk2 = svc.CalculateAsync(20).ContinueWith(c => Console.WriteLine("Result of 20:.." + c.Result.ToString()));

    Task.WaitAll(tsk1, tsk2);
    Console.WriteLine("Total Time: " + (DateTime.Now - dt).TotalMilliseconds.ToString());
}

Ok. when I test it using basicHttpBinding, it all goes perfectly:

<endpoint address="" binding="basicHttpBinding" name="svcEndpoint" contract="ServiceTest.IService1"/>

Result using basicHttpBinding

But when using netTcpBinding, the client waits for the result of the first call to execute the second one, making it synchronous

<endpoint address="" binding="netTcpBinding" name="svcEndpoint" contract="ServiceTest.IService1"/>

enter image description here

Has anybody ever had a problem like this ? Is it a limitation of the netTcp ? Is there a workarround ?

Upvotes: 2

Views: 725

Answers (1)

Fabio Marreco
Fabio Marreco

Reputation: 2303

Finally got it!

I needed to explicitly tell the service to be concurrent throught the attribute ServiceBehaviour, like this:

[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]
 public class Service1 : IService1
    {
        public string Calculate(int value)
        {
            Thread.Sleep(1000);
            return value.ToString();
        }
    }

Now it is working fine.

Upvotes: 1

Related Questions