west
west

Reputation: 384

The underlying connection was closed: The connection was closed unexpectedly wcf

I am using WCF Services in WPF project. I have a large amount of data, about 847 000 records in table. this exception is throwed on client side

In VM

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.')

In 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>
      <behaviors>
        <endpointBehaviors>
          <behavior name="clientBehavior">
            <dataContractSerializer maxItemsInObjectGraph="2147483647" />
          </behavior>
        </endpointBehaviors>
      </behaviors>
        <client>
            <endpoint address="http://localhost:7902/WpfStoreService.svc"
                binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IServices"
                contract="proxy.IServices" name="BasicHttpBinding_IServices" behaviorConfiguration="clientBehavior" />
        </client>
    </system.serviceModel>
</configuration>

In 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="2147483647">
          <readerQuotas maxDepth="2147483647"
                        maxStringContentLength="2147483647"
                        maxArrayLength="2147483647"
                        maxBytesPerRead="214748364"
                        maxNameTableCharCount="2147483647"/>

          <security mode="None"/>
        </binding>

      </basicHttpBinding>
    </bindings>

    <services>
      <service name="WpfStore.Services.Services" behaviorConfiguration="debugbehavior">
        <endpoint address="" binding="basicHttpBinding" contract="WpfStore.Services.Contracts.IServices" bindingConfiguration="BasicHttpBinding_IServices"/>
      </service>
    </services>

    <behaviors>
      <serviceBehaviors>
        <behavior name="debugbehavior">
          <serviceMetadata httpGetEnabled="true"/> 
          <serviceDebug includeExceptionDetailInFaults="false"/>
          <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>   
  </system.webServer>  
</configuration>

Note: 1. It dose not work with large amount of data. In this case I have about 847 000 records in table. It works with small data perfectly(with about 5000 - 6000 records). 2. I debug it and all record retrieved from SQL server to my DAL; This Exception rises during 5-10 seconds, after I call from client this service function.

Upvotes: 1

Views: 9603

Answers (1)

Tassisto
Tassisto

Reputation: 10345

I just added this line in my code

System.Net.ServicePointManager.Expect100Continue = false;

Find the explanation on MSDN

Hope this will help you.

Upvotes: 1

Related Questions