3

I'm experimenting with making async WCF calls. I'm looking at making multiple calls to the same service.

From the client I create a new channel for each call, make the call (giving it a callback method), and then in the callback I close the channel.

In the service I have added a call to thread.sleep to simulate the service doing some work.

The first 20 or so calls complete ok (this number varies each time). So after what seems a random number of calls, I receive this exception on the client:

Could not connect to net.tcp://localhost:61501/Calulator. The connection attempt lasted for a time span of 00:00:02.9332933. TCP error code 10061: No connection could be made because the target machine actively refused it 127.0.0.1:61501.

So I have a couple of questions:

  1. Am I right to be opening a new channel for each call?
  2. What is causing this exception?

Many thanks in advance for any help.

My code is as follows, and can also be found here: https://subversion.assembla.com/svn/agilenet/tags/WcfStackOverflow/Wcf

Service:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace Service
{
    class Program
    {
        static void Main(string[] args)
        {
            NetTcpBinding netBinding = new NetTcpBinding();

            ServiceHost host = null;
            host = new ServiceHost(
                    typeof(Calculator),
                    new Uri("net.tcp://localhost:61501/Calulator"));
            host.AddServiceEndpoint(
                    typeof(ICalculator),
                    netBinding,
                    string.Empty);
            host.Open();

            Console.ReadLine();
        }
    }

    [ServiceContract]
    public interface ICalculator
    {
        [OperationContract]
        int Add(int value1, int value2);

        [OperationContract(AsyncPattern = true)]
        IAsyncResult BeginAdd(int value1, int value2, AsyncCallback callback, object state);

        int EndAdd(IAsyncResult result);
    }

    public class Calculator : ICalculator
    {
        public int Add(int value1, int value2)
        {
            Console.WriteLine(
                "Incoming Add request {0}, {1}",
                value1.ToString(),
                value2.ToString());
            System.Threading.Thread.Sleep(500);
            return value1 + value2;
        }

        public IAsyncResult BeginAdd(int value1, int value2, AsyncCallback callback, object state)
        {
            throw new NotImplementedException();
        }

        public int EndAdd(IAsyncResult result)
        {
            throw new NotImplementedException();
        }

    }
}

Client:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using Service;

namespace Client
{
    class Program
    {
        static void Main(string[] args)
        {

            NetTcpBinding netBinding = new NetTcpBinding();
            ChannelFactory<ICalculator> factory = new ChannelFactory<ICalculator>(
                netBinding,
                "net.tcp://localhost:61501/Calulator");

            for (int i = 0; i < 100; i++)
            {
                ICalculator service = factory.CreateChannel();
                service.BeginAdd(i, 0, SaveCallback, service);
            }

            Console.ReadLine();
        }

        static void SaveCallback(IAsyncResult ar)
        {
            ICalculator service = (ICalculator)ar.AsyncState;
            Console.WriteLine(service.EndAdd(ar).ToString());
            ((IContextChannel)service).Close();

        }

    }

}
0

1 Answer 1

1

I think that you have encountered windows (7/Vista/XP) tcp connection limit. These OS:s limit the number of (inbound) tcp connections to 20. Googling "windows tcp connection limit" gives you a lot of more information about the subject.

SuperUser has also a thread about this subject: https://superuser.com/questions/253141/inbound-tcp-connection-limit-in-windows-7. More information also in ServerFault (linked from SU).

So to answer your question:

  1. No, you should not open new channel for each call.
  2. You hit the limit of non-server version of windows (probably 7 or Vista because you can get 20 connections). You get random number of successful connections because some times the OS is quick enough to clean few first connections, but the request rate is so high, that you are going to hit the maximum eventually.
2
  • Thanks, it makes a lot of sense what you're saying. I'll look into it some more based on your comments and let you know how I get on.
    – P2l
    Commented Jun 14, 2011 at 11:15
  • Yes that does appear to be the reason. Using WCF it's easy to forget what's going on under the hood, and using TcpView it's possible to see that a large number of tcp connections are simultaneously open. Using a single channel for all calls resolves the issue (although I then find that I start getting into the area of WCF timeouts due to keeping the channel open, but I can look at increasing the timeout).
    – P2l
    Commented Jun 15, 2011 at 9:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.