title: WCF, out of memory exception while serializing huge amount of data
body: <p>Basically, I am fetching data from SQL Server. If I select all data (which is about 80000 rows), then I am getting the following error  (Receiving 100 rows is working fine)<br/></p>
  
  <pre><code>Exception of type 'System.OutOfMemoryException' was thrown. &lt;br/&gt;
  
  Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. &lt;br/&gt;
  
  Exception Details: System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.
  
  Stack Trace: 
  
  [OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.]
     System.Text.StringBuilder.ToString() +29
     System.IO.StringWriter.ToString() +14
     System.Net.WebUtility.HtmlEncode(String value) +110
     System.Web.Util.HttpEncoder.HtmlEncode(String value) +54
     System.Web.UI.HtmlControls.HtmlContainerControl.set_InnerText(String value) +24
     PerformanceCompare._Default.Page_Load(Object sender, EventArgs e) in C:\Users\KK33562\Documents\Visual Studio 2010\Projects\TestWebService\PerformanceCompare\Default.aspx.cs:34
     System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
     System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
     System.Web.UI.Control.OnLoad(EventArgs e) +91
     System.Web.UI.Control.LoadRecursive() +74
     System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207
  </code></pre>
  
  <p>Web.config (Client Side)</p>
  
  <pre><code>&lt;system.serviceModel&gt;
  &lt;behaviors&gt;
    &lt;endpointBehaviors&gt;
      &lt;behavior name="endpointbehaviour"&gt;
        &lt;dataContractSerializer maxItemsInObjectGraph="2147483647"/&gt;
      &lt;/behavior&gt;
    &lt;/endpointBehaviors&gt;
  &lt;/behaviors&gt;
  &lt;bindings&gt;
    &lt;basicHttpBinding&gt;
      &lt;binding name="BasicHttpBinding_IService1" closeTimeout="00:50:00"
        openTimeout="00:50:00" receiveTimeout="00:50:00" sendTimeout="00:50:00"
        allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
        maxBufferSize="2147483647" maxBufferPoolSize="524288" maxReceivedMessageSize="2147483647"
        messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
        useDefaultWebProxy="true"&gt;
        &lt;readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="2147483647"
          maxBytesPerRead="4096" maxNameTableCharCount="16384" /&gt;
        &lt;security mode="None"&gt;
          &lt;transport clientCredentialType="None" proxyCredentialType="None"
            realm="" /&gt;
          &lt;message clientCredentialType="UserName" algorithmSuite="Default" /&gt;
        &lt;/security&gt;
      &lt;/binding&gt;
    &lt;/basicHttpBinding&gt;
  &lt;/bindings&gt;
  &lt;client&gt;
    &lt;endpoint address="http://localhost:53268/Service1.svc" binding="basicHttpBinding"
      bindingConfiguration="BasicHttpBinding_IService1" contract="WCFService.IService1"
      name="BasicHttpBinding_IService1" behaviorConfiguration="endpointbehaviour" /&gt;
  &lt;/client&gt;
  &lt;/system.serviceModel&gt;
  </code></pre>
  
  <p>Web.config (Server-Side)</p>
  
  <pre><code>&lt;system.serviceModel&gt;
  &lt;behaviors&gt;      
    &lt;serviceBehaviors&gt;
      &lt;behavior name="TestWCF.Service1Behavior"&gt;
        &lt;dataContractSerializer maxItemsInObjectGraph="2147483646"/&gt;
        &lt;serviceMetadata httpGetEnabled="true"/&gt;
        &lt;serviceDebug includeExceptionDetailInFaults="true"/&gt;
      &lt;/behavior&gt;
    &lt;/serviceBehaviors&gt;
  &lt;/behaviors&gt;
    &lt;services&gt;
      &lt;service name="TestWCF.Service1" behaviorConfiguration="TestWCF.Service1Behavior"&gt;
        &lt;endpoint address="" binding="basicHttpBinding" contract="TestWCF.IService1" bindingConfiguration="BasicHttpBinding_IService1"&gt;
          &lt;identity&gt;
            &lt;dns value="localhost"/&gt;
          &lt;/identity&gt;
        &lt;/endpoint&gt;
        &lt;endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/&gt;
      &lt;/service&gt;
    &lt;/services&gt;    
  &lt;bindings&gt;
    &lt;basicHttpBinding&gt;
      &lt;binding name="BasicHttpBinding_IService1" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" openTimeout="00:50:00" closeTimeout="00:50:00" sendTimeout="00:50:00" receiveTimeout="00:10:00"&gt;
        &lt;readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647"
          maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" /&gt;
      &lt;/binding&gt;
    &lt;/basicHttpBinding&gt;
  &lt;/bindings&gt;
  &lt;/system.serviceModel&gt;
  </code></pre>