2011-12-05 52 views
16

我遇到了這個我試圖處理的瘋狂問題。我知道,當我們獲取大量數據時,我們必須增加客戶端.config文件的配額,但是如果我的客戶端正在向WCF服務器發送大量數據,我該怎麼辦?傳入消息的最大消息大小配額(65536)....要提高配額,請使用MaxReceivedMessageSize屬性

當我發送小尺寸輸入參數時,它完美正常工作。不幸的是,當輸入變大時,它就會崩潰。

調試器,它說:

錯誤的請求,400;

上跟蹤文件,它是:

傳入的消息(65536)的最大消息大小配額 被超過。要增加配額,請在適當的綁定元素上使用MaxReceivedMessageSize 屬性。

有什麼方法可以增加服務器端數據傳輸的配額嗎?如果是這樣,怎麼樣?

這裏是我的示例配置相關的部分:

<bindings> 
    <basicHttpBinding> 
    <binding name="MyBasicHttpBinding" 
     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="64" 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="MyWcfService"> 
    <endpoint address="http://myservice..." 
     binding="basicHttpBinding" bindingConfiguration="MyBasicHttpBinding" 
     name="MyBasicHttpBinding" contract="IMyContract" /> 
    </service> 
</services> 

,這裏是我的客戶端代碼(我動態創建一個):

 var binding = new BasicHttpBinding(); 
     binding.MaxBufferPoolSize = 2147483647; 
     binding.MaxBufferSize = 2147483647; 
     binding.MaxReceivedMessageSize = 2147483647; 
     binding.ReaderQuotas.MaxStringContentLength = 2147483647; 
     binding.ReaderQuotas.MaxArrayLength = 2147483647; 
     binding.ReaderQuotas.MaxDepth = 2147483647; 
     binding.ReaderQuotas.MaxBytesPerRead = 2147483647; 


     var address = new EndpointAddress("http://mylocalservice.."); 

     ChannelFactory<IMyContract> factory = new ChannelFactory<IMyContract>(binding, address); 

     foreach (OperationDescription op in factory.Endpoint.Contract.Operations) 
     { 
      DataContractSerializerOperationBehavior dataContractBehavior = 
         op.Behaviors.Find<DataContractSerializerOperationBehavior>() 
         as DataContractSerializerOperationBehavior; 
      if (dataContractBehavior != null) 
      { 
       dataContractBehavior.MaxItemsInObjectGraph = 2147483646; 
      } 
     } 
     public IMyContract MyClientObject = factory.CreateChannel(); 
+0

我通過上述[這裏]的步驟解決了這個問題[1] [1]:HTTP://計算器。COM /問題/ 7476853/WCF的錯誤最大數的項,也就是說,可以待連載 - 或反序列化功能於一/ 8656402#8656402 – Ram

+1

在您的客戶端代碼要設置通過代碼readerquotas所以下面的是設置他們的方法: XmlDictionaryReaderQuotas myReaderQuotas =新XmlDictionaryReaderQuotas(); myReaderQuotas.MaxStringContentLength = 2147483647; myReaderQuotas.MaxArrayLength = 2147483647; myReaderQuotas.MaxBytesPerRead = 2147483647; myReaderQuotas.MaxDepth = 64; myReaderQuotas.MaxNameTableCharCount = 2147483647; binding.GetType()的getProperty( 「ReaderQuotas」)的SetValue(結合,myReaderQuotas,空)。; – Rajesh

回答

36

您可以設置MaxReceivedMessageSize屬性經由服務服務的配置文件。

設置客戶端的MaxReceivedMessageSize僅影響客戶端收到的消息;它對服務收到的消息沒有影響。相應地,爲了允許服務接收較大的消息,您需要設置服務的配置文件。

樣品服務配置

<system.serviceModel> 
    <bindings> 
    <wsHttpBinding> 
     <binding name="MyBinding" maxReceivedMessageSize="2147483647" /> 
    </wsHttpBinding> 
    </bindings> 
    <services> 
    <service name="MyService"> 
     <endpoint address="http://myservice" binding="wsHttpBinding" 
       bindingConfiguration="MyBinding" contract="IMyServiceContract" /> 
    </service> 
    </services> 
</system.serviceModel> 

以上是一個非常精簡配置文件,但顯示的相關部分。最重要的部分是您定義綁定併爲其指定名稱,並顯式設置與默認值不同的任何值,並在endpoint元素的bindingConfiguration屬性中使用該名稱。

+0

事情是我沒有看到服務器端的任何綁定配置。有沒有一些默認綁定或什麼?我怎麼能覆蓋它? –

+1

是的,有。如果你沒有在你的配置文件中的特定的結合部分,它不是在通過bindingConfiguration屬性''元素引用,爲綁定默認值將被使用(這是65536 MaxReceivedMessageSize屬性)。因此,您需要將相應的''部分添加到配置中。 – Tim

+0

有什麼樣的服務器端綁定和/或端點配置的例子?我已經停留在那一點。 –

5

此問題可以通過將下面的附加綁定節點添加到配置文件的綁定部分來解決。

<basicHttpBinding> 
    <binding maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"> 
    <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" 
     maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" /> 
    </binding> 
</basicHttpBinding> 
+3

歡迎StackOverflow的任何替代方法。 OP在其配置中具有非常類似的代碼。你的答案比哪種方式更好?你可以通過解釋他們之前沒有做過的答案來改善你的答案。 – carlosfigueira

6

我的問題是與wcf測試客戶端。出於某種原因,它不會生成增加maxReceivedMessageSize的客戶端配置。我的修復是右鍵單擊「配置文件」 - >「使用SvcConfigEditor編輯」 - >綁定 - > maxReceivedMessageSize。

+0

在大多數答案中,他們展示瞭如何在服務器上設置這些設置。但如果仍然錯誤,那麼,正如你所提到的,我們也應該在客戶端上配置這些設置。 在我的情況下,首先消耗web服務的應用程序的的app.config看起來像這樣: <綁定名稱= 「BasicHttpBinding_ICGSIncomingSOAPCalls」/> <綁定名稱= 「BasicHttpBinding_MyICGSIncomingSOAPCalls」/> 我修改這樣的(見部分2):... – mihai71

+0

部分2: <綁定名稱= 「BasicHttpBinding_ICGSIncomingSOAPCalls」 maxReceivedMessageSize = 「10485760」 maxBufferPoolSize = 「10485760」 MAXBUFFERSIZE =」 10485760「/> <綁定名稱= 「BasicHttpBinding_MyICGSIncomingSOAPCalls」 maxReceivedMessageSize = 「10485760」 maxBufferPoolSize = 「10485760」 MAXBUFFERSIZE = 「10485760」/> 和它的工作正常。 – mihai71

相關問題