【问题标题】:WebMethod - JSON Serialization ErrorWebMethod - JSON 序列化错误
【发布时间】:2012-07-05 20:04:30
【问题描述】:

我正在对 aspx.cs 页面中的后端 WebMethod 进行 jQuery AJAX 调用。我在 .NET JSON 序列化中遇到错误。因此,我正在寻找修复错误或避免使用 JSON(WebMethods 的唯一返回格式)的方法:

Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property

相关的 StackTrace 是: at System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj, StringBuilder output, SerializationFormat serializationFormat)\r\n at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary'2 rawParams)\r\n at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)

后端代码如下(注意:result 实际上是大约 110k 的控件呈现为字符串):

[WebMethod]
public static string GetContactListControl()
{
    try
    {
        var result = "Hello World!"
        return result;
    }
    catch (Exception e){
        Logging.LogException(e);
        return "Exception Thrown";
    }
}

而且我从来没有遇到过catch 块,这对我来说表明这个问题不在我的代码范围内。

我通过插入以下块找到了涉及更改 web.config 的修复程序,但它不起作用:

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

项目是 .NET 3.5。

感谢您的任何想法和建议!

【问题讨论】:

  • 我假设你的意思是当你说它“永远不会命中 catch 块”时,方法开头的断点永远不会被命中。使用原样的代码,我真诚地怀疑return "Hello World!" 可能会抛出异常。
  • 尝试将maxJsonLength 增加一吨。大约 110kb 大约是 112,640 字节,与 123,456 字节相差不远。也许额外的开销(或者“关于”不是太精确)正在打破限制。试着把它打到 9999999 看看会发生什么。
  • 更正断点正常。当我在 return result 上中断时,它显示结果的值没有任何问题。
  • 感谢 Chris 的建议,但使用 Web.Config 会抱怨 无效,并且搜索返回的 IIS 错误会使修复程序看起来只是 .NET 4.0。感谢 L.B.还!但由于项目限制,开源解决方案很难获得批准。
  • 上面提到的IIS错误:HTTP Error 500.19 - Internal Server Error The requested page cannot be accessed because the related configuration data for the page is invalid.

标签: c# json serialization .net-3.5 webmethod


【解决方案1】:

maxJsonLength 属性的配置必须在 web.config 中设置。为了允许 IIS 允许此配置,&lt;sectionGroup&gt; 必须包含在 &lt;configSections&gt; 中:

<sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
  <sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
    <section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
    <sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere"/>
      <section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
      <section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
      <section name="roleService" type="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
    </sectionGroup>
  </sectionGroup>
</sectionGroup>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-07
    • 2018-07-28
    • 2018-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多