WCF - create a new channel for each call - why does this code not work?

I am experimenting with async WCF calls. I am looking for multiple calls to the same service.

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

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

The first 20 or so calls end normally (this number changes each time). So after what seems like a random number of calls, I get this exception on the client:

Failed to connect to net.tcp: // localhost: 61501 / Calulator. The connection attempt continued for a period of 00: 00: 02.9332933. TCP error code 10061: The connection could not be completed because the target computer actively refused it 127.0.0.1:61501.

So, I have a couple of questions:

  • Am I right, open a new channel for every call?
  • What causes this exception?

Thanks so much for any help.

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

Services:

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();
        }

    }
}

Customer:

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();

        }

    }

}
+3
source share
1 answer

, tcp windows (7/Vista/XP). : s () tcp 20. Googling " Windows tcp" .

SuperUser : https://superuser.com/questions/253141/inbound-tcp-connection-limit-in-windows-7. ServerFault ( SU).

, :

  • , .
  • Windows (, 7 Vista, 20 ). , , , .
+1

All Articles