HTTP 400 Bad Request Error with WCF Service when message size exceeds 64K

I have a .NET 4.0 WCF service. If I send a soap message larger than 64K, then I get “The remote server returned an unexpected response: (400) Bad Request” Error. It works fine all the way until I go over the 64K message size. I have read the many posts out there regarding what to do for this error, and as far as I can tell, I have the correct web.config values, but I still get the error. Below are the settings in my web.config. Anything I am missing? This occurs when communicating both to my local ASP.NET VS server and a remote Windows 2008 R2 IIS server. Is there a way to verify or log the maxReceivedMessageSize settings, etc. that are in the service binding in real-time or in the debugger? The service is hosted in MVC if that makes any difference.

<httpRuntime  maxRequestLength="50000000" />

...

<bindings>
  <basicHttpBinding>

    <binding name="IpsApiBinding" receiveTimeout="00:15:00" sendTimeout="00:05:00" maxReceivedMessageSize="40000000">
      <readerQuotas maxDepth="5000000" maxStringContentLength="50000000"
        maxArrayLength="50000000" maxBytesPerRead="50000000" />
    </binding>

  </basicHttpBinding>
</bindings>


<services>

  <service behaviorConfiguration="ApiBehavior" name="IPSApi.IpsApi">
    <endpoint behaviorConfiguration="endpointBehavior" binding="basicHttpBinding"
      bindingConfiguration="IpsApiBinding" name="IPSApi.IpsApi"
      contract="IPSApi.IIPSApi" />
  </service>

</services>

<behaviors>

  <endpointBehaviors>
    <behavior name="endpointBehavior">
      <dataContractSerializer maxItemsInObjectGraph="6553600" />
      <callbackDebug includeExceptionDetailInFaults="true" />
    </behavior>
  </endpointBehaviors>

  <serviceBehaviors>
    <behavior name="ApiBehavior">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
  </serviceBehaviors>

</behaviors>

<serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true"  />

On the client side, the stack track is showing…

Server stack trace: at System.ServiceModel.Channels.HttpChannelUtilities.ValidateRequestReplyResponse(HttpWebRequest request, HttpWebResponse response, HttpChannelFactory factory, WebException responseException, ChannelBinding channelBinding) at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout) at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout) at System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message message, TimeSpan timeout) at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout) at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs) at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation) at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message) Exception rethrown at [0]: at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) …

Streaming binary data to WCF rest service gives Bad Request (400) when content length exceeds 64k

I have a WCF service that takes a stream: [ServiceContract] public class UploadService : BaseService { [OperationContract] [WebInvoke(BodyStyle=WebMessageBodyStyle.Bare, Method=WebRequestMethods.Http.

WCF ProtocolException : Bad Request 400 (related to http message size)

I am trying to pass a base64 encoded string over WCF using WebHttpBinding. I get the cryptic error ‘Bad Request 400’ that the server is not responding. I know its related to the size of the string bec

HTTP 400 Bad Request when calling WCF Service Operation?

When I call http://localhost/TestService.svc/GetColors, I get a Http (400) Bad Request. When I run this in fiddler, I also get Bad Request. When I invoke the service through the wcf test client, it wo

HTTP 400 Bad Request – CORS enabled WCF Service

I am trying to use CORS on WCF for cross domain calls to the service. I have most of the things working, but when I try to call the function it always gives me a error -HTTP 400 Bad Request I used Fid

Getting 400 http bad request error while accessing WCF rest service using GET

the Exact error is System.Net.WebException: The Remote server returned an Error: (400) Bad Request. at system.Net.HttpWebRequest.GetResponse()i have a WCF rest service hosted in IIS..I have created a

HTTP 400 Bad Request when PUTting data to WCF RESTful service – Low maximum request size?

I have a WCF method that accepts arbitrary binary data which the service will save to a file on the filesystem. Here is the WCF method: [OperationContract] [WebInvoke( Method=PUT, UriTemplate=/prod

WCF method being called via webclient – HTTP 400 – Bad Request

I am consuming a WCF service and receiving the error message The remote server returned an error: (400) Bad Request. when consuming via WebClient public class WebClientService : WebClient … base.C

wcf service json 400 Bad Request

I get a 400 Bad Request error when I try to call WCF service from client side using ajax. Following is my code, [OperationContract] [WebInvoke(Method = POST, BodyStyle = WebMessageBodyStyle.Wrapped,

400 Bad request when trying to contact WCF service using XHR

I’m making a WCF web service that will return a json object, but I keep getting a 400 bad request error when I try to make an AJAX call: OPTIONS http://localhost:55658/WebServiceWrapper.svc/GetData?_=

Bad Request – Request Too Long HTTP Error 400. The size of the request headers is too long

Some of my users are getting the following error sometimes when they request some of the pages of my site: Bad Request – Request Too Long HTTP Error 400. The size of the request headers is too long It

Answers

I had a similar issue and also had to increase two additional fields on the binding that I do not see in your code… maxBufferSize and maxBufferPoolSize. Not sure if this would be your issue but I know that the default for these is roughly 64k.

Sample Web.config settings:

<binding name="HTTPNoneBinding" closeTimeout="00:01:00" openTimeout="00:01:00"
      receiveTimeout="00:10:00" sendTimeout="00:10:00" allowCookies="false"
      bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
      maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"
      messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
      useDefaultWebProxy="true">
      <readerQuotas maxDepth="32" maxStringContentLength="2147483647"
        maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />

I would suggest for to enable Tracing on your Service to determine the exact cause of the Bad Request. If it is because of the message size the framework would tell you clearly on which property needs to be set. In order to enable tracing follow this link

I figured out my problem. Here are the details. I am hosting my WCF service in a MVC app. The WCF service is in a separate DLL. I had the configuration settings in the web.config thinking that it would read its config from there. Come to find out, if in a separate DLL, then it will not use the web.config settings. It was just using the default bindings, etc.

To get it to read the configuration in web.config, I used the method that was suggested in http://blogs.msdn.com/b/dotnetinterop/archive/2008/09/22/custom-service-config-file-for-a-wcf-service-hosted-in-iis.aspx . I created my own custom ServiceHostFactory and ServiceHost. In the ServiceHostFactory.CreateServiceHost I create an instance of my ServiceHost (derived class). In my ServiceHost derived class, I take over the ApplyConfiguration and load the config from the web.config.

It works!