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 by Stormpath—offering a pre-built Identity API for developers. Easily build powerful user management, authentication, and authorization into your web and mobile applications. Download this Forrester report on the new landscape of Customer Identity and Access Management.

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 by Stormpath—offering a pre-built, streamlined User Management API for building web and mobile applications. Plan On Building User Management? Buy It Instead. Download Our White Paper To Learn More.

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 }}