1. 程式人生 > >InnerException 訊息是“反序列化物件 屬於型別 *** 時出現錯誤。讀取 XML 資料時,超出最大字串內容長度配額 (8192)。(注意細節)

InnerException 訊息是“反序列化物件 屬於型別 *** 時出現錯誤。讀取 XML 資料時,超出最大字串內容長度配額 (8192)。(注意細節)

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,通過。