2010-06-17 76 views
7

我在指定主機web.config中的dataContractSerializer maxItemsInObjectGraph時遇到問題。web.config中的WCF服務dataContractSerializer maxItemsInObjectGraph

<behaviors> 
    <serviceBehaviors> 
    <behavior name="beSetting"> 
     <serviceMetadata httpGetEnabled="True"/> 
     <serviceDebug includeExceptionDetailInFaults="True" /> 
     <dataContractSerializer maxItemsInObjectGraph="2147483646"/> 
    </behavior> 
    </serviceBehaviors> 
</behaviors> 
<services> 
    <service name="MyNamespace.MyService" 
      behaviorConfiguration="beSetting" > 
    <endpoint address="http://localhost/myservice/" 
       binding="webHttpBinding" 
       bindingConfiguration="webHttpBinding1" 
       contract="MyNamespace.IMyService" 
       bindingNamespace="MyNamespace"> 
    </endpoint> 
    </service> 
</services> 

以上對我的數據拉動沒有影響。由於大量數據,服務器超時。

我可以然而,在代碼中指定的最大限額和工作

[ServiceBehavior(MaxItemsInObjectGraph=2147483646, IncludeExceptionDetailInFaults = true)] 
    public abstract class MyService : MyService 
    { 
    blah... 
} 

有誰知道爲什麼我不能通過一個web.config設置,使這項工作?我想保留在web.config中,以便將來的更新更容易。

+0

我也有這個問題。我想知道爲什麼沒有人回答?感謝能夠在代碼中設置'ServiceBehavior'的提示,它至少可以讓我感動。 – jocull 2011-07-05 06:00:54

+0

你知道maxItemsInObjectGraph只定義了響應中允許的元素的總數,而不是直接響應的大小是啊? (我想如果沒有指定它默認爲60k xml元素) – 2011-08-10 14:42:32

+0

戴夫,你的問題是否回答了?如果給定的答案解決了您的問題,請將其標記爲如此。 – Bardia 2012-12-17 19:56:56

回答

12

在你的行爲部分中,添加的終結點行爲與DataContractSerializer的,就像這樣:

<endpointBehaviors> 
    <behavior name="LargeQuotaBehavior"> 
    <dataContractSerializer maxItemsInObjectGraph="2147483646"/> 
    </behavior> 
</endpointBehaviors> 

然後修改您的端點使用此行爲,像這樣:

<endpoint address="http://localhost/myservice/" 
      binding="webHttpBinding" 
      bindingConfiguration="webHttpBinding1" 
      contract="MyNamespace.IMyService" 
      bindingNamespace="MyNamespace" 
      behaviorConfiguration="LargeQuotaBehavior"> 

這應該解決您的問題。

+1

就像一個魅力。謝謝 – SergioM 2014-06-06 07:26:37

+1

今天我遇到了類似的問題。即使WCF在端點行爲中存在,Wcf也會拋出超出maxItemsInObjectGraph的異常。然後我將其轉移到解決問題的服務行爲中.http://stackoverflow.com/questions/26610861/passing-comma-separated-string-value-to-a-wcf-rest-service/26613810#26613810 – user1131926 2014-10-28 17:04:07