Wednesday, October 19, 2011

Step 1 : From SOAP to WCF REST without LINQ



From SOAP to WCF REST without LINQ : small and easy steps
with sample code  


For my goal I don't must have a license of the full Visual Studio ;
I'm OK with Visual Web Developer 2010 Express .

RESTful webservices are a lot easier to consume on Android than SOAP ;
So let's see the small steps from a simple SOAP webservice to a RESTful one :

The initial SOAP method is called ReturnEmail :
 - requested parameter is a username ;
 - returned value is the appropiate email address retrieved from an SQLS DB without LINQ ;

http://pantestmb.blogspot.com/2011/02/personpassport4asmx.html

public class PersonPassport2 : WebService
{
const string connStr = "server=localhost;uid=sa;pwd=kash;database=minipassport";


[WebMethod(Description = "Method to obtain User Email Address")]
public string ReturnEmail(string username)
{
SqlConnection dbConn = new SqlConnection(connStr);
string sqlStr = "Select email from users where username = '" + username + "';";
dbConn.Open();
SqlCommand dbCommand = new SqlCommand(sqlStr,dbConn);
SqlDataReader dbReader = dbCommand.ExecuteReader();
dbReader.Read();
string _name = dbReader[0].ToString();
dbReader.Close();
dbConn.Close();
return _name;
}


}


The final WCF REST webservice method using WebInvoke - GET :

    [ServiceContract]
    public interface IRestServiceImpl
    {
        [OperationContract]
        [WebInvoke(Method = "GET",
            ResponseFormat = WebMessageFormat.Xml,
            BodyStyle = WebMessageBodyStyle.Wrapped,
            UriTemplate = "xml/{user}")]
        string ReturnEmail(string user);
    }


   public class RestServiceImpl : IRestServiceImpl
    {
     const string connStr = "server=localhost;uid=sa;pwd=kash;database=minipassport";
     public string ReturnEmail(string user)
        {
            SqlConnection dbConn = new SqlConnection(connStr);
            string sqlStr = "Select password from users where username = '" + user + "';";
            dbConn.Open();
            SqlCommand dbCommand = new SqlCommand(sqlStr, dbConn);
            SqlDataReader dbReader = dbCommand.ExecuteReader();
            dbReader.Read();
            string _name = dbReader[0].ToString();
            dbReader.Close();
            dbConn.Close();
            return _name;
        }
}

How to invoke the WCF REST webservice method :
http://localhost/RestServicePost/RestServiceImpl.svc/xml/user1

and the result is :



GET with FIDDLER (click to zoom) :




# 73 - Migrating from .asmx web services to WCF web services



http://www.dimecasts.net/Casts/CastDetails/73



12/26/2008
Level: Intermediate
Tags: WCF
Comments: (12)
Author:
Donn Felker

In this episode we will walk you though the process of converting your company from using .asmx services over to wcf services.


The focus of this episode is how you can setup your environment to support both types of services and use the same back end. This will allow you to slowly convert over any consumers from .asmx to wcf.
Click here to Watch this Episode


Download (12.76 MB) (8:02) (1440x900)
Download (9.15 MB) (8:02) (960x600)

No comments:

Post a Comment