LA.NET [EN]

Jul 16

Today I”ve seen an interesting question on the Silverlight forum. The question was: how to change the MaxJsonLenght property of the JavaScriptSerializer object that is used on the serialization of the objects that are passed to the web service?

Answer: well, you need to use the BrowserHttpWebRequest object and create a custom JavaScriptSerializer object. Here”s a quick example. Lets start by creating a custom class that is used to return the response of an asmx web service:

public class Test
{
     private string _nome;
     private string _morada;

     public string Nome
    {
         get {  return _nome; }
         set { _nome = value; }
    }

    public string Morada
    {
        get { return _morada; }
        set { _morada = value;}
    }
}

Now, the simple web service:

[WebMethod]
[ScriptMethod]
public Test HelloWorld(string info) {
   Test t = new Test();
   t.Nome = “LKLLL”
   t.Morada = info;
   return t;
}

Pretty simple stuff…Now, the interesting stuff: the code you need to use to call the web service:

Test ret;
string path = “WebService.asmx/HelloWorld”
using (BrowserHttpWebRequest request =
       new BrowserHttpWebRequest(new Uri(path, UriKind.Relative)))
{
    request.ContentType = “application/json”
    request.Method = “POST”
    HttpWebResponse response = null;
    using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
    {
         StringBuilder str = new StringBuilder();
         str.Append(“{“);
         str.Append(“”info”:”);
         JavaScriptSerializer converter = new JavaScriptSerializer();
         converter.Serialize(“address”, str);
         str.Append(“}”);
         writer.Write(str.ToString());
         writer.Flush();
         response = request.GetResponse();
   }
   if (response.StatusCode != HttpStatusCode.OK
   {
       //check for error and inform user 
   }
   using (StreamReader reader = new StreamReader(response.GetResponseStream()))
  {
      JavaScriptSerializer converter = new JavaScriptSerializer();
      string json = reader.ReadToEnd();
      ret = converter.Deserialize<Test>(json);
      if (ret != null)
      {
          Debug.WriteLine(ret.Nome);
          Debug.WriteLine(ret.Morada);
      }
      else
     {
         Debug.WriteLine(“ret is null”);
     }
  }

}

Important things to retain from the demo sample:

  • Don”t forget to add the name of the method you”re calling to the url;
  • When you”re using JSON as the serialization format, you must set the  content type to application/json;
  • I really didn”t need to use the serializer to serialize the string; i could simply add it to the string builder but I”ve opted for using the serializer for demonstrating purposes;
  • You really need to get a reference to the response from within the StreamWriter using block;
  • Deserializing the return result is easy: you just need to get the JSON string and pass it to the generic Deserialize method.

6 comments so far

  1. skepuploows
    7:41 pm - 1-1-2010

    ?????±???»?????‹?? ?????°???µ?? ?????°?‡?°?‚?? nokia 6120 ?????±???»?????‹?? ???µ???‚???µ?? ?????°???µ?? ?????°?‡?°?‚?? ?±?µ?????»?°?‚???? http://www.pyzam.com/profile/3314400 ?????°?‡?°?‚?? ?????±???»?????‹?? ?????„?‚ ?????°???µ?? ?????‚?????‹?? ?????±???»?????‹?? ?????°???µ?? ?????°?‡?°?‚?? ?±?µ?????»?°?‚????

  2. hoideGurori
    5:06 pm - 1-12-2010

    http://multi672.megafilms2.ru – ????? ?????? http://heater763.megafilms2.ru – ????? ????? ?????? http://petal694.megafilms2.ru – ?????? ??????? ????? ???????? ?????? http://trott559.megafilms2.ru – ????? ?????? ????? ?????? http://informative931.megafilms2.ru – ????? ??????? ??????? ??????? ???? http://puxi202.megafilms2.ru – ????? ???????? ???? 2007 ?????? http://aabar233.megafilms2.ru – ??????? ????? ?????? http://chandra259.megafilms2.ru – ????? ????? ???? ??????? ??????? http://hallways319.megafilms2.ru – ????? ????? ????? http://felicity815.megafilms2.ru – ?? ??????? ?????? ????? http://satisfying771.megafilms2.ru – ??????? ????? ????????? ???? http://sheet887.megafilms2.ru – ????? ????? ?????????? ? ??????? http://honed604.megafilms2.ru – ?????? ?? ?????? ??????? http://transact460.megafilms2.ru – ???????? 78 ????? ?????? ?????? http://greatly621.megafilms2.ru – ????? ??????????? ?????? ?????? http://tacardon232.megafilms2.ru – ????? ?? ?????? ???? 2 http://wang110.megafilms2.ru – ??????? ????? ??? ?????? ???? http://gardening513.megafilms2.ru – ????? 13 ? ????? http://bragg769.megafilms2.ru – ? ?????? ???? ????? ????????? http://plit963.megafilms2.ru – ???????? ????? ?? ????????? ?????? http://init918.megafilms2.ru – ?????? ?????? ???? http://accrual672.megafilms2.ru – ??????? ??? ????? http://worrying903.megafilms2.ru – ???????? 2 ????? 2009 http://interpreted468.megafilms2.ru – ????????? ???? ????? 2008 http://vallisneri109.megafilms2.ru – ????? ???????? ?????? ???? http://verdanau454.megafilms2.ru – ?????? ??????? ????? http://sito799.megafilms2.ru – ?????? ?????? ?????????? ????????? http://scaggs988.megafilms2.ru – ??? ???? ???????????? http://caseyville698.megafilms2.ru – ??????? ????? ???? ?????? http://backed947.megafilms2.ru – ??????? ?????????? ????? ??????? ?????

  3. hoideGurori
    5:06 pm - 1-12-2010

    http://multi672.megafilms2.ru – ????? ?????? http://heater763.megafilms2.ru – ????? ????? ?????? http://petal694.megafilms2.ru – ?????? ??????? ????? ???????? ?????? http://trott559.megafilms2.ru – ????? ?????? ????? ?????? http://informative931.megafilms2.ru – ????? ??????? ??????? ??????? ???? http://puxi202.megafilms2.ru – ????? ???????? ???? 2007 ?????? http://aabar233.megafilms2.ru – ??????? ????? ?????? http://chandra259.megafilms2.ru – ????? ????? ???? ??????? ??????? http://hallways319.megafilms2.ru – ????? ????? ????? http://felicity815.megafilms2.ru – ?? ??????? ?????? ????? http://satisfying771.megafilms2.ru – ??????? ????? ????????? ???? http://sheet887.megafilms2.ru – ????? ????? ?????????? ? ??????? http://honed604.megafilms2.ru – ?????? ?? ?????? ??????? http://transact460.megafilms2.ru – ???????? 78 ????? ?????? ?????? http://greatly621.megafilms2.ru – ????? ??????????? ?????? ?????? http://tacardon232.megafilms2.ru – ????? ?? ?????? ???? 2 http://wang110.megafilms2.ru – ??????? ????? ??? ?????? ???? http://gardening513.megafilms2.ru – ????? 13 ? ????? http://bragg769.megafilms2.ru – ? ?????? ???? ????? ????????? http://plit963.megafilms2.ru – ???????? ????? ?? ????????? ?????? http://init918.megafilms2.ru – ?????? ?????? ???? http://accrual672.megafilms2.ru – ??????? ??? ????? http://worrying903.megafilms2.ru – ???????? 2 ????? 2009 http://interpreted468.megafilms2.ru – ????????? ???? ????? 2008 http://vallisneri109.megafilms2.ru – ????? ???????? ?????? ???? http://verdanau454.megafilms2.ru – ?????? ??????? ????? http://sito799.megafilms2.ru – ?????? ?????? ?????????? ????????? http://scaggs988.megafilms2.ru – ??? ???? ???????????? http://caseyville698.megafilms2.ru – ??????? ????? ???? ?????? http://backed947.megafilms2.ru – ??????? ?????????? ????? ??????? ?????

  4. hoideGurori
    5:07 pm - 1-12-2010

    http://multi672.megafilms2.ru – ????? ?????? http://heater763.megafilms2.ru – ????? ????? ?????? http://petal694.megafilms2.ru – ?????? ??????? ????? ???????? ?????? http://trott559.megafilms2.ru – ????? ?????? ????? ?????? http://informative931.megafilms2.ru – ????? ??????? ??????? ??????? ???? http://puxi202.megafilms2.ru – ????? ???????? ???? 2007 ?????? http://aabar233.megafilms2.ru – ??????? ????? ?????? http://chandra259.megafilms2.ru – ????? ????? ???? ??????? ??????? http://hallways319.megafilms2.ru – ????? ????? ????? http://felicity815.megafilms2.ru – ?? ??????? ?????? ????? http://satisfying771.megafilms2.ru – ??????? ????? ????????? ???? http://sheet887.megafilms2.ru – ????? ????? ?????????? ? ??????? http://honed604.megafilms2.ru – ?????? ?? ?????? ??????? http://transact460.megafilms2.ru – ???????? 78 ????? ?????? ?????? http://greatly621.megafilms2.ru – ????? ??????????? ?????? ?????? http://tacardon232.megafilms2.ru – ????? ?? ?????? ???? 2 http://wang110.megafilms2.ru – ??????? ????? ??? ?????? ???? http://gardening513.megafilms2.ru – ????? 13 ? ????? http://bragg769.megafilms2.ru – ? ?????? ???? ????? ????????? http://plit963.megafilms2.ru – ???????? ????? ?? ????????? ?????? http://init918.megafilms2.ru – ?????? ?????? ???? http://accrual672.megafilms2.ru – ??????? ??? ????? http://worrying903.megafilms2.ru – ???????? 2 ????? 2009 http://interpreted468.megafilms2.ru – ????????? ???? ????? 2008 http://vallisneri109.megafilms2.ru – ????? ???????? ?????? ???? http://verdanau454.megafilms2.ru – ?????? ??????? ????? http://sito799.megafilms2.ru – ?????? ?????? ?????????? ????????? http://scaggs988.megafilms2.ru – ??? ???? ???????????? http://caseyville698.megafilms2.ru – ??????? ????? ???? ?????? http://backed947.megafilms2.ru – ??????? ?????????? ????? ??????? ?????

  5. hoideGurori
    5:07 pm - 1-12-2010

    http://multi672.megafilms2.ru – ????? ?????? http://heater763.megafilms2.ru – ????? ????? ?????? http://petal694.megafilms2.ru – ?????? ??????? ????? ???????? ?????? http://trott559.megafilms2.ru – ????? ?????? ????? ?????? http://informative931.megafilms2.ru – ????? ??????? ??????? ??????? ???? http://puxi202.megafilms2.ru – ????? ???????? ???? 2007 ?????? http://aabar233.megafilms2.ru – ??????? ????? ?????? http://chandra259.megafilms2.ru – ????? ????? ???? ??????? ??????? http://hallways319.megafilms2.ru – ????? ????? ????? http://felicity815.megafilms2.ru – ?? ??????? ?????? ????? http://satisfying771.megafilms2.ru – ??????? ????? ????????? ???? http://sheet887.megafilms2.ru – ????? ????? ?????????? ? ??????? http://honed604.megafilms2.ru – ?????? ?? ?????? ??????? http://transact460.megafilms2.ru – ???????? 78 ????? ?????? ?????? http://greatly621.megafilms2.ru – ????? ??????????? ?????? ?????? http://tacardon232.megafilms2.ru – ????? ?? ?????? ???? 2 http://wang110.megafilms2.ru – ??????? ????? ??? ?????? ???? http://gardening513.megafilms2.ru – ????? 13 ? ????? http://bragg769.megafilms2.ru – ? ?????? ???? ????? ????????? http://plit963.megafilms2.ru – ???????? ????? ?? ????????? ?????? http://init918.megafilms2.ru – ?????? ?????? ???? http://accrual672.megafilms2.ru – ??????? ??? ????? http://worrying903.megafilms2.ru – ???????? 2 ????? 2009 http://interpreted468.megafilms2.ru – ????????? ???? ????? 2008 http://vallisneri109.megafilms2.ru – ????? ???????? ?????? ???? http://verdanau454.megafilms2.ru – ?????? ??????? ????? http://sito799.megafilms2.ru – ?????? ?????? ?????????? ????????? http://scaggs988.megafilms2.ru – ??? ???? ???????????? http://caseyville698.megafilms2.ru – ??????? ????? ???? ?????? http://backed947.megafilms2.ru – ??????? ?????????? ????? ??????? ?????

  6. call center philippines
    3:46 am - 5-20-2010

    Call center companies who are going to use this kind of application will make the jobs of their agents a lot easier.

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>