Error – The remote server returned an unexpected response: (413) Request Entity Too Large

When I run into errors I like to write a quick blog about them because there is nothing more frustrating than getting an error which you vagely remember resolving but can’t remember why.

Hopefully the blog posts will help other people who have the same problem.

I got this error below

The remote server returned an unexpected response: (413) Request Entity Too Large. —> System.Net.WebException: The remote server returned an error: (413) Request Entity Too Large.

This error occurred from a WCF web service being consumed from within a plugin.  The problem occurred when an attachment to an email was great than 1 mb.  When I tried to pass this data in an encoded xml file to my WCF service I got the error.

The problem is all to do with the bindings of the WCF service, I work on WCF services so infrequently I often forget the intricacies and can end up tinkering with them for hours until they are right.

Configuring Bindings are a bind


This blog post (413) Request Entity Too Large is very useful for explaining the change.

The problem is caused because the default values in WCF basicHttpBinding are very small for security reasons I believe.

To resolve the problem I had to increase maxReceivedMessageSize in the web.config file for my WCF service.  There are other values you can try increasing to resolve the problem but you probably don’t need them.  I am including them in the example in case increasing the maxReceivedMessageSize doesn’t work.

Binding change to increase maxRecievedMessageSize

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

In my service endpoint I then reference my new binding

 <service name="Hosk.WebService" >
          address="" binding="basicHttpBinding" bindingConfiguration="<strong>BasicHttpBinding_largeentity</strong>" contract="Hosk.IWebService"
            <dns value="localhost"/>

Step by step and double check

Checking the fix took a lot longer because I spent 1 hour constantly changing the web.config and testing.  Nothing seemed to fix the problem, I then released I was changing the web.config in the backup folder. ARHGHGHGHGHGHGHGHGHGHG

To muddy the water I was getting errors because the WCF service was plain HTML

<security mode="Transport">
<transport clientCredentialType="Ntlm" />

What I think was happening was the transport was making the WCF service think it was SSL and then it couldn’t find the endpoint.

Usefully you can test this by putting your WCF service url into IE and it will complain.

To resolve the issue I had to create a new binding and leave the rest of the web.config and other WCF services alone.

It’s probably good practise to isolate the WCF service and use a new binding particularly when testing the effect of changing values for the WCF binding.

In the end it was a frustrating bug to fix which was made more complex and difficult by trying to quickly change the web.config

2 thoughts on “Error – The remote server returned an unexpected response: (413) Request Entity Too Large

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s