System.ServiceModel.CommunicationException:基础连接已关闭

本文关键字:连接 ServiceModel CommunicationException System | 更新日期: 2023-09-27 18:36:33

我正在从 wcf Web 服务检索数据,当数据超过 20 万条记录时,我得到一个异常,如下所示:

System.ServiceModel.CommunicationException: The underlying connection was closed: A connection that was expected to be kept alive was closed by the server. ---> System.Net.WebException: The underlying connection was closed: A connection that was expected to be kept alive was closed by the server. ---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host
at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
--- End of inner exception stack trace ---
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.PooledStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.Connection.SyncRead(HttpWebRequest request, Boolean userRetrievedStream, Boolean probeRead)
--- End of inner exception stack trace ---
at System.Net.HttpWebRequest.GetResponse()
at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
--- End of inner exception stack trace ---
Server stack trace: 
at System.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException(WebException webException, HttpWebRequest request, HttpAbortReason abortReason)
at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout)
at System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message message, TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

我的 web.config 看起来像这样:

<basicHttpBinding>
  <binding name="BasicHttpBinding_IService" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
             maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"
             allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
             messageEncoding="Text" textEncoding="utf-8" transferMode="StreamedResponse" useDefaultWebProxy="true">
    <readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="2147483647"
                  maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
    <security mode="None">
      <transport clientCredentialType="None" proxyCredentialType="None" realm="" />
      <message clientCredentialType="UserName" algorithmSuite="Default" />
    </security>
  </binding>
  <binding maxReceivedMessageSize="2147483647" allowCookies="true">
    <readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647"
maxNameTableCharCount="2147483647" />
  </binding>
</basicHttpBinding>

我确定我的 Web 服务正在从数据库检索数据,但无法从我发起呼叫的位置转移到我的 Web。提前感谢任何帮助。

System.ServiceModel.CommunicationException:基础连接已关闭

我最近在 .Net 4.0 Web 应用程序中的 Wcf 服务上遇到了同样的问题。我增加了maxItemsInObjectGraph值,服务器不再抛出异常。这里的文档说默认最大值是 Int32.MaxValue,但我认为这是不正确的,最大值是 65535。

   <serviceBehaviors>
    <behavior name="ServiceBehaviour">
      <dataContractSerializer maxItemsInObjectGraph="6553500"/>
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
  </serviceBehaviors>

您可以做的另一件事是启用跟踪。这是我在web.config中拥有的示例:

<system.diagnostics>
  <sources>
    <source name="System.ServiceModel"
            switchValue="Information, ActivityTracing"
            propagateActivity="true">
      <listeners>
        <add name="traceListener"
            type="System.Diagnostics.XmlWriterTraceListener"
            initializeData= "c:'temp'log'Traces.svclog" />
      </listeners>
    </source>
  </sources>
</system.diagnostics>

如果双击 Traces.svclog,窗口应打开 Microsoft 服务跟踪查看器。

如果使用 Microsoft WCF 测试客户端测试服务,请确保更改默认客户端配置以匹配服务器设置(这包括客户端终结点行为),以确保客户端可以从服务器接收响应。

使客户端 basicHttpBinding 与服务器绑定相同。这是一个非常常见的错误,只更改一个绑定而不是服务器和客户端绑定。

如果这不起作用,您可以启用 wcf 跟踪:WCF 跟踪

这将使您更深入地了解底层问题是什么。

将以下配置添加到 wcf 服务配置中。请参阅 c:''log''Traces.svclog 文件。我的例外是抛出;

尝试序列化参数时出错InnerException 消息为"枚举值 '0' 对于类型"ThyCams2014.XrmBase.new_filingstatu"无效,无法序列化。确保必要的枚举值存在,并且如果类型具有 DataContractAttribute 属性属性代码,则使用 EnumMemberAttribute 属性进行标记。 有关更多详细信息,请参阅 InnerException。

<configuration>
   <system.diagnostics>
      <sources>
            <source name="System.ServiceModel" 
                    switchValue="Information, ActivityTracing"
                    propagateActivity="true">
            <listeners>
               <add name="traceListener" 
                   type="System.Diagnostics.XmlWriterTraceListener" 
                   initializeData= "c:'log'Traces.svclog" />
            </listeners>
         </source>
      </sources>
   </system.diagnostics>
</configuration>

我们在对象图中有一个返回的循环。我知道这可能不是您的问题,但我在这里添加它,以防其他人遇到同样的问题。我们启用了 includeExceptionDetailInFaults,但未在任何客户端(我们的应用程序或 WCF 测试客户端)中收到错误。幸运的是,它出现在服务器日志中,因此我们能够以这种方式找到它。

我们有父 -> 子项和子项 -> 父级进行双向导航,我们必须断开该链接,而是让父级 -> 子项,并且子级有一个 id 来查找父级,然后错误消失了。

迁移到新服务器后出现相同的错误。它最终归结为旧服务器上不需要的缺少targetFramework属性......

<system.web>
  <httpRuntime enableVersionHeader="false" targetFramework="4.7.2"/>
</system.web>

这句话解决了我的问题:

db.ContextConfiguration.ProxyCreationEnabled = false;