Over a million developers have joined DZone.
Platinum Partner

WCF: The Underlying Connection was Closed Unexpectedly

What to do if you get the above error when trying to return large numbers of items in a List from a WCF service.

· Web Dev Zone

The Web Dev Zone is brought to you in partnership with Mendix. Download this Forrester Report to gain a better understanding of the low-code platform market and how to make a strategic platform selection you won’t regret.

If you get the above error when trying to return large numbers of items in a List<T> 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="MyService.ServiceImplementation.MyService_Behavior">

                                  <serviceDebug includeExceptionDetailInFaults="true" />

                                  <serviceMetadata httpGetEnabled="true" />

                                  <dataContractSerializer maxItemsInObjectGraph="2147483646"/>

                           </behavior>

                     </serviceBehaviors>

<service behaviorConfiguration="MyService.ServiceImplementation.MyService_Behavior"

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="debuggingBehaviour">

<dataContractSerializer maxItemsInObjectGraph="2147483646" />

                           </behavior>

                     </endpointBehaviors>

              </behaviors>

<endpoint address="http://localhost/Host/Internal/myservice.svc" binding="basicHttpBinding"

bindingConfiguration="BasicHttpBinding_IMyService"contract="LeadsServiceInternal.IMyService" name="BasicHttpBinding_IMyService"

behaviorConfiguration="debuggingBehaviour"

                                    /> 


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="500000000" maxBufferPoolSize="524288" maxReceivedMessageSize="500000000"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>


The Web Dev Zone is brought to you in partnership with Mendix. Better understand the aPaaS landscape and how the right platform can accelerate your software delivery cadence and capacity with the Gartner 2015 Magic Quadrant for Enterprise Application Platform as a Service.

Topics:
web dev

Published at DZone with permission of Merrick Chaffer , DZone MVB .

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}