title: Data sent by WCF client is broken
body: <p>We are getting following exception in the <code>.svclog</code> file generated through system diagnostics config settings at server side.</p>
  
  <blockquote>
    <p>Exception: System.ServiceModel.ProtocolException<br>
    Message: The number of bytes available is inconsistent with the HTTP Content-Length
    header.  There may have been a network error or the client may be
    sending invalid requests.</p>
  </blockquote>
  
  <p>We have observed that the data sent by WCF client is broken. We came to know about this by viewing the message xml in the <code>.svclog</code> file generated at WCF client side.</p>
  
  <p>Actually we are sending data large data by dividing it into small chunks calling the WCF service method to send data in a loop. Following is the code for the same at WCF client side.</p>
  
  <pre><code>DataTable dtStockDetails = new DataTable("StockDetails");
  sqlDataAdapter.Fill(dtStockDetails);
  
  int stockDetailsBatchSize = 100;
  DataTable dtStockDetailsBatch = dtStockDetails.Clone();
  DataRow dr;
  int stockDetailsBatchCount = 0;
  int stockDetailsTotalRecords = dtStockDetails.Rows.Count;
  
  while (dtStockDetails.Rows.Count &gt; 0)
  {
      dr = dtStockDetails.Rows[0];
  
      dtStockDetailsBatch.ImportRow(dr);
      dtStockDetailsBatch.AcceptChanges();
  
      dtStockDetails.Rows.Remove(dr);
      dtStockDetails.AcceptChanges();
  
      if ((dtStockDetailsBatch.Rows.Count == stockDetailsBatchSize) || (dtStockDetails.Rows.Count == 0))
      {
          stockDetailsBatchCount++;
          sendStockDetailsResult = serviceClient.SendStockDetails(dtStockDetailsBatch);
          dtStockDetailsBatch.Clear();
      }
  }
  </code></pre>
  
  <p>Now WCF client some times sends 7 batches of data, some times sends 9 batches of data, some times sends 10 batches of data, .... . In all cases lase message(data batch) xml gets corrupted. This behavior is random.</p>
  
  <p>We are not sure why the message xml is getting corrupted and resulting into end of communication. It never sends all batches of data.</p>
  
  <p>Following are config settings:</p>
  
  <h2>WCF Service config settings:</h2>
  
  <pre><code>&lt;system.serviceModel&gt;
    &lt;serviceHostingEnvironment aspNetCompatibilityEnabled="true"/&gt;
    &lt;services&gt;
      &lt;service behaviorConfiguration="InventoryServices.InventoryImportServiceBehavior"
        name="InventoryServices.InventoryImportService"&gt;
        &lt;endpoint address="https://www.domain.com/InventoryServices/InventoryImportService.svc" behaviorConfiguration="wsServiceEndpointBehavior"
          binding="wsHttpBinding" bindingConfiguration="b1" contract="InventoryServices.IInventoryImportService"&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;host&gt;
          &lt;baseAddresses&gt;
            &lt;add baseAddress="https://www.domain.com" /&gt;
          &lt;/baseAddresses&gt;
        &lt;/host&gt;
      &lt;/service&gt;
    &lt;/services&gt;
    &lt;bindings&gt;
      &lt;wsHttpBinding&gt;
        &lt;binding name="b1" closeTimeout="00:30:00" openTimeout="00:30:00"
          receiveTimeout="00:30:00" sendTimeout="00:30:00" maxReceivedMessageSize="2147483647"&gt;
          &lt;readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
            maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" /&gt;
          &lt;reliableSession inactivityTimeout="00:30:00" /&gt;
          &lt;security mode="TransportWithMessageCredential"&gt;
            &lt;transport clientCredentialType="Certificate"&gt;
            &lt;/transport&gt;
            &lt;message clientCredentialType="UserName" negotiateServiceCredential="true" /&gt;
          &lt;/security&gt;
        &lt;/binding&gt;
      &lt;/wsHttpBinding&gt;
    &lt;/bindings&gt;
    &lt;behaviors&gt;
      &lt;endpointBehaviors&gt;
        &lt;behavior name="wsServiceEndpointBehavior"&gt;
          &lt;dataContractSerializer maxItemsInObjectGraph="2147483647" /&gt;
        &lt;/behavior&gt;
      &lt;/endpointBehaviors&gt;
      &lt;serviceBehaviors&gt;
        &lt;behavior name="InventoryServices.InventoryImportServiceBehavior"&gt;
          &lt;serviceMetadata httpGetEnabled="true" /&gt;
          &lt;serviceDebug includeExceptionDetailInFaults="true" /&gt;
          &lt;serviceCredentials&gt;
            &lt;serviceCertificate findValue="XyzClient" x509FindType="FindBySubjectName" /&gt;
            &lt;userNameAuthentication userNamePasswordValidationMode="Custom"
              customUserNamePasswordValidatorType="InventoryServices.Helpers.CustomValidator, InventoryServices" /&gt;
          &lt;/serviceCredentials&gt;
        &lt;/behavior&gt;
      &lt;/serviceBehaviors&gt;
    &lt;/behaviors&gt;
  &lt;/system.serviceModel&gt;
  </code></pre>
  
  <h2>WCF Client config settings:</h2>
  
  <pre><code>&lt;system.serviceModel&gt;
    &lt;diagnostics&gt;
      &lt;messageLogging logEntireMessage="true" logMalformedMessages="true"
        logMessagesAtServiceLevel="true" logMessagesAtTransportLevel="true"
        maxSizeOfMessageToLog="2147483647" /&gt;
    &lt;/diagnostics&gt;
    &lt;bindings&gt;
      &lt;wsHttpBinding&gt;
        &lt;binding name="WSHttpBinding_IInventoryImportService"
            closeTimeout="00:30:00" openTimeout="00:30:00" receiveTimeout="00:30:00"
            sendTimeout="00:30:00" bypassProxyOnLocal="false" transactionFlow="false"
            hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="524288"
            maxReceivedMessageSize="65536" messageEncoding="Text" textEncoding="utf-8"
            useDefaultWebProxy="true" allowCookies="false"&gt;
          &lt;readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
              maxBytesPerRead="4096" maxNameTableCharCount="16384" /&gt;
          &lt;reliableSession ordered="true" inactivityTimeout="00:30:00"
              enabled="false" /&gt;
          &lt;security mode="TransportWithMessageCredential"&gt;
            &lt;transport clientCredentialType="None" proxyCredentialType="None"
                realm=""&gt;
              &lt;extendedProtectionPolicy policyEnforcement="Never" /&gt;
            &lt;/transport&gt;
            &lt;message clientCredentialType="UserName" negotiateServiceCredential="true"
                algorithmSuite="Default" establishSecurityContext="true" /&gt;
          &lt;/security&gt;
        &lt;/binding&gt;
      &lt;/wsHttpBinding&gt;
    &lt;/bindings&gt;
    &lt;client&gt;
      &lt;endpoint address="https://www.domain.com/InventoryServices/InventoryImportService.svc"
              binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IInventoryImportService"
              contract="InventoryImportService.IInventoryImportService"
              name="WSHttpBinding_IInventoryImportService"&gt;
        &lt;identity&gt;
          &lt;dns value="localhost" /&gt;
        &lt;/identity&gt;
      &lt;/endpoint&gt;
    &lt;/client&gt;
  &lt;/system.serviceModel&gt;
  &lt;system.diagnostics&gt;
    &lt;sources&gt;
      &lt;source name="System.ServiceModel" switchValue="Verbose, ActivityTracing"
        propagateActivity="false"&gt;
        &lt;listeners&gt;
          &lt;add type="System.Diagnostics.DefaultTraceListener" name="Default"&gt;
            &lt;filter type="" /&gt;
          &lt;/add&gt;
          &lt;add name="sharedListener"&gt;
            &lt;filter type="" /&gt;
          &lt;/add&gt;
        &lt;/listeners&gt;
      &lt;/source&gt;
      &lt;source name="System.ServiceModel.MessageLogging" switchValue="Verbose"&gt;
        &lt;listeners&gt;
          &lt;add type="System.Diagnostics.DefaultTraceListener" name="Default"&gt;
            &lt;filter type="" /&gt;
          &lt;/add&gt;
          &lt;add name="sharedListener"&gt;
            &lt;filter type="" /&gt;
          &lt;/add&gt;
        &lt;/listeners&gt;
      &lt;/source&gt;
    &lt;/sources&gt;
    &lt;sharedListeners&gt;
      &lt;add initializeData="C:\Program Files\InventoryExportUtilitySetup\servicetrace.svclog"
        type="System.Diagnostics.XmlWriterTraceListener" name="sharedListener"&gt;
        &lt;filter type="" /&gt;
      &lt;/add&gt;
    &lt;/sharedListeners&gt;
  &lt;/system.diagnostics&gt;
  </code></pre>
  
  <p>Can any one please suggest what should we do to resolve this problem?</p>
  
  <p>Thanks in advance.</p>