WCF字符串如果過長該如何處理
在WCF服務(wù)中,如果我們遇到了程序中的字符串過長的話,應(yīng)該如何正確處理呢?在這里我們將會為大家詳細(xì)介紹一下WCF字符串的相關(guān)應(yīng)用技巧,以幫助大家解決在實(shí)際應(yīng)用中出現(xiàn)的一些特定問題。#t#
編寫基于WCF服務(wù)的程序時,向WCF服務(wù)端發(fā)送一長串的HTML源碼,結(jié)果客戶端收到提示如下:
WCF字符串格式化程序嘗試對消息反序列化時引發(fā)異常: 對操作“AddArticle”的請求消息正文進(jìn)行反序列化時出現(xiàn)錯誤。讀取 XML 數(shù)據(jù)時,超出最大字符串內(nèi)容長度配額 (8192)。通過更改在創(chuàng)建 XML 讀取器時所使用的 XmlDictionaryReaderQuotas 對象的 MaxStringContentLength 屬性,可增加此配額。 第 64 行,位置為 79。
主要是maxStringContentLength和maxReceivedMessageSize的設(shè)置會影響到消息的發(fā)送和接收,于是全部改為2MB大小,即2097152。
客戶端app.config修改:
- < bindings>
 - < basicHttpBinding>
 - < binding name="BasicHttpBinding_ShareService" closeTimeout="00:01:00"
 - openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
 - allowCookies="false" bypassProxyOnLocal="false"
 
hostNameComparisonMode="StrongWildcard"- maxBufferSize="65536" maxBufferPoolSize="524288"
 
maxReceivedMessageSize="2097152"- messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
 - useDefaultWebProxy="true">
 - < !-- Reset maxStringContentLength for deserialize -->
 - < readerQuotas maxDepth="32" maxStringContentLength="2097152"
 
maxArrayLength="16384"- maxBytesPerRead="4096" maxNameTableCharCount="16384" />
 - < security mode="None">
 - < transport clientCredentialType="None" proxyCredentialType="None"
 - realm="" />
 - < message clientCredentialType="UserName" algorithmSuite="Default" />
 - < /security>
 - < /binding>
 - < /basicHttpBinding>
 - < /bindings>
 
服務(wù)端web.config修改:
- < system.serviceModel>
 - < !-- add for the message size -->
 - < bindings>
 - < basicHttpBinding>
 - < binding name="NewBinding2MB" maxReceivedMessageSize="2097152">
 - < readerQuotas maxStringContentLength="2097152" />
 - < /binding>
 - < /basicHttpBinding>
 - < /bindings>
 - < !-- add for the message size -->
 - < behaviors>
 - < serviceBehaviors>
 - < behavior name="Web.WCF.ShareServiceBehavior">
 - < serviceMetadata httpGetEnabled="true"/>
 - < serviceDebug includeExceptionDetailInFaults="false"/>
 - < /behavior>
 - < /serviceBehaviors>
 - < /behaviors>
 - < serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
 - < services>
 - < service behaviorConfiguration="Web.WCF.ShareServiceBehavior"
 
name="Web.WCF.ShareService">- < endpoint address="" binding="basicHttpBinding"
 
bindingConfiguration="NewBinding2MB" contract="Web.WCF.ShareService"/>- < endpoint address="mex" binding="mexHttpBinding"
 
contract="IMetadataExchange"/>- < /service>
 - < /services>
 - < /system.serviceModel>
 
以上就是針對WCF字符串過程的解決方法。















 
 
 








 
 
 
 