
This exception is thrown by a WCF web service published on IIS if you try to download by soap a serialized object (an image for instance) which in Base64 is longer than 216-1 bytes.
To avoid the exception you need to change the Client Application configuration, not the server configuration. In my case it is a windows forms application built to test the service so I have to change the app.config, while in a web application I would have to change the web.config.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0,Profile=Client" />
</startup>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_ISoapWS" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="65535" maxBufferPoolSize="500000" maxReceivedMessageSize="65535"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="64" maxStringContentLength="65535" maxArrayLength="65535"
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://localhost/MyWebservice/ServiceClass.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ISoapWS"
contract="SoapWsTest.ISoapWS" name="BasicHttpBinding_ISoapWS" />
</client>
</system.serviceModel>
</configuration>
On the above configuration you need to change the following attributes
- maxBufferSize="65535"
- maxBufferPoolSize="500000"
- maxReceivedMessageSize="65535"
- maxStringContentLength="65535"
- maxArrayLength="65535"
To a value you think is large enough for your needs. Pay attention if you don’t change them all, you will receive a further exception for each parameter not large enough. The value depends on you so if you need to read images or PDF files you can raise the values above the millions, because Base64 is one of the less optimized data exchange formats.
Yesterday I’ve blogged a memo showing some unusual conversions due to a Checksum routine I wrote for Alberto. Today I discovered that the real checksum was a little more cumbersome than I thought so I publish a few more conversions I made, just to help who will have the “luck” to bang on this kind of things.
Get a sub-array of a byte array
byte[] sourceArray;
... //Initialization with a file read or a serial port read
byte[] destArray = new byte[desiredSublength]
Array.Copy(sourceArray, StartIndex, destArray, DestStartIndex, Length);
Conversion of the bytes in correspondant ascii chars
char[] chars = System.Text.Encoding.UTF8.GetChars(destArray);
Get a string from a portion of the characters array
string subValue = new string(charsArray, startIndex, length);
Convert a string representing an hex number into a byte
byte myHexByte = Convert.ToByte("1C", 16);
Convert a byte into an HexNumber String
string hexString = Convert.ToString(myByteValue, 16);
Be sure the string has the correct Case
string lowerHex = hexString.ToLower();
string upperHex = hexString.ToUpper();
That’s because db and DB are the same as a numeric value, but become different if you convert the string to a char array
char[] hexLetters = upperHex.ToCharArray();
And then you convert the char array back to a byte array
byte[] bytesFromHexLetters = new byte[hexLetters.Length];
for (int i = 0; i < hexLetters.Length; i++)
{
bytesFromHexLetters [i] = Convert.ToByte(chars[i]);
}
Try it with upper and lower case hex letters and you’ll see. And all this in a single checksum check routine
here we call this kind of things Simple Business Complication Office (sometimes it is a very crowded office).