Click here to Skip to main content
15,893,722 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hai All,
I am new to wcf rest services,
C#
 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.
Posted
Updated 6-Apr-13 3:57am
v2
Comments
Prasad Khandekar 6-Apr-13 16:24pm    
The Web Application Description Language (WADL) is a machine-readable XML description of HTTP-based web applications (typically REST web services). However this recommendation is not yet standardized. But you can make one of the end point of your service to output the XML definition of the rest of the end points. But again you will have to publish some information about this end point on your web site.

Regards,

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900