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