WCF字符串如果过长该如何处理
创始人
2024-06-23 11:01:20
0

在WCF服务中,如果我们遇到了程序中的字符串过长的话,应该如何正确处理呢?在这里我们将会为大家详细介绍一下WCF字符串的相关应用技巧,以帮助大家解决在实际应用中出现的一些特定问题。#t#

编写基于WCF服务的程序时,向WCF服务端发送一长串的HTML源码,结果客户端收到提示如下:

WCF字符串格式化程序尝试对消息反序列化时引发异常: 对操作“AddArticle”的请求消息正文进行反序列化时出现错误。读取 XML 数据时,超出最大字符串内容长度配额 (8192)。通过更改在创建 XML 读取器时所使用的 XmlDictionaryReaderQuotas 对象的 MaxStringContentLength 属性,可增加此配额。 第 64 行,位置为 79。

主要是maxStringContentLength和maxReceivedMessageSize的设置会影响到消息的发送和接收,于是全部改为2MB大小,即2097152。

客户端app.config修改:

  1. < bindings> 
  2. < basicHttpBinding> 
  3. < binding name="BasicHttpBinding_ShareService" closeTimeout="00:01:00" 
  4. openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" 
  5. allowCookies="false" bypassProxyOnLocal="false" 
    hostNameComparisonMode="StrongWildcard" 
  6. maxBufferSize="65536" maxBufferPoolSize="524288" 
    maxReceivedMessageSize="2097152" 
  7. messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" 
  8. useDefaultWebProxy="true"> 
  9. < !-- Reset maxStringContentLength for deserialize --> 
  10. < readerQuotas maxDepth="32" maxStringContentLength="2097152" 
    maxArrayLength="16384" 
  11. maxBytesPerRead="4096" maxNameTableCharCount="16384" /> 
  12. < security mode="None"> 
  13. < transport clientCredentialType="None" proxyCredentialType="None" 
  14. realm="" /> 
  15. < message clientCredentialType="UserName" algorithmSuite="Default" /> 
  16. < /security> 
  17. < /binding> 
  18. < /basicHttpBinding> 
  19. < /bindings> 

服务端web.config修改:

 

  1. < system.serviceModel> 
  2. < !-- add for the message size --> 
  3. < bindings> 
  4. < basicHttpBinding> 
  5. < binding name="NewBinding2MB" maxReceivedMessageSize="2097152"> 
  6. < readerQuotas maxStringContentLength="2097152" /> 
  7. < /binding> 
  8. < /basicHttpBinding> 
  9. < /bindings> 
  10. < !-- add for the message size --> 
  11. < behaviors> 
  12. < serviceBehaviors> 
  13. < behavior name="Web.WCF.ShareServiceBehavior"> 
  14. < serviceMetadata httpGetEnabled="true"/> 
  15. < serviceDebug includeExceptionDetailInFaults="false"/> 
  16. < /behavior> 
  17. < /serviceBehaviors> 
  18. < /behaviors> 
  19. < serviceHostingEnvironment aspNetCompatibilityEnabled="true"/> 
  20. < services> 
  21. < service behaviorConfiguration="Web.WCF.ShareServiceBehavior" 
    name="Web.WCF.ShareService"> 
  22. < endpoint address="" binding="basicHttpBinding" 
    bindingConfiguration="NewBinding2MB" contract="Web.WCF.ShareService"/> 
  23. < endpoint address="mex" binding="mexHttpBinding" 
    contract="IMetadataExchange"/> 
  24. < /service> 
  25. < /services> 
  26. < /system.serviceModel> 

以上就是针对WCF字符串过程的解决方法。

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
Windows恶意软件20年“... 在Windows的早期年代,病毒游走于系统之间,偶尔删除文件(但被删除的文件几乎都是可恢复的),并弹...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
着眼MAC地址,解救无法享受D... 在安装了DHCP服务器的局域网环境中,每一台工作站在上网之前,都要先从DHCP服务器那里享受到地址动...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...