Where to put MaxReceivedMessageSize property in WCF service's web.config file?
I need to change my web.config file and add the MaxReceivedMessageSize property in my web.config - but where?
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.
<?xml version="1.0"?> <configuration> <system.web> <compilation debug="false"><assemblies><add assembly="System.Data.Entity, Version=126.96.36.199, Culture=neutral, PublicKeyToken=b77a5c561934e089" /></assemblies></compilation> </system.web> <system.serviceModel> <behaviors> <serviceBehaviors> <behavior> <serviceMetadata httpGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="false" /> </behavior> </serviceBehaviors> </behaviors> <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> </system.serviceModel> <system.webServer> <modules runAllManagedModulesForAllRequests="true" /> </system.webServer>
You need to define a binding configuration for the binding you want to use and then you need to define your services (on the server-side) and clients (on the client side) to use that binding and binding configuration:
<system.serviceModel> <bindings> <!-- pick whichever binding you want .... --> <basicHttpBinding> <!-- binding configuration with a name --> <binding name="ExtendedMaxSize" maxBufferSize="999999" maxReceivedMessageSize="999999" /> </basicHttpBinding> </bindings> <services> <service name="Yournamespace.YourServiceClass" behaviorConfiguration="..."> <!-- define endpoint with your binding and the name of the binding configuration that you have defined just above --> <endpoint address="" binding="basicHttpBinding" bindingConfiguration="ExtendedMaxSize" contract="Yournamespace.IYourServiceContract" /> </service> </services>
To help those who may end up here like I did. I cannot add to the comments above yet (Usually someone already has the answers long before I have the problem), so I have to add an answer.
I have an MVC 4 app, and I suspect the initial sample above is from the web.config of the actual WCF service project. One of the comments mentions they suspect it is an MVC 4 app and the default config settings.
But how do you fix the problem? From more research, it appears that the change actually needs to be made to the web.config for the CLIENT, in other words, the web config for the project with the REFERENCE to the WCF service. You will find it is much easier to make the change there. That version of the web.config will actually resemble what you are looking for.
That worked easily for me and fixed my issue.
- No need, contrary to often claimed, to set on the server.
- Contrary to what MSDN is saying, it is not enough to set the limit on the transport binding element. Need to set on binding itself too. For example:
var targetBinding = new BasicHttpsBinding(); targetBinding.MaxReceivedMessageSize = MaxWcfMessageSize; targetBinding.MaxBufferPoolSize = MaxWcfMessageSize; targetBinding.MaxBufferSize = MaxWcfMessageSize; var targetBindingElements = targetBinding.CreateBindingElements(); var httpsBindElement = targetBindingElements.Find<HttpsTransportBindingElement>(); httpsBindElement.MaxReceivedMessageSize = MaxWcfMessageSize; httpsBindElement.MaxBufferPoolSize = MaxWcfMessageSize; httpsBindElement.MaxBufferSize = MaxWcfMessageSize; TextMessageEncodingBindingElement tmbebe = targetBindingElements.Find<TextMessageEncodingBindingElement>(); tmbebe.ReaderQuotas.MaxArrayLength = MaxWcfMessageSize;