Ankit
Ankit

Reputation: 663

WCF maxBytesPerRead limit to 4096

I am using basic WCF web service in steaming mode to download files from server.

I have specified binding on server side as

     <basicHttpBinding>
        <binding name="DBUpdateServiceBinding" closeTimeout="23:59:59"
           openTimeout="23:59:59" receiveTimeout="23:59:59" sendTimeout="23:59:59"
           maxReceivedMessageSize="10067108864" messageEncoding="Mtom"
           transferMode="Streamed">
           <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
              maxBytesPerRead="8192" maxNameTableCharCount="16384" />
        </binding>
     </basicHttpBinding>

and my client side binding xml looks like

  <bindings>
     <basicHttpBinding>
        <binding name="ws" closeTimeout="23:59:59" openTimeout="23:59:59"
           receiveTimeout="23:59:59" sendTimeout="23:59:59" maxReceivedMessageSize="10067108864"
           messageEncoding="Mtom" transferMode="Streamed">
           <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
              maxBytesPerRead="8192" maxNameTableCharCount="16384" />
           <security>
              <transport realm="" />
           </security>
        </binding>
     </basicHttpBinding>
  </bindings>

I am trying to download files using

   byte[] buffer = new byte[32768];
   while (true)
   {
      int read = serverStream.Read(buffer, 0, buffer.Length);
      if (read <= 0)
         break;
      fs.Write(buffer, 0, read);
   }

Even though I have specified maxBytesPerRead="8192", max bytes that I can read in a call is only 4096.

Upvotes: 2

Views: 7544

Answers (2)

der_chirurg
der_chirurg

Reputation: 1525

It seems it is hardcoded in .NET Framework. See the following code: https://referencesource.microsoft.com/#System.Runtime.Serialization/System/Xml/XmlMtomReader.cs,1101

It is using 4096 bytes max for MTOM with XOP. Binary Encoding works fine, but is proprietary.

Upvotes: 0

Johann Blais
Johann Blais

Reputation: 9469

Unless you have very specific security requirements, you might want to consider setting the maximum sizes to Int32.MaxValue. It will save you some debugging time. Then tune it down to a more reasonable value if needed.

Upvotes: 2

Related Questions