The maximum nametable character count quota (16384) has been exceeded while reading XML data
When I am trying to read a meta data I got this error any Idea here is My code
WSHttpBinding binding = new WSHttpBinding(SecurityMode.None);开发者_StackOverflow社区
binding.MaxReceivedMessageSize = Int32.MaxValue; // DPNote: This may actually be too big. see how it performs.
binding.ReaderQuotas.MaxNameTableCharCount = 99999999;
MetadataExchangeClientMode exchangeMode = MetadataExchangeClientMode.HttpGet; // Default to a HttpGET
metaClient = new MetadataExchangeClient(binding);
metaClient.MaximumResolvedReferences = 10 * 100; //DPNote: Some arbitrary number. Default is 10, so this is bigger.
if (address.Scheme == "http")
exchangeMode = MetadataExchangeClientMode.HttpGet;
else if (address.Scheme == "https")
exchangeMode = MetadataExchangeClientMode.HttpGet;
else
exchangeMode = MetadataExchangeClientMode.MetadataExchange;
MetadataSet metadata = metaClient.GetMetadata(address, exchangeMode);
MetadataImporter importer = new WsdlImporter(metadata);
and this is the line which throws the error
MetadataSet metadata = metaClient.GetMetadata(address, exchangeMode);
If you are doing 'Update Reference' through visual studio, add these lines to devenv.exe.config
<system.serviceModel>
<client>
<endpoint binding="netTcpBinding" bindingConfiguration="GenericBinding" contract="IMetadataExchange" name="net.tcp" />
</client>
<bindings>
<netTcpBinding>
<binding name="GenericBinding"
maxBufferPoolSize="2147483647" maxBufferSize="2147483647"
maxReceivedMessageSize="2147483647">
<readerQuotas maxDepth="2147483647"
maxStringContentLength="2147483647"
maxArrayLength="2147483647"
maxBytesPerRead="2147483647"
maxNameTableCharCount="2147483647" />
<security mode="None" />
</binding>
</netTcpBinding>
</bindings>
</system.serviceModel>
This is a error in Microsoft code,Http-Get not support Reader Quotas, so we can do this
var smAsm = AppDomain.CurrentDomain.GetAssemblies().First(a => a.FullName.StartsWith("System.ServiceModel,"));
var defTy = smAsm.GetType("System.ServiceModel.Channels.EncoderDefaults");
var rq = (System.Xml.XmlDictionaryReaderQuotas)defTy.GetField("ReaderQuotas", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic).GetValue(null);
rq.MaxArrayLength = int.MaxValue;
rq.MaxDepth = int.MaxValue;
rq.MaxNameTableCharCount = int.MaxValue;
rq.MaxStringContentLength = int.MaxValue;
Have you tried incrementing any of these other values inside of binding.ReaderQuotas
beyond their default values:
maxDepth, maxStringContentLength, maxArrayLength, maxBytesPerRead?
It could also be the maxBufferPoolSize of the binding
The problem seems to be fixed in .NET 4.0. According to Reflector, MetadataExchangeClient now reads the quotas from the supplied binding, and if it doesn't have those, it falls back to EncoderDefaults. Previously, I think it only used EncoderDefaults, that's why @BreakHead's solution seems to be the only solution (to change EncoderDefaults with reflection).
精彩评论