Ron
Ron

Reputation: 988

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) ...

Upvotes: 3

Views: 14605

Answers (3)

Ron
Ron

Reputation: 988

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!

Upvotes: 3

Rajesh
Rajesh

Reputation: 7876

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

Upvotes: 0

Borophyll
Borophyll

Reputation: 1119

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" />

Upvotes: 0

Related Questions