Friday, October 21, 2011

Step 3 : From SOAP to WCF-RESTful-GET



Step 3 : From SOAP to WCF-RESTful-GET

Google Docs PDF :
https://docs.google.com/viewer?a=v&pid=explorer&chrome=true&srcid=0BzKVfKe--t_cZjM4YWU2NWMtZDQ3Yy00ZWZmLTgyYWYtZDQyM2EzN2EzYjVk&hl=en_GB


SOAP :
[WebMethod]
public List GetPersonList()
{
   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 myList = new List();
   foreach (DataRow row in dt.Rows)
   {
      Person target = new Person();
      target.Name = row["Name"].ToString();
      target.UserName = row["UserName"].ToString();
      target.EMail = row["EMail"].ToString();
      target.Password = row["Password"].ToString();
      myList.Add(target);
   }
   return myList;
}



public class Person
{
private string _name = string.Empty;
private string _user_name = string.Empty;
private string _eMaiL = string.Empty;
private string _password = string.Empty;
public Person() {}
public string Name
{
get { return _name; }
set { _name = value; }
}
public string Password
{
get { return _password; }
set { _password = value; }
}
public string UserName
{
get { return _user_name; }
set { _user_name = value; }
}
public string EMail
{
get { return _eMaiL; }
set { _eMaiL = value; }
}
}


WCF REST : getAllPersons

[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "xml/getallpersons")]
PersonData[] getAllPersons();

[DataContract]
public class PersonData
{
[DataMember]
public string Name { get; set; }
[DataMember]
public string User { get; set; }
[DataMember]
public string Email { get; set; }
[DataMember]
public string Password { get; set; }
}


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<PersonData> list = new List<PersonData>();
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();
}





http://192.168.61.3/RestServicePost/RestServiceImpl.svc/xml/getallpersons





VWD 2010 Express - full project : 
https://docs.google.com/leaf?id=0BzKVfKe--t_cMmUxYTlkY2UtZmIzOC00YjhjLWExN2MtNDk3MWFhNDQ4ZWY0&hl=en_GB






http://192.168.61.3/RestServicePost/RestServiceImpl.svc/json/getallpersons

More about JSON part of the RESTful WebService on :

Step 8 : WCF RESTful GET JSON List with FIDDLER



No comments:

Post a Comment