Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF: EndpointNotFoundException after running for a couple of seconds

Tags:

c#

wcf

I'm working with two applications, one has a self hosted service configured to use the net.tcp binding. The ServiceBehaviorAttribute of the service is configured with:

[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple,
                 InstanceContextMode = InstanceContextMode.Single,
                 IncludeExceptionDetailInFaults = true,
                 UseSynchronizationContext = false,
                 ValidateMustUnderstand = false)]

For both, service and client the transferMode is set to Streamed, and the timeouts are:

closeTimeout="00:01:00"
openTimeout="00:00:30"
receiveTimeout="00:02:30"
sendTimeout="00:02:30"

MaxConnections is set to 500 and the ServiceThrottlingBehavior uses the WCF 4 default values:

  • MaxConcurrentSessions: 100 * ProcessorCount
  • MaxConcurrentCalls: 16 * ProcessorCount
  • MaxConcurrentInstances: default is the total of the above two, which follows the same pattern as before.

I'm using a quad-core machine and the Net.Tcp Port Sharing service is enabled.

The client application has a single channel to the service created using the ChannelFactory class. Once the channel is created 100 threads are spawned. Each thread uses the channel to send messages to the server with a frequency of one message per second.

After a couple of seconds running ok (the client sends messages to the server an it receives them correctly) an EndpointNotFoundException is thrown with the following message:

Could not connect to net.tcp://localhost/service. The connection attempt lasted 
for a time span of 00:00:02.1777100. TCP error code 10061: No connection could 
be made because the target machine actively refused it 127.0.0.1:808.

The weird things are:

  • If I run both applications in the same machine the time span of the exception is about 2 seconds, but if I run the server application in my machine and the client application in a different machine the time span of the exception is always 1 second.
  • Some times (like one in ten) the exception doesn't throw and both applications works fine.
  • Before the exception is thrown the server receives messages and processes them correctly. No exceptions are thrown in the server.

I made a lot of tests, reducing the amount of threads, increasing it, changing the close, open, receive and send timeouts to lower and higher values, setting a higher value for maxConnections but the result is always the same, at some point the EndpointNotFoundException is thrown. I'm about to give up and change the code so each thread has its own channel hoping this fixes the issue, but I want to know why this happens. If someone knows what I'm doing wrong or can point me in the right direction to keep investigating it will be helpful.

like image 828
Diego Avatar asked Apr 12 '12 14:04

Diego


1 Answers

By default Windows doesn't enable port sharing. I would check that you have it properly enabled (see here).

If possible, you could also try changing the port of one application, or do testing of one within a VM.

Also, for anyone else that may have the same issue, do as Diego has done and check that port sharing is enabled in the configuration. Add portSharingEnabled="true" to the binding:

<system.serviceModel>
  <bindings>
    <netTcpBinding name="portSharingBinding" 
                   portSharingEnabled="true" />
  <services>
    <service name="MyService">
        <endpoint address="net.tcp://localhost/MyService"
                  binding="netTcpBinding"
                  contract="IMyService"
                  bindingConfiguration="portSharingBinding" />
    </service>
  </services>
</system.serviceModel>

Taken from: http://msdn.microsoft.com/en-us/library/ms731810.aspx

like image 196
Jim Schubert Avatar answered Oct 23 '22 00:10

Jim Schubert