WCF-在读取XML数据时,已超过了最大的字符数量配额(16384)
-
03-10-2019 - |
题
我正在拥有使用WSHTPBINDING的WCF服务。服务器配置如下:
<bindings>
<wsHttpBinding>
<binding name="wsHttpBinding" maxBufferPoolSize="2147483647"
maxReceivedMessageSize="2147483647">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
<security mode="None">
<transport clientCredentialType="Windows" proxyCredentialType="None"
realm="" />
<message clientCredentialType="Windows" negotiateServiceCredential="true"
algorithmSuite="Default" establishSecurityContext="true" />
</security>
</binding>
</wsHttpBinding>
</bindings>
在客户端,我包括WCF服务的服务参考。如果我的功能有限,则在我的Iservice中说90个操作合同,但是如果添加一个比我无法更新服务参考的操作合同,则它的效果很好,也无法添加该服务参考。在 这 文章提到,通过更改那些配置文件(即devenv.exe.config,wcftestclient.exe.config和svcutil.exe.config),它将起作用,但即使在那些配置文件中包含这些绑定,该错误仍然弹出了,
下载错误'http://10.0.3.112/myservice/service1.svc/mex'。 HTTP状态400:不良请求失败。元数据包含无法解决的参考:'http://10.0.3.112/myservice/service1.svc/mex'。 XML文档(1,89549)中存在错误。在读取XML数据时,已超过了最大的命名字符计数(16384)。该名称是一种数据结构,用于存储XML处理过程中遇到的字符串 - 具有非重复元素名称,属性名称和属性值的长XML文档可能会触发此配额。通过更改创建XML读取器时使用的XmldictionaryReaderQuotas对象上的MaxNametablecharcount属性,可以增加该配额。第1行,位置89549。如果在当前解决方案中定义了服务,请尝试构建解决方案并再次添加服务参考。
知道如何解决这个问题???
解决方案
尝试以下操作:
在您的Visual Studio的“安装目录”中,Devenv.exe所在的位置(例如C: Program Files Microsoft Visual Studio 9.0 Common7 IDE)将此部分添加到Devenv.exe.cofig
<system.serviceModel>
<client>
<endpoint binding="customBinding" bindingConfiguration="largeServiceBinding" contract="IMetadataExchange" name="http" />
</client>
<bindings>
<customBinding>
<!-- NOTE: The binding name must be the same as specified in the config file of the wcf service -->
<binding name="largeServiceBinding" >
<textMessageEncoding>
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
</textMessageEncoding>
<httpTransport transferMode="Buffered" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647"/>
</binding>
</customBinding>
</bindings>
</system.serviceModel>
在您的wcf服务的app.config中添加相同的绑定:
<bindings>
<customBinding >
<!-- NOTE: The binding name must be the same as specified in the devenv.exe.config file located ..\Common7\IDE folder of the VS installation directory -->
<binding name="largeServiceBinding" >
<textMessageEncoding>
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647"
maxNameTableCharCount="2147483647" />
</textMessageEncoding>
<httpTransport transferMode="Buffered" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647"/>
</binding>
</customBinding>
</bindings>
请注意,来自两个文件的绑定标签的名称属性必须匹配 (例如largeserviceBinding)
最后将以下MEX端点添加到您的服务标签中:
<endpoint address="mex" binding="customBinding" contract="IMetadataExchange" bindingName="testBinding" bindingConfiguration="largeServiceBinding" name="http"/>
看起来像这样:
<services>
<service behaviorConfiguration="MyServiceBehavior"
name="MyService.MyService">
<endpoint address="" binding="wsHttpBinding" contract="MyService.IMyService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="customBinding" contract="IMetadataExchange" bindingName="testBinding" bindingConfiguration="largeServiceBinding" name="http"/>
<host>
<baseAddresses>
<add baseAddress="http://localhost:8731/Design_Time_Addresses/MyService/MyService/" />
</baseAddresses>
</host>
</service>
</services>
其他提示
我知道已经有一段时间了,但是我遇到了同样的问题,并找到了其他(简单)的解决方案 在Codeproject中
在给出的解决方案中,值是在代码而不是.config文件中设置的值。
BasicHttpBinding binding = new BasicHttpBinding();
binding.Security.Mode = BasicHttpSecurityMode.Transport;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
binding.MaxReceivedMessageSize = 50000000;
binding.ReaderQuotas.MaxArrayLength = 50000000;
binding.ReaderQuotas.MaxStringContentLength = 50000000;
binding.ReaderQuotas.MaxNameTableCharCount = 50000000;
EndpointAddress endpoint = new EndpointAddress(new Uri("https://server/EWS/Exchange.asmx"));
ExchangeServicePortTypeClient ews = new ExchangeServicePortTypeClient(binding, endpoint);
但是,我更改了.config文件中相关值中的值 <binding>
和 <readerQuotas>
部分)并解决了问题(而不是添加自定义绑定):
<binding name="ITransactionProcessor" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="50000000" maxBufferPoolSize="524288" maxReceivedMessageSize="50000000"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="50000000" maxArrayLength="50000000"
maxBytesPerRead="4096" maxNameTableCharCount="50000000" />
<security mode="TransportWithMessageCredential">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
我希望这对某人有帮助:)
要识别的一件事是,该消息是指SVCUTIL读取器配额而不是服务! Svcutil对它可以阅读多少元数据有限制。可以使用配置文件更改此限制。解决方案是为SVCutil创建一个配置文件,并将其与工具相同的文件夹中。下次您运行svcutil时,将考虑配置文件值。
http://geekswithblogs.net/claraoscura/archive/2007/08/20/114806.aspx
在您的app.config或dll.config中,在客户端上添加:
<configuration>
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="myMex" maxReceivedMessageSize="1024000"> <!-- modify this to avoid stupid error -->
<readerQuotas maxNameTableCharCount="163840" /> <!-- DO NOT touch this one -->
<security mode="None" />
</binding>
</netTcpBinding>
</bindings>
...
<client>
<endpoint binding="netTcpBinding" bindingConfiguration="myMex"
contract="IMetadataExchange" name="net.tcp" />
...
</client>
</system.serviceModel>
</configuration>
然后你去!这是WCF真正令人讨厌的事情之一,而Google往往只会赚取很多BS。浪费了很多时间。