WEB站点在调用我们WCF服务的时候,只要传入的参数过长,就报如下错误:
格式化程序尝试对消息反序列化时引发异常: 尝试对参数 http://tempuri.org/ 进行反序列化时出错: formDataXml。InnerException 消息是“反序列化对象 属于类型 System.String 时出现错误。读取 XML 数据时,超出最大字符串内容长度配额 (8192)。通过更改在创建 XML 读取器时所使用的 XmlDictionaryReaderQuotas 对象的 MaxStringContentLength 属性,可增加此配额。 第 137 行,位置为 76。”。有关详细信息,请参阅 InnerException。
网上一搜索,答案基本得到解决,可我的问题就是不能解决(主要是细节打败了我),按照网上的文章进行服务器端修改配置如下:
<binding name="HttpBinding" maxReceivedMessageSize="2097152"> <readerQuotas maxDepth="32" maxStringContentLength="2097152" maxArrayLength="2097152" maxBytesPerRead="2097152" maxNameTableCharCount="2097152" /> <security mode="None"></security> </binding>
其实这里主要的配置是两个:maxReceivedMessageSize、maxStringContentLength;
网上提到还需要配置客户端,其实如果是报上面错误就不要管客户端了,因为如果是客户端调用WCF报错,就不是读取XML数据超时,而是明确的错误提示,如下:
已超过传入消息(1024)的最大消息大小配额。若要增加配额,请使用相应绑定元素上的 MaxReceivedMessageSize 属性。
所以这篇文章提到的错误基本与客户端无关。
一般情况下,安装上面的修改就可以解决问题了,但是我的WCF还是报错,没办法,只能继续找,无意间发现服务器端WCF配置有点异常,不用登录验证的WCF接口有用到bindingConfiguration,但是需要验证的WCF接口就没有配置该属性,如下代码:
<bindings> <wsHttpBinding> <binding name="HttpBinding" maxReceivedMessageSize="2097152"> <readerQuotas maxDepth="32" maxStringContentLength="2097152" maxArrayLength="2097152" maxBytesPerRead="2097152" maxNameTableCharCount="2097152" /> <security mode="None"></security> </binding><binding name="HttpBinding" maxReceivedMessageSize="2097152"> <readerQuotas maxDepth="32" maxStringContentLength="2097152" maxArrayLength="2097152" maxBytesPerRead="2097152" maxNameTableCharCount="2097152" /> <security mode="None"></security> </binding> </wsHttpBinding> </bindings> <serviceHostingEnvironment aspNetCompatibilityEnabled="true" /> <services> <service name="Achievo.MMIP.WMP.WebService.WMPProcService" behaviorConfiguration="wmpWcfBehavior"> <endpoint address="" binding="wsHttpBinding" contract="Achievo.MMIP.WMP.WebServiceIService.WMPServiceProcIService"> <identity> <dns value="localhost" /> </identity> </endpoint> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> </service> <service name="Achievo.MMIP.WMP.WebService.WMPGetFormDataService" behaviorConfiguration="wmpWcfFormDataBehavior"> <endpoint address="" binding="wsHttpBinding" bindingConfiguration="HttpBinding" contract="Achievo.MMIP.WMP.WebServiceIService.IWMPGetFormDataIService"> <identity> <dns value="localhost" /> </identity> </endpoint> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> </service> </services>
注意红色部分的配置,大伙可以对比一下,就发现其中一个缺少bindingConfiguration配置;如果这个时候,把缺少的部分补上,和另外一个配置一样,WCF就会出错,这里我的初步判断是:name="Achievo.MMIP.WMP.WebService.WMPProcService"是必须要验证登录才能访问,所以不能绑定上面的匿名访问的配置,简单说就是规定是验证就不能在配置为匿名;所以把配置修改为如下:
<bindings> <wsHttpBinding> <binding name="HttpBinding" maxReceivedMessageSize="2097152"> <readerQuotas maxDepth="32" maxStringContentLength="2097152" maxArrayLength="2097152" maxBytesPerRead="2097152" maxNameTableCharCount="2097152" /> <security mode="None"></security> </binding> <binding name="HttpBinding1" maxReceivedMessageSize="2097152"> <readerQuotas maxDepth="32" maxStringContentLength="2097152"/> </binding> </wsHttpBinding> </bindings> <serviceHostingEnvironment aspNetCompatibilityEnabled="true" /> <services> <service name="Achievo.MMIP.WMP.WebService.WMPProcService" behaviorConfiguration="wmpWcfBehavior"> <endpoint address="" binding="wsHttpBinding" bindingConfiguration="HttpBinding1" contract="Achievo.MMIP.WMP.WebServiceIService.WMPServiceProcIService"> <identity> <dns value="localhost" /> </identity> </endpoint> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> </service> <service name="Achievo.MMIP.WMP.WebService.WMPGetFormDataService" behaviorConfiguration="wmpWcfFormDataBehavior"> <endpoint address="" binding="wsHttpBinding" bindingConfiguration="HttpBinding" contract="Achievo.MMIP.WMP.WebServiceIService.IWMPGetFormDataIService"> <identity> <dns value="localhost" /> </identity> </endpoint> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> </service> </services>
在验证,OK,通过。