【问题标题】:The underlying connection was closed: The connection was closed unexpectedly - wcf底层连接已关闭:连接意外关闭 - wcf
【发布时间】:2013-03-26 09:26:55
【问题描述】:

我在我的 WPF 项目中使用 WCF 服务。 我有大量的数据,表中大约有 847000 条记录。这个异常是在客户端抛出的

proxy.ServicesClient client = new proxy.ServicesClient();
var result = client.GetCustomers(); // here throws ('**ComunicationException was unhandled by user code**: *The underlying connection was closed: The connection was closed unexpectedly*.')

在我的 app.config 中

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
      <bindings>
        <basicHttpBinding>
          <binding name="BasicHttpBinding_IServices" closeTimeout="00:10:00"
              openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00"
              maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
            <security mode="None" />
          </binding>
        </basicHttpBinding>
      </bindings>
        <client>
            <endpoint address="http://localhost:5302/WpfStoreService.svc"
                binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IServices"
                contract="proxy.IServices" name="BasicHttpBinding_IServices" />
        </client>
    </system.serviceModel>
</configuration>

在 Web.Config 中

<?xml version="1.0"?>
<configuration>

  <connectionStrings>
    <add name="AdventureWorksLTConnectionString" connectionString="Data Source=.;Initial Catalog=AdventureWorksLT;Integrated Security=True" />
  </connectionStrings>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>

    <bindings>
      <basicHttpBinding>

        <binding name="BasicHttpBinding_IServices" receiveTimeout="00:10:00" sendTimeout="00:10:00" openTimeout="00:10:00" closeTimeout="00:10:00"
                 maxReceivedMessageSize="2147483647"
                 maxBufferSize="2147483647"
                 maxBufferPoolSize="52428899">
          <readerQuotas maxDepth="128"
                        maxStringContentLength="2147483647"
                        maxArrayLength="2147483647"
                        maxBytesPerRead="4096"
                        maxNameTableCharCount="2147483647" />
          <security mode="None"/>
        </binding>

      </basicHttpBinding>
    </bindings>

    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="True" />
          <serviceDebug includeExceptionDetailInFaults="true" />
          <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>

    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

</configuration>

您在 WCF 服务上遇到过这个问题吗?

【问题讨论】:

  • 您的配置中存在超时:问题不是在 10 分钟后出现吗?
  • 否,此异常发生在 30 - 40 秒内

标签: c# wpf wcf silverlight


【解决方案1】:

除非您在其他地方覆盖它,否则默认的数据库连接超时将是一个问题。

我认为如果将 Connection Timeout 值设置为 0 可以禁用它,但这取决于 DbConnection 实现;

尝试将此添加到您的连接字符串。 Connect Timeout=0

"Data Source=.;Initial Catalog=AdventureWorksLT;Integrated Security=True;Connect Timeout=0"

【讨论】:

  • 感谢您的回复,但不起作用:(我注意到这个异常发生在大量数据上。如果我将记录数减少到 1000(现在我有 847 000)它将起作用
  • 真的只有一个...不要在一个事务中选择 847000 条记录。而是集中精力重新设计界面以处理更小的数据块。
  • 从 SQL Server 检索记录没有问题。我对其进行了调试,并在服务器端毫无例外地检索了整个 847 000 条记录。在 wcf 服务调用此函数后会引发该异常
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-31
  • 1970-01-01
相关资源
最近更新 更多