底层连接已关闭:连接被意外关闭- wcf

本文关键字:连接 意外 wcf | 更新日期: 2023-09-27 18:06:05

我在我的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。配置

<?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服务上有这个问题吗?

底层连接已关闭:连接被意外关闭- wcf

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

我认为如果你将连接超时值设置为0,它可以被禁用,但这取决于DbConnection的实现;

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

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