【问题标题】:.Net JSON WebService and return limit.Net JSON WebService 和返回限制
【发布时间】:2012-11-26 07:50:46
【问题描述】:

我有一个 C# WebService 设置来返回 JSON 信息以在移动设备上使用。一切正常,除了现在抛出“HTTP 错误 502(坏网关):网关或代理服务器从上游服务器收到无效响应”的调用之一。

当我增加调用返回的数据时,错误开始了。数据是从驻留在服务器上的 JSON 文件中读取的。此数据文件的大小以前为 1,324,859 字节,但工作正常(现在仍然如此),新数据文件为 1,563,570 字节,这是失败的。

似乎很明显,我达到了某种默认限制,即我可以在一次调用中返回多少,但对于像我这样的人来说,我无法弄清楚如何增加这个限制。谷歌搜索指向设置 maxJsonLength 的方向,但这似乎没有任何效果。

下面是 WebService 的深入代码,更重要的是我的 web.config。我应该说调用在本地运行良好,这意味着在 VS2010 中运行时,但在服务器上失败(运行 IIS 7.5 的 GearHost)。

我是配置/设置 IIS 的新手,因此非常感谢任何帮助。

[ServiceContract]
public interface IMyService
{
  [OperationContract]
  [WebInvoke(Method = "GET", UriTemplate = "/GetItems", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
  Item[] GetItems();
}

public class MyService : IMyService
{
  public Item[] GetItems()
  {
      var result = ReadDataFile<List<Item>>(DataType.Items);
      return result == null ? null : result.ToArray();
  }
}

到目前为止,我已经尝试了很多设置,我什至不确定这里需要什么,什么不应该......

<?xml version="1.0"?>
<configuration>
  <configSections>
    <sectionGroup name="system.web.extensions"         type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
      <sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
        <section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication"/>
        <sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
          <section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="Everywhere" />
          <section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication" />
          <section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication" />
          <section name="roleService" type="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication" />
        </sectionGroup>
      </sectionGroup>
    </sectionGroup>
  </configSections>
  <system.web>
    <customErrors mode="On"/>
    <compilation debug="true" targetFramework="4.0" />
    <caching>
      <outputCacheSettings>
        <outputCacheProfiles>
          <add name="NoCacheProfile" noStore="true" duration="0" varyByParam="none" enabled="true"/>
        </outputCacheProfiles>
      </outputCacheSettings>
    </caching>
  </system.web>
  <system.serviceModel>
    <bindings>
      <webHttpBinding>
        <binding name="StreamedRequestWebBinding"
                 bypassProxyOnLocal="true"
                 useDefaultWebProxy="false"
                 hostNameComparisonMode="WeakWildcard"
                 sendTimeout="10:15:00"
                 openTimeout="10:15:00"
                 receiveTimeout="10:15:00"
                 maxReceivedMessageSize="2147483647"
                 maxBufferSize="2147483647"
                 maxBufferPoolSize="2147483647"
                 transferMode="StreamedRequest">
          <readerQuotas maxArrayLength="2147483647"
                        maxStringContentLength="2147483647" />
        </binding>
      </webHttpBinding>
    </bindings>
    <services>
      <service name="JSON_WebService.WoWService" behaviorConfiguration="ServiceBehaviour">
        <endpoint address ="" binding="webHttpBinding" bindingConfiguration="StreamedRequestWebBinding" contract="JSON_WebService.IWoWService" behaviorConfiguration="web">
        </endpoint>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehaviour">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="web">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
  <system.web.extensions>
    <scripting>
      <webServices>
        <jsonSerialization maxJsonLength="50000000"/>
      </webServices>
    </scripting>
  </system.web.extensions>
</configuration>

【问题讨论】:

    标签: c# asp.net .net json web-services


    【解决方案1】:
    <system.web.extensions>
        <scripting>
            <webServices>
                <jsonSerialization maxJsonLength="1000000000" />
            </webServices>
        </scripting>
    </system.web.extensions>
    

    【讨论】:

    • 这似乎完全没有效果。我确实认为我的 50 Mb 设置对于 1.5 Mb 数据源来说应该足够了,即使 Web 服务引入了任何开销
    • 好吧,我错了。这确实解决了问题,只是花了很长时间才使更改变得“活跃”(我无法轻松停止/启动 GearHost 上的服务)。现在我只是担心消息的实际大小,因为它应该通过移动网络下载......
    • 好吧,这越来越奇怪了。现在它再次停止工作,从那以后我没有改变任何东西。想知道是不是服务器有问题...
    【解决方案2】:

    在与 GearHost(托管 Web 服务的)来回通信后,他们确认我的问题与服务器错误有关,而不是与编程/web.config 错误有关。 jsonSerialization 设置应该已经足够了。

    在他们的辩护中,我必须说他们的支持确实非常出色,我认为很多其他人会认为我的主张是一个发展问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多