WCF System.Net.WebException: The underlying connection was closed: The connection was closed unexpectedly
If you get the above error when trying to return large numbers of items in a List from a WCF service, then ensure you have set the following behaviour for the dataContractSerializer in both your client and service configuration files..
<dataContractSerializer maxItemsInObjectGraph="2147483646"/>
As in this blog, but with larger number (i.e. max of int) http://processmentor.com/community/blogs/scott_middleton/archive/2007/06/08/169.aspx
Need the settings in service layer and client, set to this.
Service has this
<behavior name="MyServiceBehavior">
<serviceDebug includeExceptionDetailInFaults="true" />
<serviceMetadata httpGetEnabled="true" />
<dataContractSerializer maxItemsInObjectGraph="2147483646"/>
behavior>
serviceBehaviors>
<service behaviorConfiguration="MyServiceBehavior"
name="MyService.ServiceImplementation.MyService">
<endpoint binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_Leads"
bindingNamespace="http://MyService.ServiceContracts/2007/04"
contract="MyService.ServiceContracts.IMyService" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
service>
services>
Client has this …
<behaviors>
<endpointBehaviors>
<behavior name="LargeList">
<dataContractSerializer maxItemsInObjectGraph="2147483646" />
behavior>
endpointBehaviors>
behaviors>
<endpoint address="http://localhost/myservice.svc" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IMyService" contract="LeadsServiceInternal.IMyService" name="BasicHttpBinding_IMyService"
behaviorConfiguration="LargeList"
/>
Note you must also set the following two settings on the client maxReceivedMessageSize and maxBufferSize
<binding name="BasicHttpBinding_IMyService" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" allowCookies="false" bypassProxyOnLocal="false"
hostNameComparisonMode="StrongWildcard" maxBufferSize="2147483646" maxBufferPoolSize="524288" maxReceivedMessageSize="2147483646" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384"/>
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None" realm=""/>
<message clientCredentialType="UserName" algorithmSuite="Default"/>
security>
binding>