Search code examples
.netwcfweb-services.net-3.5wcf-binding

How can I bypass the maximum nametable character count quota (16384) with my custom binding?


I am recieving the below CommunicationException from a WCF service call. I understand that according to this answer, I need to set the TextMessageEncodingBindingElement.ReaderQuotas object, but it appears to be read only (though in the documentation it say it is Get/Set).

Does anyone know how to do this in code?

System.ServiceModel.CommunicationException was caught
  Message="Error in deserializing body of reply message for operation 'PutMessage'."
  Source="mscorlib"
  StackTrace:
     Server stack trace:     at System.ServiceModel.Dispatcher.XmlSerializerOperationFormatter.DeserializeBody(XmlDictionaryReader reader, MessageVersion version, XmlSerializer serializer, MessagePartDescription returnPart, MessagePartDescriptionCollection bodyParts, Object[] parameters, Boolean isRequest)    at System.ServiceModel.Dispatcher.XmlSerializerOperationFormatter.DeserializeBody(XmlDictionaryReader reader, MessageVersion version, String action, MessageDescription messageDescription, Object[] parameters, Boolean isRequest)    at System.ServiceModel.Dispatcher.OperationFormatter.DeserializeBodyContents(Message message, Object[] parameters, Boolean isRequest)    at System.ServiceModel.Dispatcher.OperationFormatter.DeserializeReply(Message message, Object[] parameters)    at System.ServiceModel.Dispatcher.ProxyOperationRuntime.AfterReply(ProxyRpc& rpc)    at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc)    at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)    at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs)    at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)    at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)  Exception rethrown at [0]:     at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)    at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)    at
     ...
  InnerException: System.InvalidOperationException
       Message="There is an error in XML document (1, 695)."
       Source="System.Xml"
       StackTrace:
            at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)    at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle)    at System.ServiceModel.Dispatcher.XmlSerializerOperationFormatter.DeserializeBody(XmlDictionaryReader reader, MessageVersion version, XmlSerializer serializer, MessagePartDescription returnPart, MessagePartDescriptionCollection bodyParts, Object[] parameters, Boolean isRequest)
       InnerException: System.Xml.XmlException
            LineNumber=0
            LinePosition=0
            Message="The maximum nametable character count quota (16384) has been exceeded while reading XML data. The nametable is a data structure used to store strings encountered during XML processing - long XML documents with non-repeating element names, attribute names and attribute values may trigger this quota. This quota may be increased by changing the MaxNameTableCharCount property on the XmlDictionaryReaderQuotas object used when creating the XML reader. Line 1, position 695."
            Source="System.Runtime.Serialization"
            StackTrace:
                 at System.Xml.XmlExceptionHelper.ThrowXmlException(XmlDictionaryReader reader, String res, String arg1, String arg2, String arg3)    at System.Xml.XmlExceptionHelper.ThrowMaxNameTableCharCountExceeded(XmlDictionaryReader reader, Int32 maxNameTableCharCount)    at System.Xml.XmlBaseReader.QuotaNameTable.Add(Int32 charCount)    at System.Xml.XmlBaseReader.QuotaNameTable.Add(String value)    at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderstarTransportPortTypes.InitIDs()    at System.Xml.Serialization.XmlSerializationReader.Init(XmlReader r, XmlDeserializationEvents events, String encodingStyle, TempAssembly tempAssembly)    at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
            InnerException: 

This is the code I use to construct my binding:

Private Function GetCustomBinding() As Channels.Binding

    Dim asbe As New Channels.AsymmetricSecurityBindingElement

    '... set various properties on asbe object

    'Add the elements to the custom binding
    Dim myBinding As New CustomBinding

    'Protocol Binding Elements (security)
    myBinding.Elements.Add(asbe)

    'Encoding Binding Element
    Dim textMsgEncoder As New TextMessageEncodingBindingElement(MessageVersion.Soap11, System.Text.Encoding.UTF8)

    Dim rQuota As New System.Xml.XmlDictionaryReaderQuotas
    rQuota.MaxDepth = 2147483647
    rQuota.MaxStringContentLength = 2147483647
    rQuota.MaxArrayLength = 2147483647
    rQuota.MaxBytesPerRead = 2147483647
    rQuota.MaxNameTableCharCount = 2147483647


    ''''''''''

    'textMsgEncoder.ReaderQuotas = rQuota 'this is readonly, so can't do it

    ''''''''''


    myBinding.Elements.Add(textMsgEncoder)

    'Transport Binding Element
    Dim httpsBindingElement As New HttpsTransportBindingElement()
    httpsBindingElement.MaxBufferSize = 5000000
    httpsBindingElement.MaxReceivedMessageSize = 5000000

    myBinding.Elements.Add(httpsBindingElement)

    Return myBinding

End Function

Solution

  • You can't set the ReaderQuotas object, but you can set individual properties on it, like so:

    textMsgEncoder.ReaderQuotas.MaxNameTableCharCount = 2147483647 or other appropriate value