【问题标题】:Json web service max content length?Json Web服务最大内容长度?
【发布时间】:2023-03-24 03:50:01
【问题描述】:

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

我知道我正在发送有效的 json,我发送的是一个包含大约 1000 个项目的数组,每个项目的 json 如下所示: {"oid":0,"am":1,"me":2,"ofooid":0,"fooid":1104,"sync":1,"type":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>

【问题讨论】:

    标签: wcf web-services json


    【解决方案1】:

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

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

    <httpRuntime maxRequestLength="10000" />
    

    【讨论】:

    • 谢谢,这就是我想要的!您认为 maxReceivedMessageSize 的值是多少?我不想发送太多数据,也许我应该将请求分成较小的大小并连接几次?
    【解决方案2】:

    就增加请求的大小而言,上面提到的答案是正确的,但是如果您想增加 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-17
      • 2013-05-27
      • 2011-04-10
      相关资源
      最近更新 更多