Tuesday, February 8, 2011

WCF - How to accept long strings as parameters

I have a simple web service, it takes 2 parameters one is a simple xml security token, the other is usually a long xml string. It works with short strings but longer strings give a 400 error message. maxMessageLength did nothing to allow for longer strings.

  • You should remove the quotas limitations as well. Here is how you can do it in code with Tcp binding. I have added some code that shows removal of timeout problems because usually sending very big arguments causes timeout issues. So use the code wisely... Of course, you can set these parameters in the config file as well.

            NetTcpBinding binding = new NetTcpBinding(SecurityMode.None, true);
    
            // Allow big arguments on messages. Allow ~500 MB message.
            binding.MaxReceivedMessageSize = 500 * 1024 * 1024;
    
            // Allow unlimited time to send/receive a message. 
            // It also prevents closing idle sessions. 
            // From MSDN: To prevent the service from aborting idle sessions prematurely increase the Receive timeout on the service endpoint's binding.’
            binding.ReceiveTimeout = TimeSpan.MaxValue;
            binding.SendTimeout = TimeSpan.MaxValue;
    
            XmlDictionaryReaderQuotas quotas = new XmlDictionaryReaderQuotas();
    
            // Remove quotas limitations
            quotas.MaxArrayLength = int.MaxValue;
            quotas.MaxBytesPerRead = int.MaxValue;
            quotas.MaxDepth = int.MaxValue;
            quotas.MaxNameTableCharCount = int.MaxValue;
            quotas.MaxStringContentLength = int.MaxValue;
            binding.ReaderQuotas = quotas;
    
  • After the answer on quotas I just did all that in the web.config

    <bindings>
      <wsHttpBinding>
        <binding name="WSHttpBinding_IPayroll" maxReceivedMessageSize="6553600">
          <security mode="None"/>
          <readerQuotas maxDepth="32" 
                        maxStringContentLength="6553600" 
                        maxArrayLength="16384"
                        maxBytesPerRead="4096" 
                        maxNameTableCharCount="16384" />
        </binding>
      </wsHttpBinding>
    </bindings>
    
    Tuoski : How long was your string? And what contract do you use? I have MessageContract and the string is 64k char long.
    From

0 comments:

Post a Comment