Platinum Partner
web dev

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.

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>


Published at DZone with permission of {{ articles[0].authors[0].realName }}, DZone MVB. (source)

Opinions expressed by DZone contributors are their own.

{{ tag }}, {{tag}},

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

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks
Tweet

{{parent.nComments}}