notes of a dotnet developer

Main menu

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 ona 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:

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

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

<?xmlversion="1.0"?><configuration><system.web><compilationdebug="true"targetFramework="4.0"/></system.web><system.serviceModel><bindings><basicHttpBinding><bindingname="LargeContent"maxReceivedMessageSize="655360000"><securitymode="None"/><readerQuotasmaxDepth="32"maxStringContentLength="655360000"maxArrayLength="163840"maxBytesPerRead="4096"maxNameTableCharCount="1638400"/></binding></basicHttpBinding></bindings><services><servicename="CompanyName.PrintGiftMessage.Services.PrintGiftMessage"><endpointaddress=""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 --><serviceMetadatahttpGetEnabled="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 --><serviceDebugincludeExceptionDetailInFaults="false"/></behavior></serviceBehaviors></behaviors><serviceHostingEnvironmentmultipleSiteBindingsEnabled="true"/></system.serviceModel><system.webServer><modulesrunAllManagedModulesForAllRequests="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.