Site icon port135.com

WCF service shows 413 Request Entity Too Large error if uploading files over 64 KB

In another post, I mentioned the common way of solving (413) Request Entity Too Large error. However, If you are using WCF and seeing this error, you may need to go extra mile to solve this issue.

While trying to upload files that are bigger than 64 KB, we came across this error. Unfortunately, the usual fix (increasing uploadReadAheadSize and maxRequestEntityAllowed values) didn’t help.

In the dump file, we saw this error:

Exception Type : System.ServiceModel.ProtocolException
Message: 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.

As the error above mentions, this issue occurs because the request size is bigger than the MaxReceivedMessageSize for WCF.

Solution for WCF 413 Request Entity Too Large error

In order to solve this problem, you will need to increase MaxReceivedMessageSize value for your web service. However, this may get tricky and cause you to spend a lot of time because there are two different MaxReceivedMessageSize parameters:

Resources you will find will recommend editing your web.config like the following example:

<basicHttpBinding>
   <binding name="basicHttpBinding_Portal" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" maxBufferPoolSize="2147483647">
       <readerQuotas maxBytesPerRead="2147483647" maxArrayLength="2147483647" maxStringContentLength="2147483647" maxDepth="2147483647" maxNameTableCharCount="2147483647"/>
    </binding>
</basicHttpBinding>

This configuration will increase the MaxReceivedMessageSize in System.ServiceModel.Configuration.BasicHttpBindingElement

You should increase the MaxReceivedMessageSize in System.ServiceModel.Channels.HttpTransportBindingElement as well. Here is the way to do it in web.config:

<customBinding>
   <binding closeTimeout="00:10:00" openTimeout="00:10:00" sendTimeout="00:10:00">
      <httpTransport maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" useDefaultWebProxy="true" transferMode="Buffered" />
   </binding>
</customBinding>

You can also do it through the code as explained in this document.

Note 1: For some cases, removing the name from basicHttpBinding also fixes the issue (name=”basicHttpBinding_Portal”)

Note 2: While trying to figure out this issue, your application may further break (not showing a table etc.) after adding maxBufferSize to your web.config. This happens because maxBufferSize is not supported for wsHttpBinding. Event ID 3 “ServiceActivationException: Unrecognized attribute ‘maxBufferSize'” error mentions this issue.

Exit mobile version