Sunday, August 28, 2011

WCF - how to fix maxStringContentLength 8192 limit

If you use WCF service from Web application or desktop application, you might have the following error if you send/receive big data.

The maximum string content length quota (8192) has been exceeded while reading XML data.

This error occurs since WCF by default sets 8K limit to prevent DoS attack. However, there are many business needs to increase this limit. And it can be adjusted by change some values in .config file (web.config for ASP.NET/WCF or app.config for Windows application).  There are many sites that talked about this topic. For example, you can refer to this discussion (The maximum string content length quota (8192) has been exceeded). I just decided to summarize this topic (well, primarily for myself) since those sites didn't work for me somehow, at least for my case.

Here is a simple example of increasing the 8K limit. Basically both WCF side web.config and client side web.config (let's assume ASP.NET, but the same applies to app.config) should be changed accordingly.

(1) Server side (WCF web.config)

In web.config for WCF, you need to increase maxBufferSize, maxReceivedMessageSize, maxStringContentLength. Below example increased the value to 2GB, but it purely depends on business need.

    <compilation debug="true" targetFramework="4.0" />

      <service name="MyWcfService.MyService">
        <endpoint address="" binding="basicHttpBinding" bindingConfiguration="basicHttpConfiguration" contract="MyWcfService.IMyService">
        <binding name="basicHttpConfiguration" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
          <readerQuotas maxStringContentLength="2147483647" />
          <serviceMetadata httpGetEnabled="false"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
    <modules runAllManagedModulesForAllRequests="true"/>

2) Client side (ASP.NET web.config)

In client side web.config, you need to increase maxBufferSize, maxBufferPoolSize, maxReceivedMessageSize, maxStringContentLength.

        <binding name="BasicHttpBinding_ICipherService" closeTimeout="00:01:00"
           openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
           allowCookies="false" bypassProxyOnLocal="false"
           maxBufferSize="2147483647" maxBufferPoolSize="2147483647"
           maxReceivedMessageSize="2147483647"           messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
           <readerQuotas maxDepth="32" maxStringContentLength="2147483647"
                  maxArrayLength="16384" maxBytesPerRead="4096"
                  maxNameTableCharCount="16384" />
           <security mode="None">
              <transport clientCredentialType="None" proxyCredentialType="None"
                           realm="" />
               <message clientCredentialType="UserName" algorithmSuite="Default" />
     <endpoint address=""
       binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IMyService"
       contract="MyWcfServiceReference.IMyService" name="BasicHttpBinding_IMyService" />


  1. this is wrong block which you are saying is wrong because i am trying it but it same error is continue occuring

  2. Thank you. This post helped me solve my problem. I still don't understand why these settings need to be on the server side, but when I put them there, it works.

    Prior to this, I didn't even have or nodes in my server-side web.config file. Is the default value of 8192 hardcoded into the guts of WCF, or is it in a machine.config file or something?

  3. My understanding is that 8192 is the default value(hardcoded) of maxStringContentLength in WCF.

  4. Thank you very much for giving the entirely content of clientside and serverside configuration. Many others use to publish only fragments, that is not very helpful. Excellent post!