Tuesday, November 1, 2011

Step 8 : WCF RESTful GET JSON List with FIDDLER


http://pantestmb.blogspot.com/2011/10/step-3-from-soap-to-wcf-restful-get.html

In Step 3 there was a sample of transforming a SOAP Web Service into a WCF RESTful one
with "application/xml" Content-Type ;

In step 7 - there are all the details about the full source code

To be accessed from a Android Client the sample must switch from XML to JSON data-content ;

Let's consider that the webservice is installed on the IP : 192.168.61.3 :
http://192.168.61.3/RestServicePost/RestServiceImpl.svc/json/getallpersons

The code behind the above link : 

        [OperationContract]
        [WebInvoke(Method = "GET",
            ResponseFormat = WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.Wrapped,
            UriTemplate = "json/getallpersons")]
        PersonData[] getJsonPersons();

        public PersonData[] getJsonPersons()
        {
            return getAllPersons();
        }


        public PersonData[] getAllPersons()
        {
            SqlConnection dbConn = new SqlConnection(connStr);
            dbConn.Open();
            string sqlSelect = "select * from users ";
            SqlDataAdapter da = new SqlDataAdapter(sqlSelect, dbConn);
            DataTable dt = new DataTable();
            SqlCommand dbCommand = new SqlCommand(sqlSelect, dbConn);
            da.Fill(dt);
            dbConn.Close();
            List list = new List();
            foreach (DataRow row in dt.Rows)
            {
                // Person target = Activator.CreateInstance();
                PersonData target = new PersonData();
                target.Name = row["Name"].ToString();
                target.User = row["UserName"].ToString();
                target.Email = row["EMail"].ToString();
                target.Password = row["Password"].ToString();
                // DataColumnAttribute.Bind(row,target);
                list.Add(target);
            }
            return list.ToArray();
        }

Pictures about what the code does ;
FIDDLER gets from the webservice all the persons from the database in JSON format :







No comments:

Post a Comment