title: Encountering max message size quota in WCF
body: <p>I have inherited a WCF component where the service is a SQL query-like service.
  It takes in a query and in turn passes the query on to another web service.
  The problem is that by default, the WCF web service client accepts 100 results only.
  While it is able to remove this limit, once removed it encounters the max message size for incoming messages (65536) error.
  A brief architecture is as below.
  The client will consume a WCF service which in turn makes another service call.</p>
  <p><a href="https://i.stack.imgur.com/wEJBS.png" rel="nofollow noreferrer">Brief architecture</a></p>
  <p>This is the WCF Web.config</p>
  <pre><code>&lt;system.serviceModel&gt;
      &lt;diagnostics performanceCounters=&quot;Default&quot;&gt;
          &lt;messageLogging logEntireMessage=&quot;true&quot; /&gt;
      &lt;/diagnostics&gt;
      &lt;bindings&gt;
          &lt;basicHttpBinding&gt;
              &lt;binding name=&quot;basicBinding&quot; maxBufferPoolSize=&quot;2147483647&quot; maxBufferSize=&quot;2147483647&quot; maxReceivedMessageSize=&quot;2147483647&quot;&gt;
                  &lt;readerQuotas maxDepth=&quot;32&quot; maxStringContentLength=&quot;2147483647&quot; maxArrayLength=&quot;2147483647&quot; maxBytesPerRead=&quot;4096&quot; maxNameTableCharCount=&quot;36384&quot; /&gt;
              &lt;/binding&gt;
          &lt;/basicHttpBinding&gt;
          &lt;webHttpBinding&gt;
              &lt;binding maxBufferSize=&quot;2147483647&quot; maxReceivedMessageSize=&quot;2147483647&quot; /&gt;
          &lt;/webHttpBinding&gt;
      &lt;/bindings&gt;
      &lt;services&gt;
          &lt;service name=&quot;MYWCF.DfsWcf&quot;&gt;
              &lt;clear /&gt;
              &lt;endpoint address=&quot;&quot; binding=&quot;basicHttpBinding&quot; bindingConfiguration=&quot;basicBinding&quot; contract=&quot;MYWCF.IDfsWcf&quot;&gt;
                  &lt;identity&gt;
                      &lt;dns value=&quot;localhost&quot; /&gt;
                  &lt;/identity&gt;
              &lt;/endpoint&gt;
              &lt;endpoint address=&quot;mex&quot; binding=&quot;mexHttpBinding&quot; contract=&quot;IMetadataExchange&quot; /&gt;
              &lt;host&gt;
                  &lt;baseAddresses&gt;
                      &lt;add baseAddress=&quot;http://mercury/MOM_WCF/&quot; /&gt;
                  &lt;/baseAddresses&gt;
              &lt;/host&gt;
          &lt;/service&gt;
      &lt;/services&gt;
      &lt;behaviors&gt;
          &lt;serviceBehaviors&gt;
              &lt;behavior name=&quot;&quot;&gt;
                  &lt;serviceMetadata httpGetEnabled=&quot;true&quot; /&gt;
                  &lt;serviceDebug includeExceptionDetailInFaults=&quot;false&quot; /&gt;
              &lt;/behavior&gt;
          &lt;/serviceBehaviors&gt;
      &lt;/behaviors&gt;
  &lt;/system.serviceModel&gt;
  </code></pre>
  <p>I have tried to set the maxReceivedMessageSize in the wcfTestClient config.
  I have also written a standalone web service client. Both returns the same 64k max message error.</p>
  <p>I have tested not making the chained web service call and instead looping and returning some dummy data, and it is able to return the desired number of results I need (this web service is a SQL query-like service, where it takes in a query and returns the String results to the client).
  Thus, it seems like the problem exists when the WCF acts as the web service client to the DFS (Documentum Foundation Services) web service, and that the response message returned exceeded the 64k limit.
  How do I set the maxReceivedMessageSize for the web service client in WCF?</p>
  <p>Edit: added the WCF code and the code it uses to call DFS.</p>
  <pre><code>    public List&lt;String[]&gt; DoQuery(String username, String password, String repository, String query)
      {
          log4net.Config.XmlConfigurator.Configure();
          log.Debug(&quot;DoQuery started..&quot;);
          List&lt;String[]&gt; results = new List&lt;String[]&gt;();
          try
          {
              var prop = new Properties();
              var dfs = new DFSCore();
              var serviceContext = dfs.GetServiceContext(repository, username, password, prop.Domain);
              var queryService = dfs.GetQueryService(serviceContext, prop.Address);
              log.Debug(&quot;Executing DQL: &quot; + query);
  
              /*
              for (int i = 0; i &lt; 3000; i++)
              {
                  results.Add(new string[] { &quot;&quot; + i, &quot;objectID=&quot; + i });
              }
              */
  
              //return results;
              return dfs.ExecuteDql(queryService, query, prop.DocBase, int.Parse(prop.MaxResultCount));
          }
          catch (Exception e)
          {
              log.Error(e);
              throw e;
          }
      }
  
     public List&lt;String[]&gt; ExecuteDql(IQueryService querySvc, String dqlQuery, String repositoryName, int maxResultCount)
      {
          log4net.Config.XmlConfigurator.Configure();
          var results = new List&lt;String[]&gt;();
  
          try
          {
              var query = new PassthroughQuery { QueryString = dqlQuery };
              query.AddRepository(repositoryName);
              var queryEx = new QueryExecution { CacheStrategyType = CacheStrategyType.DEFAULT_CACHE_STRATEGY };
              queryEx.MaxResultCount = maxResultCount;
              queryEx.MaxResultPerSource = maxResultCount;
  
              List&lt;String&gt; fieldNames = new List&lt;String&gt;();
              String[] temp = dqlQuery.Split(new String[] { &quot;from&quot; }, StringSplitOptions.None);
  
              if (temp[0].StartsWith(&quot;select &quot;))
              {
                  string fields = temp[0].Substring(&quot;select &quot;.Length).Trim();
                  DFSCore.log.Debug(&quot;fields = &quot; + fields);
                  string[] temp2 = fields.Split(new char[] { ',' });
                  foreach (string field in temp2)
                  {
                      DFSCore.log.Debug(&quot;\tfield = &quot; + field);
                      string[] temp3 = field.Split(new string[] { &quot; as &quot; }, StringSplitOptions.None);
                      DFSCore.log.Debug(string.Concat(new object[] { &quot;\t\ttemp3 = &quot;, temp3, &quot;, length = &quot;, temp3.Length }));
                      if (temp3.Length == 1)
                      {
                          if (temp3[0].Contains(&quot;.&quot;))
                          {
                              temp3[0] = temp3[0].Split(new string[] { &quot;.&quot; }, StringSplitOptions.None)[1];
                          }
                          DFSCore.log.Debug(&quot;\t\t\tadding fieldName &quot; + temp3[0].Trim());
                          fieldNames.Add(temp3[0].Trim());
                      }
                      else
                      {
                          DFSCore.log.Debug(&quot;\t\t\tadding fieldName &quot; + temp3[1].Trim());
                          fieldNames.Add(temp3[1].Trim());
                      }
                  }
              }
  
              var queryResult = querySvc.Execute(query, queryEx, null);
              var dataPackage = queryResult.DataPackage;
              var dataObjects = dataPackage.DataObjects;
  
              int index = 0;
              //convert the results into a List&lt;AclIdentity&gt;
              foreach (var dObj in dataObjects)
              {
                  var docProperties = dObj.Properties;
                  String[] temp4 = new String[fieldNames.Count];
  
                  for (int i = 0; i &lt; fieldNames.Count; i++)
                  {
                      String fieldName = fieldNames[i];
                      var result = docProperties.Get(fieldName).GetValueAsString();
                      temp4[i] = result;
                      DFSCore.log.Debug(string.Concat(new object[] { &quot;[&quot;, index, &quot;] fieldName[&quot; + i + &quot;] = &quot;, fieldName, &quot;, value = &quot;, result }));
                  }
                  results.Add(temp4);
                  index++;
              }
          }
          catch (Exception e)
          {
              DFSCore.log.Error(e);
          }
          return results;
      }
  }
  </code></pre>