2011-05-05 51 views
5

我正在編寫一個WCF服務來上傳文件,但是當字節數組超過16384個元素時,它會引發異常。WCF中的最大數組長度配額例外

這是異常的細節:

格式化拋出異常而 試圖反序列化消息: 錯誤操作 「CreateDocument」反序列化請求 消息的主體。在讀取XML數據時,最大數組長度限額(16384)已超過 。通過在創建XML閱讀器時更改 XmlDictionaryReaderQuotas對象 上的 MaxArrayLength屬性,可以增加此配額 。行1, 位置22862.

爲客戶端和服務器的配置設置最大數組長度配額到2147483647

客戶端配置:

<system.serviceModel> 
    <bindings> 
    <basicHttpBinding> 
    <binding name="BasicHttpBinding_IDocumentLibraryService" closeTimeout="00:01:00" 
    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" 
    allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" 
    maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" 
    messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" 
    useDefaultWebProxy="true"> 
    <readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="2147483647" 
     maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" /> 
    <security mode="None"> 
     <transport clientCredentialType="None" proxyCredentialType="None" 
     realm="" /> 
     <message clientCredentialType="UserName" algorithmSuite="Default" /> 
    </security> 
    </binding> 
    </basicHttpBinding> 
    </bindings> 
    <client> 
    <endpoint address="http://localhost:50764/DocumentLibraryService.svc" 
    binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IDocumentLibraryService" 
    contract="DocumentLibrary.IDocumentLibraryService" name="BasicHttpBinding_IDocumentLibraryService" /> 
    </client> 

服務器配置:

<bindings> 

      <basicHttpBinding> 
       <binding name="BasicHttpBinding_IDocumentLibraryService" 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="65536" 
       messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" 
       useDefaultWebProxy="true"> 
        <readerQuotas maxDepth="2147483647" 
       maxStringContentLength="2147483647" 
       maxArrayLength="2147483647" 
       maxBytesPerRead="2147483647" 
       maxNameTableCharCount="2147483647" /> 
        <security mode="None"> 
         <transport clientCredentialType="None" proxyCredentialType="None" 
         realm="" /> 
         <message clientCredentialType="UserName" algorithmSuite="Default" /> 
        </security> 
       </binding> 
      </basicHttpBinding> 
     </bindings> 
     <services> 

      <service name="BasicHttpBinding_IDocumentLibraryService"> 

       <clear /> 
       <endpoint address="mex" binding="mexHttpBinding" name="mex" contract="Peninsula.DocumentLibrary.ServiceLayer.IDocumentLibraryService" /> 
       <endpoint binding="basicHttpBinding" name="DocumentLibraryService" contract="Peninsula.DocumentLibrary.ServiceLayer.IDocumentLibraryService" address="" 
          bindingConfiguration="BasicHttpBinding_IDocumentLibraryService"/> 
      </service> 
     </services> 

回答

2

所有我需要做的是在web.config文件來更改服務名稱以完整的服務名稱命名空間:

<service name="SampleNameSpace.DocumentLibraryService"> 

       <clear /> 
       <endpoint address="mex" binding="mexHttpBinding" name="mex" contract="Peninsula.DocumentLibrary.ServiceLayer.IDocumentLibraryService" /> 
       <endpoint binding="basicHttpBinding" name="DocumentLibraryService" contract="Peninsula.DocumentLibrary.ServiceLayer.IDocumentLibraryService" address="" 
          bindingConfiguration="BasicHttpBinding_IDocumentLibraryService"/> 
      </service> 
+0

這次1000!我失去了兩天將我的頭撞在牆上,試圖弄清楚我的配置有什麼問題。我有所有適當的配額設置,並仍然出現錯誤。謝謝! – Emjay 2014-02-07 15:33:13

0

這不是真的答案,因爲你的配置似乎沒問題。我認爲您只需要在服務主機代理的代碼(輸出以跟蹤或調試)中檢查這些值,以確保配置中的相同值已加載到您的通道中。

在可能性是達到另一閾值和錯誤誤導

現在我強烈建議不要使用字節數組,特別是如果你使用XML來上傳文件。這些將被表示爲XML數組,並且結構將會是非常臃腫的XML,其將比原始文件多10倍。

我會用:

  • WCF Streaming與基本的結合,以及工作和爲超快
  • 或者代表字節數組作爲的base64字符串。這將需要更多的33%的空間,但不是1000%

UPDATE

可以跟蹤被用來配置服務(使用它內部的任何您的WCF操作)的綁定名稱:

public int MyServiceOperation() 
{ 
    Trace.WriteLine(OperationContext.Current.EndpointDispatcher.ChannelDispatcher.BindingName) 
.... 
+0

感謝您的回覆Ali Jan,我的服務託管在IIS上。我如何在IIS上檢查這些值? – 2011-05-05 11:53:39

+0

我做了更新,請看看。 – Aliostad 2011-05-05 12:16:18