有一个WCF应用, 主要功能是存储doc, txt等类型文件到database,当文件的大小在16kb之内,调用WCF service能正常工作;但如果文件大小超出16KB之外, 它将抛出这样一个错误:
The remote server returned an unexpected response: (400) Bad Request.
经过在网上的查询, 需要在服务端添加配置maxReceivedMessageSize (可以参考这位朋友的文章), 基于这位朋友发布的信息上,还需要注意定义binding name时需要与endpoint里的bindingConfiguration的名称一致,否则还是会出错。
开始我使用的binding类型是basicHttpBinding, 添加之后,会出现另外一个错误:
The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter . The InnerException message was 'There was an error deserializing the object of type EmailHelperWCF.EmailContent. The maximum array length quota (16384) has been exceeded while reading XML data. This quota may be increased by changing the MaxArrayLength property on the XmlDictionaryReaderQuotas object used when creating the XML reader. Line 1, position 237210.'. Please see InnerException for more details.
解决办法:
1. 改变binding类型到WSHttpBinding
2. 在Binding下添加<readerQuotas maxStringContentLength="2147483647" maxArrayLength="2147483647">, 看下面的第三行
12 3 54
3. 最好在<behaviors>下添加<dataContractSerializer maxItemsInObjectGraph="2147483647"/>, 看下面的第8行
12 3 114 5 106 7 8 9