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.

<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
 
  <system.serviceModel>

    <services>
      <service name="MyWcfService.MyService">
        <endpoint address="" binding="basicHttpBinding" bindingConfiguration="basicHttpConfiguration" contract="MyWcfService.IMyService">
        </endpoint>       
      </service> 
    </services>
   
    <bindings>     
      <basicHttpBinding>
        <binding name="basicHttpConfiguration" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
          <readerQuotas maxStringContentLength="2147483647" />
        </binding>
      </basicHttpBinding>
    </bindings>   
   
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="false"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>   
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel> 
 
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer> 
</configuration>



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

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


<system.serviceModel>
   <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_ICipherService" closeTimeout="00:01:00"
           openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
           allowCookies="false" bypassProxyOnLocal="false"
           hostNameComparisonMode="StrongWildcard"
           maxBufferSize="2147483647" maxBufferPoolSize="2147483647"
           maxReceivedMessageSize="2147483647"           messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
           useDefaultWebProxy="true">
           <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" />
           </security>
         </binding>
     </basicHttpBinding>
  </bindings>
    
  <client>
     <endpoint address="http://www.mydomain.com/wcfservice/MyService.svc"
       binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IMyService"
       contract="MyWcfServiceReference.IMyService" name="BasicHttpBinding_IMyService" />
  </client>    
</system.serviceModel>

4 comments:

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

    ReplyDelete
  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?

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

    ReplyDelete
  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!

    ReplyDelete