Vous êtes sur la page 1sur 2

Consuming data from WCF Service

We use REST (Representational State Transfer) architectural style build the WCF service. REST is an architectural style that can be used to build software in which clients (user agents) can make requests of services (endpoints). The constraints of REST are: User agents interact with resources, and resources are anything that can be named and represented. Each resource can be addressed via a unique Uniform Resource Identifier (URI). Interaction with resources (located through their unique URIs) is accomplished using a uniform interface of the HTTP standard verbs (GET, POST, PUT, and DELETE). Also important in the interaction is the declaration of the resource's media type, which is designated using the HTTP Content-Type header. (XHTML, XML, JPG, PNG, and JSON are some well-known media types.) Resources are self-descriptive. All the information necessary to process a request on a resource is contained inside the request itself (which allows services to be stateless). Resources contain links to other resources (hyper-media).

Example of WCF designed using REST Architecture. Say we have two classes Student and Department. We can write a WCF Service as below. public class Service { [OperationContract] [WebGet(UriTemplate = "Service/GetStudentById({id})")] public Student GetStudentById(string id) { Student student; // fetching from database logic goes here return student; } [OperationContract] [WebInvoke(Method ="POST", UriTemplate = "Service/AddStudentToDepartment", BodyStyle = WebMessageBodyStyle.WrappedRequest)] public int AddStudent(Student student) { // inserting in database logic goes here return returnValue; } }

" GetStudentById " accepts a "GET" method. It takes Student Id as input parameter, and returns a "Student" object fetched from the database using the Id provided. "AddStudent" accepts a "POST" method; it takes one input parameter, a "Student" object. It then adds the "Student" to the database, and returns the newly generated primary key back to the caller. We can access the above methods using the below URL var GetStudentById = "http://localhost:4305/Service/GetStudentById"; var AddStudent = "http://localhost:4305/Service/AddStudent"

Vous aimerez peut-être aussi