VS2010中添加WCF引用的问题

最近在公司的项目中,遇到WCF相关的各种问题,不得不说,WCF这东东虽好,不过还是有很多不足的地方,希望微软能够持续改进。前一段时间遇到的问题,就是其中的一个,问题具体情况如下:

当你在使用Add service reference欲添加一个WCF服务的时候,在Add service reference窗口会遇到如下的错误:

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.

wcf-img

从这个错误的描述,可以看出,显然在添加服务时,获取到的元数据信息超出了这个添加引用工具的最大值,从图中可以看出,VS2010提示我们,MaxNameTableCharCount这个属性的默认值为16384,其实已经无法满足我们生成这个接口所获取的元数据大小的需要。只要我们手动去修改这个值,即可让VS2010成功获取元数据,并生成service reference。

不过,要到哪里去修改这个值呢?检视了一下我们工程中的app.config,发现工程中的值已经设定为比16384更大了,应该不是这个地方。原来,获取元数据这个步骤,是VS2010本身来获取的,因此,我们得修改VS2010本身的app config文件。

定位到VS2010在你电脑上安装的地址,比如,我把VS2010安装在电脑的D盘上,因此,我的地址是:D:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE

接下来,需要修改文件:devenv.exe.config

在文件中加入如下的配置节(注意,这个配置节一定不要放在configSections的前面,否则会出错)

<system.servicemodel>
<bindings>
<nettcpbinding>
<binding maxreceivedmessagesize="2147483647" maxbufferpoolsize="2147483647" name="MexBinding" maxbuffersize="2147483647">
<readerquotas maxstringcontentlength="2147483647" maxarraylength="2147483647" maxnametablecharcount="2147483647" maxdepth="2147483647" maxbytesperread="2147483647"></readerquotas>
<security mode="None"></security>
</binding>
</nettcpbinding>
</bindings>
<client>
<endpoint bindingconfiguration="MexBinding" binding="netTcpBinding" name="net.tcp" contract="IMetadataExchange"></endpoint>
</client>
</system.servicemodel>

添加完毕后,退出并重启VS2010,然后重新添加服务引用,问题解决!

支持原创技术分享,据说打赏我的人,都找到了女朋友!