运行几秒后WCF:EndpointNotFoundException

Die*_*ego 6 c# wcf

我正在使用两个应用程序,一个具有配置为使用net.tcp绑定的自托管服务.该服务的ServiceBehaviorAttribute配置为:

[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple,
                 InstanceContextMode = InstanceContextMode.Single,
                 IncludeExceptionDetailInFaults = true,
                 UseSynchronizationContext = false,
                 ValidateMustUnderstand = false)]
Run Code Online (Sandbox Code Playgroud)

对于服务和客户端,transferMode设置为Streamed,超时为:

closeTimeout="00:01:00"
openTimeout="00:00:30"
receiveTimeout="00:02:30"
sendTimeout="00:02:30"
Run Code Online (Sandbox Code Playgroud)

MaxConnections设置为500,ServiceThrottlingBehavior使用WCF 4默认值:

  • MaxConcurrentSessions:100*ProcessorCount
  • MaxConcurrentCalls:16*ProcessorCount
  • MaxConcurrentInstances:默认值是以上两者的总和,它遵循与之前相同的模式.

我使用的是四核机器,并启用了Net.Tcp端口共享服务.

客户端应用程序具有使用ChannelFactory类创建的服务的单个通道.创建通道后,会生成100个线程.每个线程使用该通道以每秒一条消息的频率向服务器发送消息.

运行ok几秒钟后(客户端向服务器发送消息并正确接收它们)会抛出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.
Run Code Online (Sandbox Code Playgroud)

奇怪的事情是:

  • 如果我在同一台机器上运行这两个应用程序,则异常的时间跨度大约为2秒,但如果我在我的机器中运行服务器应用程序而在另一台机器中运行客户端应用程序,则异常的时间跨度始终为1秒.
  • 有时候(比如十分之一)异常不会抛出,两个应用程序都能正常工作.
  • 在抛出异常之前,服务器会接收消息并正确处理它们.服务器中不会​​抛出任何异常.

我做了很多测试,减少线程数量,增加线程数量,更改关闭,打开,接收和发送超时值到更低和更高的值,为maxConnections设置更高的值但结果总是相同的,在某些时候抛出EndpointNotFoundException.我即将放弃并更改代码,以便每个线程都有自己的通道,希望这可以解决问题,但我想知道为什么会发生这种情况.如果有人知道我做错了什么或者能指出我正确的方向继续调查它会有所帮助.

Jim*_*ert 1

默认情况下,Windows 不启用端口共享。我会检查您是否已正确启用它(请参阅此处)。

如果可能,您还可以尝试更改一个应用程序的端口,或者在虚拟机中测试一个应用程序。

另外,对于可能遇到相同问题的其他人,请按照迭戈所做的操作并检查配置中是否启用了端口共享。添加portSharingEnabled="true"到绑定:

<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>
Run Code Online (Sandbox Code Playgroud)

摘自:http://msdn.microsoft.com/en-us/library/ms731810.aspx