Unable to transfer large file (binary) using WCF from Web Application


  • Hi,

    I have found similar topics in this very forum and also online, but none of them could give me a solution or give an explanation about where I am going wrong. All the solutions point to changing the readerQuota size or increasing the other attributes of the config settings.

    It will be of great help if someone could point out my mistakes and I am facing my deadlines around the corner. I have a web application which has uses the WCF service to upload a file on to the DataBase. I am streaming the file in Byte Array and passing it over the function. Both service and the application will be hosted on IIS. Please the code snippet which calls the WCF Service function : CreateAttachments

    Dim fInfo As New System.IO.FileInfo(dtAttachments.Rows(iCounter)("File Location").ToString())
    Dim numBytes As Long = fInfo.Length
    Dim fStream As New System.IO.FileStream(dtAttachments.Rows(iCounter)("File Location").ToString(), IO.FileMode.Open, IO.FileAccess.Read)
    Dim br As New System.IO.BinaryReader(fStream)
    Dim data As Byte() = br.ReadBytes(CInt(numBytes))
    iStatus = proxy.CreateAttachements(iTicketID, dtAttachments.Rows(iCounter)("File Name").ToString(), data)

    I am getting the following error on executing the final statement of the code shown above:

    "The formatter threw an exception while trying to deserialize the message: Error in deserializing body of request message for operation The maximum array length quota (16384) has been exceeded while reading XML data. This quota may be increased by changing the MaxArrayLength property on the XmlDictionaryReaderQuotas object used when creating the XML reader".

    Here is the WCF Service Config File.

        <binding name="HttpBinding_MTOM" messageEncoding="Mtom" transferMode="Streamed" maxBufferSize="10485760" maxReceivedMessageSize="67108864">
         <security mode="None">
           <transport clientCredentialType="None" />

    <service name="XXXDataLayer.DataLayerService" behaviorConfiguration="XXXDataLayer.Service1Behavior">
     <endpoint address="" binding="basicHttpBinding" bindingConfiguration="HttpBinding_MTOM" contract="XXXDataLayer.IXXXDataLayer" />
     <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />

         <behavior name="XXXDataLayer.Service1Behavior">
         <serviceMetadata httpGetEnabled="True" />
         <serviceDebug includeExceptionDetailInFaults="True" />

    <httpRuntime maxRequestLength="2097151" />

    And here is the Web Application Config file which consumes the WCF Service.

    <binding name="BasicHttpBinding_IXXXDataLayer"
    maxBufferSize="10485760" maxBufferPoolSize="67108864" maxReceivedMessageSize="67108864"
    messageEncoding="Mtom" transferMode="Streamed">
    <readerQuotas maxDepth="64" maxStringContentLength="214748364" maxArrayLength="214748364"
    maxBytesPerRead="4096" maxNameTableCharCount="16384" />
    <security mode="None">
    <transport clientCredentialType="None" proxyCredentialType="None" realm="" />
    <endpoint address="http://XXX/XXXWebHelpRequestRemoting/DataLayerService.svc"
    binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IXXXDataLayer"
    contract="XXXDataLayer.IXXXDataLayer" name="BasicHttpBinding_IXXXDataLayer" />

    I have run this application in both visual studio and also from a website hosted on IIS. In both cases I seem to have problems. Kindly let me know the mistake that I have made or if you have any alternate solution for the given situation.

    Thanks and regards,


    Wednesday, August 12, 2009 5:22 AM


All replies

  • Can you try adding the <readerQuotas> section to the binding on the service file as well?
    Wednesday, August 12, 2009 6:10 AM
  • Carlos,

    That worked! Thanks a ton. I noticed that if I delete the service reference and add it back, the changes that I have made vanishes. Is there any way to not allow that???

    Thanks again.


    Wednesday, August 12, 2009 5:03 PM
  • Great that it worked!
    About the service reference, not really, once you update / delete+add the service reference, it will download the metadata again from the server to recreate the config file. A couple of options is to save the original app.config, then replace it after updating the service reference, or you can use the svcutil.exe tool (, in which you can specify the /noconfig option (so it won't generate config).
    Wednesday, August 12, 2009 5:09 PM