Astoria & IIS , HTTP 400 Bad Request on Posting Large Payloads to an astoria service

We’ve seen this in the labs and also reported by our customers on the forums, If you try to post  a large amount of data to
an astoria data service , you might receive a response code Http 400 Bad Request even though the response data is valid.

An ADO.NET Data Service sits on top of Windows Communication Foundation , although most of the required plumbing is hidden away from you  ,  you can still see the effects in scenarios such as this .

Why do I get a generic 400 Bad Request in this case ?

When the request size ( including headers ) exceeds 64 K ( 65536 bytes ) , WCF chokes on the request as this request size is greater than the limit on the maximum size of the request the service can recieve and sends out a HTTP 400 Bad Request as the request is invalid.

How do I fix this ?

a)The maximum size of the request which a WCF service can process is controlled by the MaxReceivedMessageSize property on the WCF binding.
The default value is 65536 ,exceeding which you get the 400 response code.

In the web.config of the web site hosting the service , add the following node in the <System.ServiceModel> section.

  <!-- The name of the service --> 
  <service name="NorthwindService"> 
    <!-- you can leave the address blank or specify your end point URI --> 
    <endpoint address ="YourServiceEndpoint" 
              binding="webHttpBinding" bindingConfiguration="higherMessageSize" 
     contract ="System.Data.Services.IRequestHandler"> 
    <!-- configure the maxReceivedMessageSize  value to suit the max size of 
         the request ( in bytes ) you want the service to recieve--> 
    <binding name="higherMessageSize" maxReceivedMessageSize ="MaxMessageSize"/> 
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/> 

b) If hosted on IIS , the ASP.Net Request Size restriction can also cause a large request to be rejected,
       You will need to set the HttpRuntimeSection.MaxRequestLength Property.

  <httpRuntime MaxRequestLength="ValueInKiloBytes" />
How do I Identify if the reason I am seeing a 400 Bad Request  is because of the request size ?

Identify if WCF is throwing an exception under the covers which is not being surfaced to you at the HTTP level.
You can configure WCF tracing on the server-side to log the necessary information from the WCF layer.
Once you have tracing setup and you reproduced the failure , check if the log contains one or both of these exception messages.  

"The maximum message size quota for incoming messages (65536) has been exceeded. 
To increase the quota, use the MaxReceivedMessageSize property on the appropriate binding element."

"Maximum request length exceeded."

If you see that the log does contain this message , then you can be sure that the failure is because of the message size
and apply this fix accordingly.