我正在处理两个应用程序,其中一个已经配置了自托管服务来使用net.tcp绑定。服务的ServiceBehaviorAttribute已经进行了配置:
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple,
InstanceContextMode = InstanceContextMode.Single,
IncludeExceptionDetailInFaults = true,
UseSynchronizationContext = false,
ValidateMustUnderstand = false)]
对于服务端和客户端,transferMode都被设置为Streamed,并且超时时间为:
closeTimeout="00:01:00"
openTimeout="00:00:30"
receiveTimeout="00:02:30"
sendTimeout="00:02:30"
MaxConnections被设置为500,ServiceThrottlingBehavior使用WCF 4的默认值:
- MaxConcurrentSessions:100 * ProcessorCount
- MaxConcurrentCalls:16 * ProcessorCount
- MaxConcurrentInstances:默认值是上述两个的总和,与以前相同。
我正在使用四核机器,并启用了Net.Tcp端口共享服务。
客户端应用程序使用ChannelFactory类创建一个单一通道到服务。一旦通道被创建,就会生成100个线程。每个线程使用通道以每秒一个消息的频率向服务器发送消息。
在正常运行了几秒钟后(客户端将消息发送到服务器并正确接收它们),抛出EndpointNotFoundException异常,其中包含以下消息:
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.
奇怪的事情是:
- 如果我在同一台机器上运行两个应用程序,异常的时间跨度大约为2秒,但是如果我在我的机器上运行服务器应用程序并在不同的机器上运行客户端应用程序,异常的时间跨度始终为1秒。
- 有时(如十次中有一次),异常不会被抛出,两个应用程序都正常工作。
- 在抛出异常之前,服务器接收消息并正确处理它们。服务器没有抛出任何异常。