Introduction to RESTFulService
RESTful services are those which follow Representational State Transfer architecture style. WCF allows us to make calls and exchange messages using SOAP over a variety of protocols i.e. HTTP, TCP, Named Pipes and MSMQ etc. In a scenario, if we are using SOAP over HTTP, we are just utilizing HTTP as a transport. But HTTP is much more than just a transport. So, when we talk about REST architectural style, it dictates that “Instead of using complex mechanisms like CORBA, RPC or SOAP for communication, simply HTTP should be used for making calls”.
RESTful architecture uses HTTP for all CRUD operations like (Read/Create/Update/Delete) using simple HTTP verbs like (GET, POST, PUT, and DELETE).It’s simple as well as lightweight. For the sake of simplicity, I am going to implement only a GET request for which service will return certain types of data (i.e. Product data) in XML format.
Following are 5 simple steps to create your first RESTful service that returns data in XML.
- Create a WCF Service Project.
- Preparing the data (e.g. Product) to return
- Creating Service Contract
- Implementing Service
- Configure Service and Behavior
Now lets create the service.
- Open Visual Studio.
- File - New Project - new WCF Service Application.
- Now we will create entity which needs to be share. Add Employee.cs
- [DataContract]
- public class Employee {
- [DataMember]
- public int EmpId {
- get;
- set;
- }
- [DataMember]
- public string Name {
- get;
- set;
- }
- [DataMember]
- public string DepartmentName {
- get;
- set;
- }
- [DataMember]
- public int Age {
- get;
- set;
- }
- }
Now add one more class which will return the collection of employees
- public partial class Employees {
- // private static readonly Employee _emp = new Employee();
- // private Employees();
- public static readonly Employees _emps = new Employees();
- public static Employees Instance {
- get {
- return _emps;
- }
- }
- public List < Employee > emps {
- get {
- return empList;
- }
- }
- private List < Employee > empList = new List < Employee > () {
- new Employee() {
- Name = "e1", DepartmentName = "D1", Age = 25
- },
- new Employee() {
- Name = "e2", DepartmentName = "D2", Age = 35
- },
- new Employee() {
- Name = "e3", DepartmentName = "D3", Age = 45
- },
- new Employee() {
- Name = "e4", DepartmentName = "D4", Age = 26
- },
- };
- }

arnab nandiPosted Nov 30, 2018, 9:33 PM
After running your code I am getting this error code
arnab nandiPosted Nov 30, 2018, 9:32 PM
HTTP Error 500.19 - Internal Server ErrorThe requested page cannot be accessed because the related configuration data for the page is invalid. Detailed Error Information: Module IIS Web Core Notification Unknown Handler Not yet determined Error Code 0x80070032 Config Error The configuration section 'services' cannot be read because it is missing a section declaration Config File \\?\C:\Users\arnab\source\repos\MyRESTServices\MyRESTServices\web.config Requested URL http://localhost:1446/GetEmployeeLsit/ Physical Path Logon Method Not yet determined Logon User Not yet determined Request Tracing Directory C:\Users\arnab\Documents\IISExpress\TraceLogFiles\ Config Source: 35: 36: <services> 37: <service name="WcfService1.EmployeeRESTService" behaviorConfiguration="serviceBehaviors">
John DOePosted Jun 27, 2017, 8:56 AM
Getting method not allowed