WCF-为每个呼叫创建新频道-为什么此代码失败?

问题描述

| 我正在尝试进行异步WCF调用。我正在考虑对同一服务进行多次呼叫。 从客户端,我为每个调用创建一个新通道,进行调用(为其提供回调方法),然后在回调中关闭通道。 在服务中,我添加了对thread.sleep的调用,以模拟服务进行的一些工作。 前20个左右的呼叫完成就可以了(此数字每次都不同)。因此,在看似随机的呼叫次数之后,我在客户端收到此异常: 无法连接到net.tcp:// localhost:61501 / Calulator。连接尝试持续时间为00:00:02.9332933。 TCP错误代码10061:无法建立连接,因为目标计算机主动拒绝它127.0.0.1:61501。 所以我有几个问题: 我打开一个新的书对吗? 每个通话的渠道? 是什么导致此异常? 非常感谢您的任何帮助。 我的代码如下,也可以在这里找到: https://subversion.assembla.com/svn/agilenet/tags/WcfStackOverflow/Wcf 服务:
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,object state)
        {
            throw new NotImplementedException();
        }

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

    }
}
客户:
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,SaveCallback,service);
            }

            Console.ReadLine();
        }

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

        }

    }

}
    

解决方法

        我认为您遇到了Windows(7 / Vista / XP)TCP连接限制。这些操作系统将(入站)tcp连接的数量限制为20。使用谷歌搜索“ Windows tcp connection limit”可以为您提供有关该主题的更多信息。 超级用户还有一个与此主题相关的主题:https://superuser.com/questions/253141/inbound-tcp-connection-limit-in-windows-7。在ServerFault中也有更多信息(从SU链接)。 因此,回答您的问题: 不,您不应为每个电话打开新频道。 您达到Windows的非服务器版本的限制(可能是7或Vista,因为您可以获得20个连接)。您获得的随机成功连接数是随机的,因为有时操作系统足够快地清理少量的第一个连接,但是请求速率如此之高,以至于最终您将达到最大值。