Arthur has found a really nasty bug in Microsoft’s streaming Base64 decoder as used in the WCF: Connect Bug#541494
In short: If WCF receives a message whose length is not 16+n*3 bytes (int n>=0) AND the receiver tries to read the last byte with Stream.ReadByte or Stream.ReadBoolean, a System.IO.EndOfStreamException “Unable to read beyond the end of the stream.” is thrown from the bowels of WCF. Looking at the source, Reflector’s output, and the strange length restriction, we presume that the Base64 decode tries to read beyond the underlying stream when the last byte is requested.
As an easy workaround we now pad the end of the stream with a few additional bytes which we never read. This way we do not trigger this bug.
Since MS Connect doesn’t allow downloading the Demo Project, here is a local copy.