简体   繁体   中英

ASP.Net WebForm c# WebService - The length of the string exceeds the value set on the maxJsonLength property

I'm using ASP.Net WebForm & a C# Web Service and am getting the below error when running my code;

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

I've tried putting the below into the web config but it's not resolved it;

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

My Web Service;

        public class OpenRequisitions
    {
        public string string1 { get; set; }
        public string sstring2 { get; set; }
        public string string3 { get; set; }
        public string string4 { get; set; }
        public string string5 { get; set; }
    }

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public List<OpenRequisitions> GetOpenReqData(string ReqId, string RequisitionTitle, string City, string Country, string DateCreated)
    { 
        string connectionString = ConfigurationManager.ConnectionStrings["CONN"].ConnectionString;
        string commandTextGetOpenRequisitions = Properties.Queries.commandTextGetOpenRequisitions;
        List<OpenRequisitions> GetOpenRequisitionData = new List<OpenRequisitions>();
        using (SqlConnection con = new SqlConnection(connectionString))
        {
            SqlCommand command = new SqlCommand(commandTextGetOpenRequisitions, con);
            command.CommandType = CommandType.Text;
            con.Open();
            SqlDataReader rdr = command.ExecuteReader();
            while (rdr.Read())
            {
                OpenRequisitions results = new OpenRequisitions();
                results.ReqId = rdr["string1"].ToString();
                results.RequisitionTitle = rdr["string2"].ToString();
                results.City = rdr["string3"].ToString();
                results.Country = rdr["string4"].ToString();
                results.DateCreated = rdr["string5"].ToString();

                GetOpenRequisitionData.Add(results);
            }
        }
        return GetOpenRequisitionData;
    }

It looks like the problem is related to JavaScriptSerializer. You can set a max value on that directly. It doesn't inherit the value from web.config.

// Create an instance of your JavaScriptSerializer and set the MaxJsonLength.
var serializer = new JavaScriptSerializer() { MaxJsonLength = 86753090 };

// Perform your serialization
serializer.Serialize("Your JSON Contents"); 

Source

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM