2

我可能在这里遗漏了一些东西,但根据我对 WCF 和使用 ChannelFactory 的理解,我相信我正确地创建、关闭和处理连接。

我创建了一个在我的 PC 上运行的简单服务来简单地返回一个字符串。然后我创建了一个客户端应用程序,通过创建通道、调用服务方法、关闭通道然后将其处理掉。通常,当循环到达 14,000 到 16,000 之间时,会发生错误,提示“无法对套接字执行操作,因为系统缺少足够的缓冲区空间或队列已满”。这让我认为我没有处理/清理我的连接,但从我读过的内容来看,我相信我做的一切都是正确的。我也尝试过在每次迭代中处理和创建一个新的通道工厂,这有同样的问题(并且明显更慢)。

任何帮助将不胜感激。谢谢。

// Interface for the service
[ServiceContract]
public interface ITestService
{
    [OperationContract, XmlSerializerFormat]
    string HelloWorld(int counter);
}

//Implementation of the service
public class TestService : ITestService
{
    public string HelloWorld(int counter)
    {
        return string.Format("{0,6:0}: Hello New World - {1}", counter, Guid.NewGuid().ToString());
    }
}

// Console Application
static void Main(string[] args)
    {
        int count = Convert.ToInt32(args[0]);
        System.Console.WriteLine("Generating " + args[0] + " calls");
        using (ChannelFactory<ITestService> sharedChannelFactory = new ChannelFactory<ITestService>("BasicHttpBinding_ITestService"))
        {
            for (int i = 0; i < count; i++)
            {
                ITestService svc = null;

                svc = sharedChannelFactory.CreateChannel();
                string response = svc.HelloWorld(i);
                ((IClientChannel)svc).Close();
                ((IClientChannel)svc).Dispose();
                System.Console.WriteLine(string.Format("{0}", response));
            }
        }
    }
4

0 回答 0