在web.config中设置WCF服务的DataContractSerializer maxItemsInObjectGraph

8

我在主机的web.config中指定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中,这样以后更新会更容易。


我也遇到了这个问题。不知道为什么没有其他人回答?感谢您提供的有关在代码中设置“ServiceBehavior”的提示,这可能至少可以让我继续前进。 - jocull
你知道maxItemsInObjectGraph只定义了响应中允许的元素总数,而不是响应大小吗?(我认为如果没有指定,默认为60k个xml元素) - Nick Ryan
Dave,你的问题是否得到了解答?如果所给的答案解决了你的问题,请标记它为已解决。 - Bardia
1个回答

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
今天我遇到了一个类似的问题。尽管在端点行为中设置了 maxItemsInObjectGraph,但 WCF 仍会抛出有关其超限的异常。然后我将其移动到服务行为中,问题得以解决。http://stackoverflow.com/questions/26610861/passing-comma-separated-string-value-to-a-wcf-rest-service/26613810#26613810 - user1131926

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接