Tuesday, November 1, 2011

Step 8 : WCF RESTful GET JSON List with FIDDLER


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 : :

The code behind the above link : 

        [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);
            string sqlSelect = "select * from users ";
            SqlDataAdapter da = new SqlDataAdapter(sqlSelect, dbConn);
            DataTable dt = new DataTable();
            SqlCommand dbCommand = new SqlCommand(sqlSelect, dbConn);
            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);
            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