.Net JSON WebService和返回限制

时间:2012-11-26 07:50:46

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

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

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

似乎很明显我在一次通话中达到了一定程度的默认限制,但我不能因为我这样想出如何增加这个限制。谷歌搜索指向设置maxJsonLength的方向,但这似乎没有任何影响。

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

我是配置/设置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>

2 个答案:

答案 0 :(得分:1)

<system.web.extensions>
    <scripting>
        <webServices>
            <jsonSerialization maxJsonLength="1000000000" />
        </webServices>
    </scripting>
</system.web.extensions>

答案 1 :(得分:0)

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

在他们的辩护中,我必须说他们的支持非常优秀,而且我认为其他一些人会认为我的主张是一个发展问题。