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


WCF with no service xml or endpoints? What is this magic?

I had to write a WCF service for a project and I hadn’t created a WCF service for a while.

I was trying to write mine to fit in with the rest of the projects but I was totally confused because I was looking at the other WCF services in the project and looking to copy the shell and modify (like all good developers do:-))

I could see there was a SVC file, I could see the code but in the web.config there was no service xml section for the WCF service.

I trying to look at the url for the SVC to see if I could see the wsdl but when I did I got the message no endpoint found.

no endpoint found



Sometimes as a developer, when you haven’t worked on an area for a while, you have a horrible feeling they have changed the way it worked to some completely new method and framework a bit like the way web services were replaced with WCF.   but this wasn’t the case, it was CRM 2011 project (not that makes any difference calling a WCF service).

The most puzzling aspect was the WCF code was being called (I called it myself to make sure it worked) from a Javascript form.

I asked a developer and he said it must be a factory service, I must have had a completely blank look on my face because he then offered to show me.  A good tip is if you don’t understand something ask someone to point you in the right direction because you can save yourself days of puzzling.

He said right click on the SVC file in Visual Studio and choose View Markup
You will see this
mine was like this
<%@ ServiceHost Language=”C#” Debug =”true” Service=”Hosk.HoskRibbonService” CodeBehind=”HoskRibbonService.svc.cs %>
he said change it to this
<%@ ServiceHost Language=”C#” Debug =”true” Service=”Hosk.HoskRibbonService”  Factory=”System.ServiceModel.Activation.WebServiceHostFactory” %>

I removed the service xml section in the web.config and then as if by magic it worked and I was able to call the wcf service using jsonp call (cross domain) in javascript.

Not only had I never heard of using WebServiceHostFactory to create a WCF but I had never come across it in any of the code I have worked with.

I was interested to learn a bit more about it,  It’s a way to dynamically create services and it automatically creates the endpoints for the service and was added in .NET 3.5


Here is MSDN class


this flashcard gives a good quick explanation


What is the purpose of the WebServiceHostFactory class and how is it used?

When hosting a web service in IIS, the WebServiceHostFactory class can be used to automatically configure the endpoints for the service. Endpoints will be configured to use WebHttpBinding and WebHttpBehavior. The WebServiceHostFactory class will automatically create a WebServiceHost class when the web service is activated. The following example demonstrates how to reference the WebServiceHostFactory in a .svc file:

<%@ ServiceHost 
    Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>



Here are some simple tutorials if you want to learn more


Picture from