2012-03-14 69 views
1

類似的問題正在流逝,我看着所有人。它似乎沒有解決我的問題。通過WCF服務的大文件

- 更新: -

我想上傳一個文件(PDF,DOC,或其他)使用WCF服務的數據庫。 到服務的調用是這樣的:

using (var cw = new WCFClientWrapper<ICommonService>()) 
{ 
    cw.Channel.DocumentInsert(content, filename, contentType); 
} 

這裏是合同簽字:

[OperationContract] 
void DocumentInsert(byte[] content, string fileName, string contentType); 

請注意,我傳遞的字節數組的內容,因爲這是需要被傳遞來將數據存儲在數據庫中。

- 更新結束 -

我可以成功地上傳一個小文件(夫妻KB)。然而,當我嘗試上傳更大的東西(20KB),我得到一個異常:

格式化拋出一個異常,而試圖反序列化 消息:錯誤操作 反序列化請求消息的身體DocumentInsert「 。在讀取XML數據時,最大陣列長度配額(16384)已超過 。此配額可能會增加 更改創建XML閱讀器時使用的XmlDictionaryReaderQuotas 對象上的MaxArrayLength屬性。行1,位置31774.

錯誤似乎很明顯...只是去增加MaxArrayLength。我沒有取得任何成功的結果。下面是我的web.configs相關部分

客戶:

<system.serviceModel> 

    <behaviors> 
     <endpointBehaviors> 
     <behavior name="SecureBehavior"> 
     </behavior> 
     </endpointBehaviors> 
    </behaviors> 

    <bindings> 
     <basicHttpBinding> 
     <binding name="WSHttpBinding_Service" closeTimeout="00:01:00" 
      openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" 
      bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" 
      maxBufferPoolSize="524288" maxReceivedMessageSize="262144" messageEncoding="Text" 
      textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true" allowCookies="false"> 
      <readerQuotas maxDepth="32" maxStringContentLength="5242880" maxArrayLength="2147483646" maxBytesPerRead="4096" maxNameTableCharCount="5242880" /> 
     </binding> 
     </basicHttpBinding> 
    </bindings> 

    <client> 
     <endpoint address="http://dev.svc.someurl.com/CommonService.svc" 
       binding="basicHttpBinding" 
       bindingConfiguration="WSHttpBinding_Service" 
       behaviorConfiguration="SecureBehavior" 
       contract="MyApp.Contracts.ServiceContracts.ICommonService" 
       name="MyApp.Contracts.ServiceContracts.ICommonService"> 
     </endpoint> 
    </client> 

    </system.serviceModel> 

服務:

<system.serviceModel> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior> 
      <!-- 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="true"/> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 

    <bindings> 
     <basicHttpBinding> 
     <binding name="MyBasicHttpBinding" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" maxBufferPoolSize="2147483647"> 
      <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" /> 
     </binding> 
     </basicHttpBinding> 
    </bindings> 

    <services> 
     <service name="MyApp.WCFServices.CommonService"> 
     <endpoint address="" 
        binding="basicHttpBinding" 
        bindingConfiguration="MyBasicHttpBinding" 
        contract="MyApp.Contracts.ServiceContracts.ICommonService"> 
      <identity> 
      <dns value="localhost"/> 
      </identity> 
     </endpoint> 
     <endpoint address="mex" 
        binding="mexHttpBinding" 
        contract="IMetadataExchange" /> 
     </service> 
     <service name="MyApp.WCFServices.AccountService"> 
     <endpoint address="" 
        binding="basicHttpBinding" 
        bindingConfiguration="MyBasicHttpBinding" 
        contract="MyApp.Contracts.ServiceContracts.IAccountService"> 
      <identity> 
      <dns value="localhost"/> 
      </identity> 
     </endpoint> 
     <endpoint address="mex" 
        binding="mexHttpBinding" 
        contract="IMetadataExchange" /> 
     </service> 
    </services> 

    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> 
    </system.serviceModel> 

附加診斷顯示:

  • 構建服務:無錯誤/警告
  • 打開服務:警告 - 未找到配置評估上下文 - 未找到匹配的標記。添加了默認端點。
  • 收聽在 'http://dev.svc.someurl.com/CommonService.svc':沒有 錯誤/警告
  • 處理消息1:沒有錯誤/警告
  • 處理動作' http://tempuri.org/ICommonService/DocumentInsert」。 :拋出我開始寫的異常。

任何幫助表示讚賞。

回答

0

幾個月前我遇到過同樣的例外情況。要向/從WCF服務發送/接收大數據,您必須設置transferMode="Streamed"。當使用transfermode作爲Buffered時,它實際上將整個文件放在內存中,然後上傳/下載。因此,在Web客戶端和WCF服務主機上都需要一個較大的緩衝區。由於無需大容量內存緩衝區,Streamed傳輸可以提高服務的可伸縮性。有關transfermode的詳細信息,請參閱MSDN上關於TransferMode Enumeration的文章

+0

感謝您的建議。雖然沒有幫助。它在我看來,它只是忽略了我的綁定,並使用默認的。我不確定爲什麼或如何 – Dmitry 2012-03-14 16:37:33

0

好吧,經過一天的努力,我終於找到了一個問題。 我必須確保在WCF web.config中標記的名稱命名空間和服務的名稱相匹配:

<service name="ServicesImplementation.WcfServices.CommonService"> 

不幸的是它不是東西,你們會看到基於的信息我提供。