Json网络服务最大内容长度?

时间:2009-10-06 14:13:59

标签: wcf web-services json

我一直在使用json格式构建Asp.net WCF Web服务。现在我想真正测试它在发送大量数据时的工作方式。我的http帖子的内容长度是65595.直接尝试连接时,我收到错误“HTTP / 1.1 400 Bad Request”。似乎它甚至没有尝试。

我知道我正在发送有效的json,我发送的是一个包含大约1000个项目的数组,每个项目的json看起来像这样: { “OID”:0, “AM”:1, “我”:2 “ofooid”:0 “fooid”:1104, “同步”:1, “类型”:1, “ID”:1443,”日期 “:” 2009-09-24" }

如果我只是删除数组中的一个项目,那么总内容长度为65484就完美了。所以在某处似乎是一个神奇的限制。 Asp.net是否限制了请求的大小,如果是这种情况,我该如何更改最大大小?

我的Web.Config文件看起来像,我想我应该在这里设置最大值,但我不知道在哪里:

<system.serviceModel>
    <behaviors>
       <endpointBehaviors>
           <behavior name="ServiceAspNetAjaxBehavior">
                <enableWebScript  />
           </behavior>
       </endpointBehaviors>
       <serviceBehaviors>
           <behavior name="ServiceBehavior">
                <serviceDebug includeExceptionDetailInFaults="true" />
           </behavior>
       </serviceBehaviors>
    </behaviors>
    <services>
        <service behaviorConfiguration="ServiceBehavior" name="Service">
            <endpoint address="" behaviorConfiguration="ServiceAspNetAjaxBehavior" binding="webHttpBinding" contract="Service" />
        </service>
    </services>
</system.serviceModel>

2 个答案:

答案 0 :(得分:4)

您需要在WebHttpBinding的绑定配置中增加maxReceivedMessageSize。默认值为65536.有关所有信息,请参阅WebHttpBinding configuration documentation

另请注意,您可能需要通过httpRuntime配置增加ASP.NET maxRequestLength。默认值为4 MB,但您可能需要增加:

<httpRuntime maxRequestLength="10000" />

答案 1 :(得分:3)

就增加请求的大小而言,上面提到的答案是正确的,但是如果你想增加json响应的大小,那么你可以通过在endpointBehaviors中进行更改来实现这一点,如下所述。

此外,响应可能因数据嵌套而异,因为我们可能会返回带有嵌套属性的列表。

假设端点如下:

<endpoint address="" binding="webHttpBinding" contract="WcfService1.IService1" behaviorConfiguration="ClientBehavior">
For Client
        <endpointBehaviors>
          <behavior name="ClientBehavior">
            <dataContractSerializer maxItemsInObjectGraph="10000000"/>
          </behavior>
        </endpointBehaviors>

For Server
      <serviceBehaviors>
        <behavior name="HostBehavior">
          <dataContractSerializer maxItemsInObjectGraph="10000000"/>
        </behavior>
      <serviceBehaviors>