有一个WCF应用, 主要功能是存储doc, txt等类型文件到database,当文件的大小在16kb之内,调用WCF service能正常工作;但如果文件大小超出16KB之外, 它将抛出这样一个错误:

The remote server returned an unexpected response: (400) Bad Request.

经过在网上的查询, 需要在服务端添加配置maxReceivedMessageSize (可以参考这位朋友的文章http://www.cnblogs.com/Gyoung/p/3369316.html), 基于这位朋友发布的信息上,还需要注意定义binding name时需要与endpoint里的bindingConfiguration的名称一致,否则还是会出错。

<binding name="basicHttpBinding_IEmailUtility" closeTimeout="00:10:00" receiveTimeout="00:20:00" sendTimeout="00:20:00" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" openTimeout="00:10:00" messageEncoding="Mtom"/>

开始我使用的binding类型是basicHttpBinding, 添加之后,会出现另外一个错误:

The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://tempuri.org/:oneEmail. 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">, 看下面的第三行

 <wsHttpBinding>
<binding name="WSHttpBinding_IEmailUtility" closeTimeout="00:10:00" receiveTimeout="00:20:00" sendTimeout="00:20:00" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" openTimeout="00:10:00" messageEncoding="Mtom">
<readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/>
</binding>
</wsHttpBinding>

3. 最好在<behaviors>下添加<dataContractSerializer maxItemsInObjectGraph="2147483647"/>, 看下面的第8行

  <behaviors>
<serviceBehaviors>
<behavior name="EmailHelperWCF.Service1Behavior">
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
<dataContractSerializer maxItemsInObjectGraph="2147483647"/>
</behavior>
</serviceBehaviors>
</behaviors>

The formatter threw an exception while trying to deserialize the message in WCF的更多相关文章

  1. 使用Spire组件抛出异常The type initializer for 'spr857' threw an exception

    使用Spire组件抛出异常The type initializer for 'spr857' threw an exception 我使用免费的Spire.Xls组件尝试去转换Excel文档到PDF文 ...

  2. System.Security.SecurityException The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception

    [15/08/19 00:03:10] [DataManager-7292-ERROR] System.Reflection.TargetInvocationException: Exception ...

  3. iOS - libc++abi.dylib: terminate_handler unexpectedly threw an exception

    代码出现crash,报错:libc++abi.dylib: terminate_handler unexpectedly threw an exception 当我们很明确是某一块代码执行导致了错误, ...

  4. web项目引用Java项目,连接报错error HTTP Status 500 - Servlet execution threw an exception

    错误信息 项目背景: 一个web项目引用一个java Project,项目中添加了引用,但是打开页面访问,总报500错误.提示:servlet初始化错误. 环境:Eclipse luna JDK: 1 ...

  5. System.TypeInitializationException: The type initializer for 'Mono.Unix.Native.Stdlib' threw an exception.

    08-31 17:02:03: ### DEBUG ##########################System.TypeInitializationException: The type ini ...

  6. the type initializer for '' threw an exception

    the type initializer for '' threw an exception 问题:程序启动时初始化主窗口类时,弹出该错误.调查:查看类的构造函数是否会有异常抛出.解决:去掉类的构造函 ...

  7. javax.servlet.ServletException: Servlet execution threw an exception 异常解决之一

    配置JDBC连接的JDBC.properties文件不存在(那天很奇怪配置文件不存在了,我也没有去移动那个文件.诡异呀)也会导致这个异常. 然后就报javax.servlet.ServletExcep ...

  8. libc++abi.dylib: terminate_handler unexpectedly threw an exception错误小结

    说法一: 我们在运行xcode工程时,有时候会遇到”libc++abi.dylib: terminate_handler unexpectedly threw an exception”错误,app莫 ...

  9. Filter execution threw an exception 错误

    Filter execution threw an exception 错误,我在web.xml中配置了一个filter用spring来解决懒加载的问题,为什么就会包这个错 java.lang.NoS ...

随机推荐

  1. Cocos3.0测试版发布(中文)

    最新的cocos2d-x 3.0版本,我们的目标不仅是改进渲染机制,增加对2.5D的支持,基于组件的系统功能,和更好的Label功能.同时 我们希望能够进一步优化引擎,并且使用更友好的C++ API ...

  2. Android URI简单介绍

    就Android平台而言,URI主要分三个部分:scheme, authority and path.当中authority又分为host和port.格式例如以下: scheme://host:por ...

  3. Ubuntu 下 JDK+Tomcat+MySql 环境的搭建

    Linux环境 修改catalina.sh 在“echo "Using CATALINA_BASE: $CATALINA_BASE"”上面加入以下行: JAVA_OPTS=&quo ...

  4. hdu1258 Sum It Up (DFS)

    Problem Description Given a specified total t and a list of n integers, find all distinct sums using ...

  5. 第一篇:Power BI数据可视化概述

    前言 "可视化之工具,可爱者甚蕃.统计学家独爱R,自Python来,世人盛爱matplotlib.余独爱Power BI之出微软而不染(免费),濯Office而不妖(够精简).......& ...

  6. iOS人脸识别(CoreImage)

    1.从初始UIImage获取一个CIImage对象. 2.创建一个用于分析对象的CIContext. 3.通过type和options参数创建一个CIDetector实例. type参数指定了要识别的 ...

  7. 异步tcp通信——APM.Server 消息推送服务的实现

    消息推送服务 服务器推送目前流行就是私信.发布/订阅等模式,基本上都是基于会话映射,消息对列等技术实现的:高性能.分布式可以如下解决:会话映射可采用redis cluster等技术实现,消息对列可使用 ...

  8. css sprint 生成工具 bg2css

    今天需要改个css sprint,之前使用过一个工具蛮好使,但是就是想不起叫什么名字,网上搜了很久,才再次找到,原来是bg2css,今天记录下,以备以后不时之需. 下载地址:http://www.cs ...

  9. 关于html水平垂直居中的一些总结吧

    html水平垂直居中 最近遇到很多居中的问题,就花点时间总结了一下放在这里,以后找也方便,0.0~~ 1.居中文本 <div class="wrap"> 我在中间…… ...

  10. My.Ioc 代码示例——属性和方法注入

    在 My.Ioc 中,我们可以指定让容器在构建好对象实例之后,自动为我们调用对象的公共方法或是为对象的公共属性赋值.在解析对象实例时,容器将根据我们在注册对象时指定的方法调用或属性赋值的先后顺序,调用 ...