Hai All,
I am new to wcf rest services,
public interface IRestService
{
[WebGet(UriTemplate="/Student",
ResponseFormat=WebMessageFormat.Json
)]
[OperationContract]
List<product> GetProducts();
[WebInvoke(Method="GET",UriTemplate = "/GetStudent/{Id}", RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
[OperationContract]
List<product> GetData(string Id);
[WebInvoke(Method = "POST", UriTemplate = "/Insert", RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json
)]
[OperationContract]
bool insert(Product Product);
[OperationContract]
[WebGet(UriTemplate = "/Add/{Number1}/{Number2}", RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json
)]
int Add(string Number1, string Number2);
}
public class RestService : IRestService
{
public List<product> GetProducts()
{
PractiseEntities db = new PractiseEntities();
var students = from p in db.StudentEntities select p;
List<product> st1 = students.ToList().ConvertAll(new Converter<studententity,>(TranslateStudentEntityToStudent1));
return st1;
}
public List<product> GetData(string Id)
{
int id = Convert.ToInt32(Id);
PractiseEntities db = new PractiseEntities();
var stdnts = from p in db.StudentEntities
where p.ID == id
select p;
List<product> st = stdnts.ToList().ConvertAll(new Converter<studententity,>(TranslateStudentEntityToStudent1));
return st;
}
private Product TranslateStudentEntityToStudent1(StudentEntity student)
{
Product p = new Product();
p.ProductID = student.ID;
p.ProductName = student.Name;
p.QuantityPerUnit = student.Adress;
p.UnitPrice = student.DOB;
return p;
}
public bool insert(Product product)
{
PractiseEntities db = new PractiseEntities();
StudentEntity st = productToStudent(product);
db.StudentEntities.AddObject(st);
db.SaveChanges();
return true;
}
private StudentEntity productToStudent(Product p)
{
StudentEntity s = new StudentEntity();
s.ID = p.ProductID;
s.Name = p.ProductName;
s.Adress = p.QuantityPerUnit;
s.DOB = p.UnitPrice;
return s;
}
public int Add(string Number1, string Number2)
{
int num1=Convert.ToInt32(Number1);
int num2 = Convert.ToInt32(Number2);
return num1 + num2;
}
}
is this implementation/way is good one for wcf rest services,(or) is there another way
when I try to execute http://localhost:51425/RestService.svc/Insert I got error as method not allowed,how can I resolve this error and,how can I insert data using this,
how client can know about each methods endpoint,is there any other way to client can know about method's endpoint's
can anybody help me in this.