title: System.ServiceModel.ProtocolException: The remote server returned an unexpected response: (413) Request Entity Too Large
body: <p>I'm working on a project. The website was built on MVC, <a href="http://www.example.com" rel="nofollow">http://www.example.com</a> , recently, another developer added a windows web service (not WCF) to the project, <a href="http://www.example.com/WebServices/upload.asmx" rel="nofollow">http://www.example.com/WebServices/upload.asmx</a> . It is supposed to for uploading images to server. The Route is added to RouteConfig, so it works:</p>
  
  <pre><code>routes.IgnoreRoute("WebServices/*/{resource}.aspx/{*pathInfo}");
  </code></pre>
  
  <p>However, I noticed that it works for small size files. When the file size reaches certain point, i.e. > 1MB (not sure the exact #, but 650KB file works and 1.1MB file fails). The error message is:</p>
  
  <pre><code>System.ServiceModel.ProtocolException was caught
    HResult=-2146233087
    Message=The remote server returned an unexpected response: (413) Request Entity Too Large.
    Source=mscorlib
    StackTrace:
      Server stack trace: 
         at System.ServiceModel.Channels.HttpChannelUtilities.ValidateRequestReplyResponse(HttpWebRequest request, HttpWebResponse response, HttpChannelFactory`1 factory, WebException responseException, ChannelBinding channelBinding)
         at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
         at System.ServiceModel.Channels.RequestChannel.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.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)
         at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData&amp; msgData, Int32 type)
         at SyncDatabase.eyephoto.com.upload.UploadSoap.UploadImage(UploadImageRequest request)
         ....
    InnerException: System.Net.WebException
         HResult=-2146233079
         Message=The remote server returned an error: (413) Request Entity Too Large.
         Source=System
         StackTrace:
              at System.Net.HttpWebRequest.GetResponse()
              at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
         InnerException: 
  </code></pre>
  
  <p>I searched 2 days, all the solutions I could find did not work. Now, on the site, in web.config, I have:</p>
  
  <pre><code>&lt;httpRuntime maxRequestLength="40960000" executionTimeout="300"  /&gt;
  ...
  
  &lt;system.serviceModel&gt;
      &lt;client /&gt;
      &lt;bindings&gt;
        &lt;basicHttpBinding&gt;
          &lt;binding closeTimeout="00:10:00"
            openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" maxBufferPoolSize="2147483647" maxBufferSize="2147483647"
            maxReceivedMessageSize="2147483647"  &gt;
            &lt;readerQuotas maxDepth="2000000" maxStringContentLength="2147483647"
              maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" /&gt;
  
          &lt;/binding&gt;
        &lt;/basicHttpBinding&gt;
      &lt;/bindings&gt;
  &lt;/system.serviceModel&gt;
  </code></pre>
  
  <p>We use a desktop application to consume the web service. In the app.config, I have:</p>
  
  <pre><code>&lt;system.serviceModel&gt;
      &lt;bindings&gt;
        &lt;basicHttpBinding&gt;
          &lt;binding name="UploadSoap1" maxBufferPoolSize="2147483647" maxBufferSize="2147483647"
            maxReceivedMessageSize="2147483647" closeTimeout="00:10:00"
            openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" &gt;
  
            &lt;readerQuotas maxDepth="2000000" maxStringContentLength="2147483647"
              maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" /&gt;
  
            &lt;security mode="None" /&gt;
          &lt;/binding&gt;        
        &lt;/basicHttpBinding&gt;
      &lt;/bindings&gt;
  
      &lt;client&gt;
        &lt;endpoint address="http://www.example.com/WebServices/Upload.asmx"
          binding="basicHttpBinding" bindingConfiguration="UploadSoap1"
          contract="mysite.com.upload.UploadSoap" name="UploadSoap1" /&gt;
      &lt;/client&gt;
   &lt;/system.serviceModel&gt;
  </code></pre>
  
  <p>The weird thing is: 600KB file works, but if file size > 1MB, it has this error. BUT, nowhere from these configuration I can find any relation with these numbers.</p>
  
  <p>Anyone knows what the problem is? Any suggestions? Could it because of route in MVC?</p>
  
  <p>Thanks</p>