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:
- Am I right to be opening a new channel for each call?
- 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.ToStrin开发者_高级运维g(),
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();
}
}
}
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:
- No, you should not open new channel for each call.
- 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.
精彩评论