Increase message size in WCF

During the testing of a WCF service, for certain requests I started to get the following error : “The maximum string content length quota (8192) has been exceeded while reading XML data”. While on a classic asmx web service in order to specify the maximum lenght for a request with the tag <httpRuntime> in WCF things have changed a bit.

If you’re using WCF 4 you should already have noticed the huge simplification in the configuration if compared to versions 3.0/3.5. Much less stuff to care about if you want a simple service up and running, but of course if there is the need to tweak it, there’s no escape from the config, and this is the case because to increase the size of the request to the service we need to add some keys in the config. Luckly, the “Service Configuration Editor” shipped with Visual Studio make this changes almost painless.

Start by opening the configuration file within the tool and add a new service. Choose the assembly of your WCF project and select the service you want to configure:

image

Next step is to create an endpoint where we will specify the characteristics of the message:

image

Once this is done your configuration file should look like this:

<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
          <binding name="LargeContent" maxReceivedMessageSize="655360000">
            <security mode="None"/>
            <readerQuotas maxDepth="32"
                          maxStringContentLength="655360000"
                          maxArrayLength="163840"
                          maxBytesPerRead="4096"
                          maxNameTableCharCount="1638400" />
          </binding>
      </basicHttpBinding>
    </bindings>
    <services>
      <service name="CompanyName.PrintGiftMessage.Services.PrintGiftMessage">
        <endpoint address="" binding="basicHttpBinding" bindingConfiguration="LargeContent"
          name="PrintMessageEndPoint" contract="CompanyName.PrintGiftMessage.Services.IPrintGiftMessage" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
</configuration>

You are using tools (like web service studio) to test the service, you can change the value of includeExceptionDetailInFaults to true so you can get the full exception details in case of errors.




No Comments


You can leave the first : )



Leave a Reply

Your email address will not be published. Required fields are marked *