title: WCF Service returning 400 error: The body of the message cannot be read because it is empty
body: <p>I have a WCF service that is causing a bit of a headache.  I have tracing enabled, I have an object with a data contract being built and passed in, but I am seeing this error in the log:</p>
  
  <pre><code>&lt;TraceData&gt;
              &lt;DataItem&gt;
                  &lt;TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Error"&gt;
                      &lt;TraceIdentifier&gt;http://msdn.microsoft.com/en-US/library/System.ServiceModel.Diagnostics.ThrowingException.aspx&lt;/TraceIdentifier&gt;
                      &lt;Description&gt;Throwing an exception.&lt;/Description&gt;
                      &lt;AppDomain&gt;efb0d0d7-1-129315381593520544&lt;/AppDomain&gt;
                      &lt;Exception&gt;
                          &lt;ExceptionType&gt;System.ServiceModel.ProtocolException, System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089&lt;/ExceptionType&gt;
                          &lt;Message&gt;There is a problem with the XML that was received from the network. See inner exception for more details.&lt;/Message&gt;
                          &lt;StackTrace&gt;
                              at System.ServiceModel.Channels.HttpRequestContext.CreateMessage()
                              at System.ServiceModel.Channels.HttpChannelListener.HttpContextReceived(HttpRequestContext context, Action callback)
                              at System.ServiceModel.Activation.HostedHttpTransportManager.HttpContextReceived(HostedHttpRequestAsyncResult result)
                              at System.ServiceModel.Activation.HostedHttpRequestAsyncResult.HandleRequest()
                              at System.ServiceModel.Activation.HostedHttpRequestAsyncResult.BeginRequest()
                              at System.ServiceModel.Activation.HostedHttpRequestAsyncResult.OnBeginRequest(Object state)
                              at System.Runtime.IOThreadScheduler.ScheduledOverlapped.IOCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* nativeOverlapped)
                              at System.Runtime.Fx.IOCompletionThunk.UnhandledExceptionFrame(UInt32 error, UInt32 bytesRead, NativeOverlapped* nativeOverlapped)
                              at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)
                          &lt;/StackTrace&gt;
                          &lt;ExceptionString&gt;
                              System.ServiceModel.ProtocolException: There is a problem with the XML that was received from the network. See inner exception for more details. ---&amp;amp;gt; System.Xml.XmlException: The body of the message cannot be read because it is empty.
                              --- End of inner exception stack trace ---
                          &lt;/ExceptionString&gt;
                          &lt;InnerException&gt;
                              &lt;ExceptionType&gt;System.Xml.XmlException, System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089&lt;/ExceptionType&gt;
                              &lt;Message&gt;The body of the message cannot be read because it is empty.&lt;/Message&gt;
                              &lt;StackTrace&gt;
                                  at System.ServiceModel.Channels.HttpRequestContext.CreateMessage()
                                  at System.ServiceModel.Channels.HttpChannelListener.HttpContextReceived(HttpRequestContext context, Action callback)
                                  at System.ServiceModel.Activation.HostedHttpTransportManager.HttpContextReceived(HostedHttpRequestAsyncResult result)
                                  at System.ServiceModel.Activation.HostedHttpRequestAsyncResult.HandleRequest()
                                  at System.ServiceModel.Activation.HostedHttpRequestAsyncResult.BeginRequest()
                                  at System.ServiceModel.Activation.HostedHttpRequestAsyncResult.OnBeginRequest(Object state)
                                  at System.Runtime.IOThreadScheduler.ScheduledOverlapped.IOCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* nativeOverlapped)
                                  at System.Runtime.Fx.IOCompletionThunk.UnhandledExceptionFrame(UInt32 error, UInt32 bytesRead, NativeOverlapped* nativeOverlapped)
                                  at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)
                              &lt;/StackTrace&gt;
                              &lt;ExceptionString&gt;System.Xml.XmlException: The body of the message cannot be read because it is empty.&lt;/ExceptionString&gt;
                          &lt;/InnerException&gt;
                      &lt;/Exception&gt;
                  &lt;/TraceRecord&gt;
              &lt;/DataItem&gt;
          &lt;/TraceData&gt;
  </code></pre>
  
  <p>So, here is my service interface:</p>
  
  <pre><code>[ServiceContract]
      public interface IRDCService
      {
          [OperationContract]
          Response&lt;Customer&gt; GetCustomer(CustomerRequest request);
  
          [OperationContract]
          Response&lt;Customer&gt; GetSiteCustomers(CustomerRequest request);
      }
  </code></pre>
  
  <p>And here is my service instance</p>
  
  <pre><code>public class RDCService : IRDCService
      {
          ICustomerService customerService;
  
          public RDCService()
          {
              //We have to locate the instance from structuremap manually because web services *REQUIRE* a default constructor
              customerService = ServiceLocator.Locate&lt;ICustomerService&gt;();
          }
  
          public Response&lt;Customer&gt; GetCustomer(CustomerRequest request)
          {
              return customerService.GetCustomer(request);
          }
  
          public Response&lt;Customer&gt; GetSiteCustomers(CustomerRequest request)
          {
              return customerService.GetSiteCustomers(request);
          }
      }
  </code></pre>
  
  <p>The configuration for the web service (server side) looks like this:</p>
  
  <pre><code>&lt;system.serviceModel&gt;
          &lt;diagnostics&gt;
              &lt;messageLogging logMalformedMessages="true" logMessagesAtServiceLevel="true"
                  logMessagesAtTransportLevel="true" /&gt;
          &lt;/diagnostics&gt;
          &lt;services&gt;
              &lt;service behaviorConfiguration="MySite.Web.Services.RDCServiceBehavior"
                  name="MySite.Web.Services.RDCService"&gt;
                  &lt;endpoint address="http://localhost:27433" binding="wsHttpBinding"
                      contract="MySite.Common.Services.Web.IRDCService"&gt;
                      &lt;identity&gt;
                          &lt;dns value="localhost:27433" /&gt;
                      &lt;/identity&gt;
                  &lt;/endpoint&gt;
                  &lt;endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /&gt;
              &lt;/service&gt;
          &lt;/services&gt;
          &lt;behaviors&gt;
              &lt;serviceBehaviors&gt;
                  &lt;behavior name="MySite.Web.Services.RDCServiceBehavior"&gt;
                      &lt;!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment --&gt;
                      &lt;serviceMetadata httpGetEnabled="true"/&gt;
                      &lt;!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information --&gt;
                      &lt;serviceDebug includeExceptionDetailInFaults="true"/&gt;
  
  
                      &lt;dataContractSerializer maxItemsInObjectGraph="6553600" /&gt;
                  &lt;/behavior&gt;
              &lt;/serviceBehaviors&gt;
          &lt;/behaviors&gt;
      &lt;/system.serviceModel&gt;
  </code></pre>
  
  <p>Here is what my request object looks like</p>
  
  <pre><code>[DataContract]
      public class CustomerRequest : RequestBase
      {
          [DataMember]
          public int Id { get; set; }
  
          [DataMember]
          public int SiteId { get; set; }
      }
  </code></pre>
  
  <p>And the RequestBase:</p>
  
  <pre><code>[DataContract]
      public abstract class RequestBase : IRequest
      {
          #region IRequest Members
  
          [DataMember]
          public int PageSize { get; set; }
  
          [DataMember]
          public int PageIndex { get; set; }
  
          #endregion
      }
  </code></pre>
  
  <p>And my IRequest interface</p>
  
  <pre><code>public interface IRequest
      {
          int PageSize { get; set; }
          int PageIndex { get; set; }
      }
  </code></pre>
  
  <p>And I have a wrapper class around my service calls.  Here is the class.</p>
  
  <pre><code>public class MyService : IMyService
      {
          IRDCService service;
  
          public MyService()
          {
              //service = new MySite.RDCService.RDCServiceClient();
              EndpointAddress address = new EndpointAddress(APISettings.Default.ServiceUrl);
              BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.None);
              binding.TransferMode = TransferMode.Streamed;
              binding.MaxBufferSize = 65536;
              binding.MaxReceivedMessageSize = 4194304;
  
              ChannelFactory&lt;IRDCService&gt; factory = new ChannelFactory&lt;IRDCService&gt;(binding, address);
              service = factory.CreateChannel();
          }
  
          public Response&lt;Customer&gt; GetCustomer(CustomerRequest request)
          {
              return service.GetCustomer(request);
          }
  
          public Response&lt;Customer&gt; GetSiteCustomers(CustomerRequest request)
          {
              return service.GetSiteCustomers(request);
          }
      }
  </code></pre>
  
  <p>and finally, the response object.</p>
  
  <pre><code>[DataContract]
      public class Response&lt;T&gt;
      {
          [DataMember]
          public IEnumerable&lt;T&gt; Results { get; set; }
  
          [DataMember]
          public int TotalResults { get; set; }
  
          [DataMember]
          public int PageIndex { get; set; }
  
          [DataMember]
          public int PageSize { get; set; }
  
          [DataMember]
          public RulesException Exception { get; set; }
      }
  </code></pre>
  
  <p>So, when I build my CustomerRequest object and pass it in, for some reason it's hitting the server as an empty request.  Any ideas why?  I've tried upping the object graph and the message size.  When I debug it stops in the wrapper class with the 400 error.  I'm not sure if there is a serialization error, but considering the object contract is 4 integer properties I can't imagine it causing an issue.</p>