1

我有一个 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>
4

2 回答 2

1
<system.web.extensions>
    <scripting>
        <webServices>
            <jsonSerialization maxJsonLength="1000000000" />
        </webServices>
    </scripting>
</system.web.extensions>
于 2012-11-26T07:53:50.763 回答
0

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

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

于 2012-11-29T18:20:48.817 回答