RESTful WCF(4.5.2)服务错误,“413实体请求过大”

时间:2015-11-12 20:42:25

标签: c# .net web-services wcf rest

这似乎是一个非常普遍的问题 - 我在SO和其他网站上阅读了大量评论。问题是即使在修复我的配置后,我仍然在发送~50K有效载荷时收到413错误。

这是服务配置的相关部分......

<system.serviceModel>
<serviceHostingEnvironment multipleSiteBindingsEnabled="false" />
<services>
  <service name="Sync.Inbound" behaviorConfiguration="serviceWebConfiguration">
    <endpoint address="" binding="webHttpBinding" bindingName="defaultRest" behaviorConfiguration="web" contract="Sync.IInbound"  />
  </service>
</services>
<bindings>
  <webHttpBinding>
    <binding name="defaultRest" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" maxBufferPoolSize="2147483647" transferMode="Streamed">
      <readerQuotas maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxDepth="2147483647" maxNameTableCharCount="2147483647" />
      <security mode="None" />
    </binding>
  </webHttpBinding>
</bindings>
<behaviors>
  <endpointBehaviors>
    <behavior name="web">
      <webHttp  />
      <dataContractSerializer  maxItemsInObjectGraph="2147483647"  />
    </behavior>
    <behavior name="json">
      <enableWebScript />
    </behavior>
  </endpointBehaviors>
  <serviceBehaviors>
    <behavior name="serviceWebConfiguration">
      <serviceMetadata httpGetEnabled="true" />
    </behavior>
  </serviceBehaviors>
</behaviors>

我正在测试通过一个简单的C#控制台应用程序通过httpWebRequest调用它...任何人都看到我在服务配置中缺少的东西?

2 个答案:

答案 0 :(得分:1)

您需要在 服务端点 元素上将k属性设置为bindingConfiguration,否则服务将不会使用指定的绑定配置块。

上面的示例显示了&#34; bindingName&#34;中的绑定配置名称。属性而不是&#34; bindingConfiguration&#34;属性。 因此,您需要更改:

"defaultRest"

到:

<endpoint ... bindingName="defaultRest"

示例:

<endpoint ... bindingConfiguration="defaultRest"

参考:
https://msdn.microsoft.com/en-us/library/ms733099(v=vs.110).aspx
bindingConfiguration vs bindingName

答案 1 :(得分:0)

您可能希望为服务maxItemsInObjectGraph更改dataContractSerializer以允许更大的收集。

<serviceBehaviors>
  <behavior name="SecureServiceBehavior">
    <serviceMetadata httpGetEnabled="true" httpsGetEnabled="false"/>
    <serviceDebug includeExceptionDetailInFaults="true"/>
    <dataContractSerializer ignoreExtensionDataObject="true" maxItemsInObjectGraph="2147483647"/>
    <serviceTimeouts transactionTimeout="00:03:00"/>
    <serviceThrottling maxConcurrentCalls="10" maxConcurrentSessions="10" maxConcurrentInstances="10"/>
  </behavior>
</serviceBehaviors>