I'm working on a wcf application and I'm started to see an strange timeouts behavior on it. I created a foo application with the same configuration as the real one in order to determine if the problem is wcf related or not.
The client application code is this:
public class Clients
{
public static void Main(string[] args)
{
var random = new Random();
var run = true;
var threads = 100;
for (int i = 0; i < threads; i++)
{
int delay = random.Next(1000, 1500);
var t = new Thread(() => {
var id = Guid.NewGuid().ToString("N").Substring(0, 10);
var client = new Client(@"client");
while (run)
{
Console.WriteLine(@"Sending thread id " + id);
client.Operation(@"Thread " + id);
Thread.Sleep(delay);
}
});
t.Start();
}
}
}
The server application code only logs the data sent to the console:
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple,
InstanceContextMode = InstanceContextMode.Single,
IncludeExceptionDetailInFaults = true,
UseSynchronizationContext = false,
ValidateMustUnderstand = false)]
public class Service : IService
{
public void Operation(string data)
{
Console.WriteLine(@"Data received: {0}", data);
}
}
And this is the app.config file:
<?xml version="1.0"?>
<configuration>
<system.serviceModel>
<services>
<service name="Test.Service" behaviorConfiguration="throttle">
<endpoint name="service"
address="net.tcp://localhost/service"
binding="netTcpBinding"
bindingConfiguration="NetTcpBinding"
contract="Test.IService"/>
</service>
</services>
<client>
<endpoint name="client"
address="net.tcp://localhost/service"
binding="netTcpBinding"
bindingConfiguration="NetTcpBinding"
contract="Test.IService"/>
</client>
<bindings>
<netTcpBinding>
<binding name="NetTcpBinding"
portSharingEnabled="true"
closeTimeout="00:01:00"
openTimeout="00:00:30"
receiveTimeout="00:00:10"
sendTimeout="00:00:10"
transactionFlow="false"
transferMode="Streamed"
transactionProtocol="OleTransactions"
hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="6553600"
maxBufferSize="6553600"
maxConnections="500"
maxReceivedMessageSize="6553600">
<readerQuotas maxDepth="128"
maxStringContentLength="3276800"
maxArrayLength="6553600"
maxBytesPerRead="1638400"
maxNameTableCharCount="6553600"/>
<security mode="None">
<transport protectionLevel="None"/>
</security>
</binding>
</netTcpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="throttle">
<serviceThrottling maxConcurrentCalls="1000"
maxConcurrentInstances="10"
maxConcurrentSessions="500"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
If a run both applications and the number of threads of the Client application is 100 everything is ok. But if I change the number of threads to 300 I get the following exception:
System.ServiceModel.CommunicationException: The socket connection was aborted.
This could be caused by an error processing your message or a receive timeout
being exceeded by the remote host, or an underlying network resource issue.
Local socket timeout was '00:00:10'.---> System.Net.Sockets.SocketException:
An existing connection was forcibly closed by the remote host at
System.ServiceModel.Channels.SocketConnection.ReadCore(Byte[] buffer, Int32
offset, Int32 size, TimeSpan timeout, Boolean closing)
I'm using port sharing on a quad-core machine and the service is self hosted.
This is the same problem I have in the real application but with a higher timeout (2 minutes and a half). I can't believe that 300 is the maximum number of connections a service with InstanceContextMode.Single can handle. I read about problems with the default ChannelInitializationTimeout value (which is set to five) but I don't think that's the problem (in another test I started creating and opening a single client for all the threads, and waited for 6 seconds to start all the threads, the result was the same exception after 10 seconds). Perhaps I'm configuring something in a wrong way.
If someone has a clue or ideas they will be helpful. Thanks in advance.
Update
I changed the binding from netTcp to basicHttpBinding and now everything works fine (no CommunicationExceptions thrown). Also I started to get messages in the service faster than before, does anyone know why I'm getting this improvement with basicHttpBinding?