Friday, February 11, 2011

CRUD in RESTful Services of WCF-Tutorial

http://www.codegain.com/articles/wcf/restservice/crud-in-restful-services-of-wcf-tutorial.aspx

using System.Collections.Generic;


[WebGet(UriTemplate = "contacts")]
    [OperationContract]
    List GetAllContacts();

It is possible to return from an soap ASMX a generic collection
List of Contacts instead of a DataSet of Contacts ?



Note : The notion of building UriTemplate is to specify some form of unique Uri.
Always keep in mind, the external world will request the service using the URI.
Now lets go to our ServiceHost application and configure the service to use REST based approach.
Basically for REST based service you need to use webHttpBinding as Binding configuration.

Step by Step tutorial of REST Enabled Service in WCF

, wOutput 100 is returned in the browser by the service.
It is because, below service method. URI of below GET method is mapped to root of the URI.


http://www.dotnetspark.com/kb/1373-step-by-step-tutorial-rest-enabled-service.aspx

<%@ ServiceHost Language="C#" Debug="true"
Service="RestServicePublishing.RestService"
CodeBehind="RestService.svc.cs"
Factory="System.ServiceModel.Activation.WebServiceHostFactory"%>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;


namespace RestServicePublishing
{

    [Serializable]
    public class NumberService
    {
        public int Number1 { get; set; }
        public int Number2 { get; set; }
    }

    [ServiceContract]
    public interface IRestService
    {
        [OperationContract(Name="AddParameter")]
        [WebInvoke(UriTemplate = "/",Method="POST")]
        int  Add(NumberService n1);
        [OperationContract(Name="Add")]
        [WebGet(UriTemplate = "/")]
        int Add();

    
    }


    public class RestService : IRestService
    {
        public int res = 100;

        public int Add(NumberService n1)
        {

            res = Convert.ToInt32(n1.Number1) + Convert.ToInt32(n1.Number2);
            return res;
        }
        public int Add()
        {
            return res;
        }

    }
}





Exposing RESTful Services Using WCF

http://www.drdobbs.com/windows/227600076

Implementing the TestService WCF Service

The WCF Service class called TestService implements the WCF Service Contract implemented earlier -- it defines the two methods declared in the WCF Service Contract. Here's what the class looks like:


<%@ ServiceHost Language="C#" Debug="true" Service="RESTService.TestService" CodeBehind="TestService.svc.cs" %>

using System;

using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;

namespace RESTService
{

[ServiceContract]
public interface ITestService
{
[OperationContract]
[WebGet]
Employee GetEmployee(Int32 employeeID);
[WebInvoke]
Employee PostEmployee(Int32 employeeID);
}

[DataContract(Namespace = "")]
public class Employee
{
[DataMember]
public Int32 EmployeeID { get; set; }
[DataMember]
public string FirstName { get; set; }
[DataMember]
public string LastName { get; set; }
[DataMember]
public String Address { get; set; }
}

public class TestService : ITestService
{
public Employee GetEmployee(Int32 employeeID)
{
return new Employee { EmployeeID = employeeID, FirstName = "Joydip", LastName = "Kanjilal" };
}

public Employee PostEmployee(Int32 employeeID)
{
return new Employee { EmployeeID = employeeID, FirstName = "Joydip", LastName = "Kanjilal" };
}
}
}

WCF Web Services The Easy Way

In fact, we are going to setup WCF web services without any configuration!
To make it even more simple, you could move the code for the service out of the CodeBehind file and into the .svc file. Now you have an entire WCF service in a single file! Check it out:

http://www.codethinked.com/post/2010/08/17/WCF-Web-Services-The-Easy-Way.aspx
http://twitter.com/JustinEtheredge

<%@ ServiceHost Language="C#"
Service="MyTestWebsite.MyTestService"
Factory="System.ServiceModel.Activation.ServiceHostFactory" %>
using System.ServiceModel;

namespace MyTestWebsite
{
[ServiceContract]
public class MyTestService
{
[OperationContract]
public string DoWork()
{
return "Did Something!";
}
}
}

Android Activity and sub-Activity

http://developerlife.com/tutorials/?p=302

Android 101

To get started with Android, click here.

What’s an activity?

An activity is the equivalent of a Frame/Window in GUI toolkits. It takes up the entire drawable area of the screen (minus the status and title bars on top). An activity is what gets bound to the AndroidManifest.xml as the main entry point into an application. For long running tasks, it’s best to use a service that talks to this activity.

Service

Activities are meant to display the UI and get input from the user. However, long running tasks are not meant to be spawned in a activity, since they can be frozen when the focus is switched away from them (by the user or incoming phone call or system event). Services on the other hand keep running for the duration of the user’s ‘session’ on the device.

WCF - Windows Comunication Framwwork

Creating your First WCF Service

http://www.youtube.com/watch?v=JlyND7b2-YA

Developing a REST Web Service
using C# - A walkthrough

http://www.codeproject.com/KB/webservices/RestWebService.aspx

Part # 1
- Create a simple database which contains an Employee table


Part # 2
– Create an Employee class as a placeholder
for the database employee information


Part # 3
- Code basic database operations like
Read, Update, Delete, Insert for Employee


Part # 4
- Create the REST Web Service


Part # 5
- Deploying the application


Part # 6
- Testing the application


References


Issues in deploying applications in IIS -
http://www.codeproject.com/KB/ISAPI/Curious-Case-IIS.aspx

HTTP handlers - http://msdn.microsoft.com/en-us/library/bb398986.aspx
REST - http://www.codeproject.com/KB/architecture/RESTWebServicesPart1.aspx
REST - http://tomayko.com/writings/rest-to-my-wife

Friday, February 4, 2011

PersonPassport2 C# WebService

http://bimbim.in/post/2010/10/08/Android-Calling-Web-Service-with-complex-types.aspx

http://code.google.com/p/jtelmon/source/browse/trunk/Bimbimin.Original.Android.Webservice.Client/

http://code.google.com/p/jtelmon/source/browse/Tips4Java/src/BimBimWS

http://www.flickr.com/photos/24834074@N04/5416340301/

http://www.flickr.com/photos/24834074@N04/5416340257/



<%@ WebService class = "PersonPassport2" Language="C#" Debug = "true"%>

using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.Services;

public class Person

{

private string _name = string.Empty;

private int _age = 0;

private float _salary = 100000.0f;

private System.DateTime _dob = new DateTime(1980, 01, 15);


public Person() {}

public Person(string firstname, string lastname, DateTime dob)
{
this._name = firstname+lastname;
this._dob = dob;
}


public float Salary

{

get { return _salary; }

set { _salary = value; }

}

public System.DateTime Dob

{

get { return _dob; }

set { _dob = value; }

}



public int Age

{

get { return _age; }

set { _age = value; }

}

public string Name

{

get { return _name; }

set { _name = value; }

}



}

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

[WebMethod(Description = "Method to Authenticate Users")]
public bool Authenticate(string username, string password)
{
SqlConnection dbConn = new SqlConnection(connStr);
string sqlStr = "Select password from users where username = '" + username + "';";
dbConn.Open();
SqlCommand dbCommand = new SqlCommand(sqlStr,dbConn);
SqlDataReader dbReader = dbCommand.ExecuteReader();

bool returnBool;
if (dbReader.Read())
{
if (dbReader[0].ToString()==password)
{
returnBool = true;
}
else
{
returnBool = false;
}
}
else
{
returnBool=false;
}
dbReader.Close();
dbConn.Close();
return returnBool;
}

[WebMethod(Description = "Method to Add User")]
public bool AddUser(string username, string password, string name, string email)
{
bool returnBool = false;
SqlConnection dbConn = new SqlConnection(connStr);
string sqlStr = "INSERT INTO users(username,password,name,email) values('" + username + "', '" + password + "', '" + name + "', '" + email + "');";
SqlCommand dbCommand = new SqlCommand(sqlStr,dbConn);
try
{
dbConn.Open();
if (dbCommand.ExecuteNonQuery()!=0)
{
returnBool=true;
}
returnBool=true;
}
catch
{
returnBool=false;
}
dbConn.Close();
return returnBool;
}

[WebMethod(Description = "Method to Delete User")]
public bool DeleteUser(string username)
{
bool returnBool = false;
SqlConnection dbConn = new SqlConnection(connStr);
string sqlStr = "DELETE FROM users where username = '" + username +"';";
SqlCommand dbCommand = new SqlCommand(sqlStr,dbConn);
try
{
dbConn.Open();
if (dbCommand.ExecuteNonQuery()!=0)
{
returnBool=true;
}
}
catch
{
returnBool=false;
}
dbConn.Close();
return returnBool;
}

[WebMethod(Description = "Method to Edit User Information")]
public bool EditUser(string username, string name, string email)
{
bool returnBool = false;
SqlConnection dbConn = new SqlConnection(connStr);
string sqlStr = "UPDATE users SET username = '" + username +"',name = '"+name+"',email= '"+email+"';";
SqlCommand dbCommand = new SqlCommand(sqlStr,dbConn);
try
{
dbConn.Open();
if (dbCommand.ExecuteNonQuery()!=0)
{
returnBool=true;
}
}
catch
{
returnBool=false;
}
dbConn.Close();
return returnBool;
}

[WebMethod(Description = "Method to Change User Password")]
public bool ChangePassword(string username, string password)
{
bool returnBool = false;
SqlConnection dbConn = new SqlConnection(connStr);
string sqlStr = "UPDATE users SET password = '"+password+"';";
SqlCommand dbCommand = new SqlCommand(sqlStr,dbConn);
try
{
dbConn.Open();
if (dbCommand.ExecuteNonQuery()!=0)
{
returnBool=true;
}
}
catch
{
returnBool=false;
}
dbConn.Close();
return returnBool;
}

[WebMethod(Description = "Method to Obtain User Name")]
public string ReturnName(string username)
{
SqlConnection dbConn = new SqlConnection(connStr);
string sqlStr = "Select Name 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;
}

[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;
}

[WebMethod(Description = "Method to obtain All User Info")]
public DataSet ReturnAll()
{
SqlConnection dbConn = new SqlConnection(connStr);
dbConn.Open();
string sqlSelect = "select * from users ";
SqlDataAdapter da = new SqlDataAdapter(sqlSelect, dbConn);
DataSet ds = new DataSet();
SqlCommand dbCommand = new SqlCommand(sqlSelect, dbConn);
da.Fill(ds, "users");
dbConn.Close();
return ds;
}

[WebMethod]
public Person GetSingle()
{
Person person = new Person();
person.Name = "bimbim.in";
person.Age = 30;
person.Dob = new System.DateTime(1980, 01, 15);
person.Salary = 50000f;
return person;
}

[WebMethod]
public string HelloWorld()
{
return "Hello World";
}

}