将WCF服务从WsHttpBinding转换为NetTcpBinding时的CommunicationException

时间:2012-12-06 19:17:05

标签: wcf nettcpbinding

我有一个自托管的.Net 4.0 WCF服务,出于性能原因,我正在尝试从WsHttpBinding转换为NetTcpBinding。使用WsHttpBinding,该服务可以正常工作。

奇怪的是,该服务似乎在切换到NetTcpBinding后工作。 (该服务将消息发布到企业消息系统,并且消息已发布且客户端未收到任何错误)但是,以下错误将写入服务器日志:

Error: Service: MessageSenderService, Details: 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 '10675199.02:48:05.4775807'. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host
   at System.ServiceModel.Channels.SocketConnection.HandleReceiveAsyncCompleted()
   at System.ServiceModel.Channels.SocketConnection.BeginReadCore(Int32 offset,Int32 size, TimeSpan timeout, WaitCallback callback, Object state)
   --- End of inner exception stack trace ---
   at System.ServiceModel.Channels.SocketConnection.BeginReadCore(Int32 offset,Int32 size, TimeSpan timeout, WaitCallback callback, Object state)
   at System.ServiceModel.Channels.TracingConnection.BeginRead(Int32 offset, Int32 size, TimeSpan timeout, WaitCallback callback, Object state)
   at System.ServiceModel.Channels.SessionConnectionReader.BeginReceive(TimeSpan timeout, WaitCallback callback, Object state)
   at System.ServiceModel.Channels.SynchronizedMessageSource.ReceiveAsyncResult.PerformOperation(TimeSpan timeout)
   at System.ServiceModel.Channels.SynchronizedMessageSource.SynchronizedAsyncResult`1..ctor(SynchronizedMessageSource syncSource, TimeSpan timeout, AsyncCallback callback, Object state)
   at System.ServiceModel.Channels.TransportDuplexSessionChannel.BeginReceive(TimeSpan timeout, AsyncCallback callback, Object state)
   at System.ServiceModel.Channels.TransportDuplexSessionChannel.TryReceiveAsyncResult..ctor(TransportDuplexSessionChannel channel, TimeSpan timeout, AsyncCallback callback, Object state)
   at System.ServiceModel.Channels.TransportDuplexSessionChannel.BeginTryReceive(TimeSpan timeout, AsyncCallback callback, Object state)
   at System.ServiceModel.Dispatcher.ErrorHandlingReceiver.BeginTryReceive(TimeSpan timeout, AsyncCallback callback, Object state)

同样,使用WsHttpBinding时服务器日志不会发生错误。

服务器配置:

      <service behaviorConfiguration="debugEnabled" name="My.Service">
        <endpoint address="" binding="netTcpBinding" bindingConfiguration="netTcpBindingConfig"
          contract="My.Service.Contract" />
        <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />
      </service>
....
    <behaviors>
      <serviceBehaviors>
        <behavior name="debugEnabled">
          <serviceDebug includeExceptionDetailInFaults="true" />
          <serviceThrottling maxConcurrentSessions="200" maxConcurrentCalls="500" maxConcurrentInstances="200"/>
          <serviceMetadata httpGetEnabled="true" />
          <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
....
    <bindings>
      <netTcpBinding>
        <binding name="netTcpBindingConfig" maxReceivedMessageSize="5242880" closeTimeout="00:01:00" receiveTimeout="00:01:00" sendTimeout="00:01:00" openTimeout="00:01:00">
          <readerQuotas maxArrayLength="5242880" />
          <security mode="None" />
        </binding>
      </netTcpBinding>
    </bindings>

我们在代码中构建客户端绑定,而不是配置。看起来像这样:

return new NetTcpBinding(SecurityMode.None, reliableSessionEnabled)
{
    MaxBufferPoolSize = Int32.MaxValue,
    MaxReceivedMessageSize = Int32.MaxValue,
    ReaderQuotas = new XmlDictionaryReaderQuotas
    {
        MaxArrayLength = Int32.MaxValue,
        MaxDepth = Int32.MaxValue,
        MaxStringContentLength = Int32.MaxValue
    },
    ReceiveTimeout = TimeSpan.FromMinutes(1.0),
    CloseTimeout = TimeSpan.FromMinutes(1.0),
    OpenTimeout = TimeSpan.FromMinutes(1.0),
    SendTimeout = TimeSpan.FromMinutes(1.0)
};

有任何想法吗?

编辑:我想补充一点,我在每次调用服务时都会看到错误,而不仅仅是在负载下。请求和响应消息非常小,因此可能与消息大小无关。错误几乎是瞬间写的,所以它不是超时问题。

1 个答案:

答案 0 :(得分:0)

修正了它。

事实证明,我没有正确关闭客户端代理。无论出于何种原因,不会导致WsHttpBinding出现问题,但使用NetTcpBinding会导致该错误。

将客户端代理放入使用块之后,问题就消失了。