Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

You may experience maximum bytes per read quota problems when using binary encoding with WCF 3.0 and WCF 3.5


Symptoms

Imagine you have a Windows Communication Foundation (WCF) Service running framework version 3.0 or 3.5 using binary encoding and streamed transfer mode. You provide quota information for MaxBytesPerReady and you may receive the following error when receiving data of a specific size:
"Error in deserializing body of reply message for operation '<operation-name>'. The 'maximum bytes per Read operation' quota (<quota-value>) has been exceeded while reading XML data. Long element start tags (consisting of the element name, attribute names and attribute values) may trigger this quota. This quota may be increased by changing the MaxBytesPerRead property on the XmlDictionaryReaderQuotas object used when creating the XML reader."

Larger and smaller amount of data will process without errors and only a few combinations will fail. If you change MaxBytesPerRead quotas you will succeed with the values you have failed initially and fail with different data sizes.

You cannot reproduce this problem using an encoding that is not binary or if the transfer mode is buffered.

↑ Back to the top


Cause

The underlying XmlBinaryReader (used by Windows Communication Foundation for binary encoding) expects that starting tags size is accounted for when considering the MaxBytesPerRead quota. This causes an error when deserializing the body of the WCF message in some situations. This issue has been fixed in later versions of the framework starting with WCF 4.0.

↑ Back to the top


Resolution

Microsoft is aware of the issue in WCF 3.0 and WCF 3.5 and suggests these workarounds below by order of preference. 

Workaround 1: Port the service to WCF 4.0
If all possible, the best way to resolve the issue is to port the application to WCF 4.0. The issue does not exist in WCF 4.0 RTM and beyond.

Workaround 2: Use an encoding different than binary
This issue happens at XmlBinaryReader level only which is part of binary encoding. No other encoding will cause the issue.

Workaround 3: Use buffered transfer mode
The issue only happens in streamed transfer mode with binary encoding.

Workaround 4: Change the MaxBytesPerRead quota to match MaxBufferSize
This solution must be considered carefully to avoid a large MaxBytesPerRead value that may cause the application to be more vulnerable to attacks where the attacker sends large start element tags

↑ Back to the top


More Information

There is no hotfix for this issue in .NET 3.0 and .NET 3.5.

↑ Back to the top


Keywords: kb

↑ Back to the top

Article Info
Article ID : 2735572
Revision : 1
Created on : 1/7/2017
Published on : 7/17/2012
Exists online : False
Views : 107