title: WCF - large files upload - (413) Request Entity Too Large
body: <p>Can someone please take a look at my new code ? I'm stuck on this bug for 3 days now and it's driving me crazy...</p>
  
  <p>I'm trying to have a WCF file upload, but large files are not uploaded</p>
  
  <p>I'm getting this error: "The remote server returned an unexpected response: (413) Request Entity Too Large."</p>
  
  <p>My project is built from 3 parts:</p>
  
  <ol>
  <li><p>WCF service</p></li>
  <li><p>a user control that uses the service</p></li>
  <li><p>Web site that uses the user control</p></li>
  </ol>
  
  <p>Here is my service we.config:</p>
  
  <pre><code>&lt;system.serviceModel&gt;
  
      &lt;services&gt;
        &lt;service name="AttachmentService" behaviorConfiguration=""&gt;
          &lt;endpoint name="DefaultEndPoint" address="http://localhost:54893/AttachmentService.svc"
                    behaviorConfiguration="EndpointBehaviors"
                    binding="wsHttpBinding" bindingName="AttachmentBinding"
                    contract="AttachmentsService.IAttachmentService"&gt;&lt;/endpoint&gt;
        &lt;/service&gt;
      &lt;/services&gt;
  
      &lt;behaviors&gt;
        &lt;serviceBehaviors&gt;
          &lt;behavior name=""&gt;
            &lt;serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" /&gt;
            &lt;serviceDebug includeExceptionDetailInFaults="true"/&gt;
            &lt;dataContractSerializer maxItemsInObjectGraph="2147483647" /&gt;
          &lt;/behavior&gt;
        &lt;/serviceBehaviors&gt;
  
        &lt;endpointBehaviors&gt;
          &lt;behavior name="EndpointBehaviors"&gt;
            &lt;dataContractSerializer maxItemsInObjectGraph="2147483647" /&gt;
          &lt;/behavior&gt;
        &lt;/endpointBehaviors&gt;
      &lt;/behaviors&gt;
  
      &lt;bindings&gt;
        &lt;basicHttpBinding&gt;
          &lt;binding name="AttachmentBinding" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" maxBufferPoolSize="2147483647" receiveTimeout="00:01:00" sendTimeout="00:01:00"
                   textEncoding="utf-8" openTimeout="00:01:00"&gt;
            &lt;readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/&gt;
            &lt;security mode="None" /&gt;
          &lt;/binding&gt;
        &lt;/basicHttpBinding&gt;
      &lt;/bindings&gt;
      &lt;serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" /&gt;
    &lt;/system.serviceModel&gt;
  </code></pre>
  
  <p>Here is my user control app.config:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;configuration&gt;
    &lt;system.serviceModel&gt;
      &lt;bindings&gt;
        &lt;basicHttpBinding&gt;
          &lt;binding name="BasicHttpBinding_IAttachmentService" maxBufferSize="2147483647" maxBufferPoolSize="524288" maxReceivedMessageSize="2147483647"&gt;
            &lt;readerQuotas maxDepth="128" maxStringContentLength="2147483647" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" /&gt;
          &lt;/binding&gt;
        &lt;/basicHttpBinding&gt;
      &lt;/bindings&gt;
      &lt;client&gt;
        &lt;endpoint address="http://localhost:54893/AttachmentService.svc"
            binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IAttachmentService"
            contract="AttachmentsService.IAttachmentService" name="BasicHttpBinding_IAttachmentService" /&gt;
      &lt;/client&gt;
    &lt;/system.serviceModel&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>Here is my UI web.config:</p>
  
  <pre><code>&lt;system.webServer&gt;
      &lt;security&gt;
        &lt;requestFiltering&gt;
          &lt;requestLimits maxAllowedContentLength="2147483647" /&gt;
        &lt;/requestFiltering&gt;
      &lt;/security&gt;
      &lt;serverRuntime enabled="true" uploadReadAheadSize="2147483647" maxRequestEntityAllowed="2147483647" /&gt;
    &lt;/system.webServer&gt;
  
    &lt;system.serviceModel&gt;
      &lt;bindings&gt;
        &lt;basicHttpBinding&gt;
          &lt;binding name="BasicHttpBinding_IAttachmentService" maxBufferSize="2147483647" maxBufferPoolSize="524288" maxReceivedMessageSize="2147483647"&gt;
            &lt;readerQuotas maxDepth="128" maxStringContentLength="2147483647" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" /&gt;
          &lt;/binding&gt;
        &lt;/basicHttpBinding&gt;
      &lt;/bindings&gt;
      &lt;client&gt;
        &lt;endpoint address="http://localhost:54893/AttachmentService.svc"
            binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IAttachmentService"
            contract="AttachmentsService.IAttachmentService" name="BasicHttpBinding_IAttachmentService" /&gt;
      &lt;/client&gt;
    &lt;/system.serviceModel&gt;
  </code></pre>
  
  <p>And here is how I've implemented the reference call:</p>
  
  <pre><code>public void UploadFile(string serviceUrl,decimal maxFileSize, AttachmentFileParams fileParams, Stream file)
  {
      AttachmentServiceClient client = null;
      try
      {
          if (file.Length &gt; (maxFileSize * 1024)) //maxFileSize is defined in KB and file.Length is in Bytes
                      return Serialization.ConvertToJson(new { IsError = true, ErrorMessage = Constants.Messages.MaxFileSizeExceeded + maxFileSize });
  
                  string requestUrl = string.Format("{0}/UploadFile", serviceUrl);
                  string jsonFile = Serialization.ConvertToJson(fileParams);
                  byte[] jsonFileBytes = Encoding.UTF8.GetBytes(jsonFile);
                  byte[] len = BitConverter.GetBytes(jsonFileBytes.Length);
  
                  using (client = new AttachmentServiceClient())
                  {
                      client.UploadFile(file);
                  }
      }
      catch (Exception ex)
      { }
      finally
      {
          try
                  {
                      if (client.State != System.ServiceModel.CommunicationState.Closed)
                          client.Close();
                  }
                  catch
                  {
                      client.Abort();
                  }
      }
  } 
  </code></pre>
  
  <p>Is there anything I'm missing ?</p>