Building REST Services
There are two common architectures that organizations usefor implementing Web services; services based on the Simple Object Access Protocol (SOAP),and services based on the Representational State Transfer (REST) model. Both architectures rely on the ubiquitous HTTP protocol and the addressing scheme implemented by the Internet, butthey employ it in different ways.
In SOAP model,forces the designer to focus on the business processes implemented by the Web service and expose these processes as operations
In REST model,considers thedata exposed by an organization and implements a scheme that enables client applications to access this data and manipulate it using their own business logic.
Querying Data by Implementing a REST Web Service
[ServiceContract(Namespace ="http://adventure-works.com/2010/07/28",
Name ="ProductsSales")]
public interfaceIProductsSales
{
[OperationContract]
[WebGet(UriTemplate = "Orders?skip={skip}&top={top}")]
[Description("Returns a list of all orders. By default, the list islimited to" +
" thefirst 100 orders; specify the SKIP and TOP parameters to" +
"implement paging.")]
ICollection<SalesOrderHeader> GetAllOrders(int skip, int top);
}
webHttpBinding
<system.serviceModel>
<services>
<servicename="ProductsSalesService.ProductsSales">
<endpointaddress="http://localhost:8000/Sales"binding="webHttpBinding"
bindingConfiguration=""contract="ProductsSalesService.IProductsSales" />
</service>
</services>
</system.serviceModel>
Test the REST Web Service by Using a Web Browser:
Unfortunately, Visual Studio does not currently provide the functionality to generate a proxy class for a REST Web service—but it is not difficult to implement a proxyclass manually by extending the System.ServiceModel.ClientBase class.
Updating Data Through a REST Web Service
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate =
"Customer?FirstName={firstName}&LastName={lastName}&EmailAddress={email}"+
"&Phone={phone}")]
[Description("Adds a new customer")]
intCreateCustomer(string firstName, string lastName, string email, string phone);
[OperationContract]
[WebInvoke(Method = "PUT", UriTemplate =
"Customers/{customerID}?EmailAddress={email}&Phone={phone}")]
[Description("Updates the email address and/or telephone number fora customer")]
voidUpdateCustomer(string customerID, string email, string phone);
[OperationContract]
[WebInvoke(Method = "DELETE", UriTemplate ="Customers/{customerID}")]
[Description("Deletes a customer")]
voidDeleteCustomer(string customerID);
Using WCF Data Services
ADO.NET Entity Framework entity model+WCF Data Services
SalesDataService.svc.cs
public class SalesDataService :DataService<AdventureWorksEntities>
{
// This method iscalled only once to initialize service-wide policies.
public static voidInitializeService(DataServiceConfiguration config)
{
config.DataServiceBehavior.MaxProtocolVersion =DataServiceProtocolVersion.V2;
config.SetEntitySetAccessRule("Contacts",EntitySetRights.AllRead);
config.SetEntitySetAccessRule("SalesOrderHeaders",EntitySetRights.AllRead);
config.SetEntitySetAccessRule("SalesOrderDetails",EntitySetRights.AllRead);
config.SetEntitySetPageSize("*", 25);
}
}
Test
http://localhost:48000/SalesData/SalesDataService.svc/Contacts
http://localhost:48000/SalesData/SalesDataService.svc/SalesOrderDetails
http://localhost:48000/SalesData/SalesDataService.svc/SalesOrderDetails?$skip=50
http://localhost:48000/SalesData/SalesDataService.svc/SalesOrderDetails?$orderby=UnitPrice
http://localhost:48000/SalesData/SalesDataService.svc/SalesOrderDetails?$orderby=UnitPricedesc
http://localhost:48000/SalesData/SalesDataService.svc/SalesOrderHeaders?$select=SalesOrderID,OrderDate,CustomerID,TotalDue
http://localhost:48000/SalesData/SalesDataService.svc/SalesOrderHeaders?$select=SalesOrderID,TotalDue&$filter=CustomerIDeq 99
http://localhost:48000/SalesData/SalesDataService.svc/SalesOrderHeaders(43682)
http://localhost:48000/SalesData/SalesDataService.svc/SalesOrderHeaders(43682)/Contact
http://localhost:48000/SalesData/SalesDataService.svc/SalesOrderHeaders(43682)/SalesOrderDetails
http://localhost:48000/SalesData/SalesDataService.svc/SalesOrderHeaders?$expand=Contact
You can generate a client library in two ways; you can usethe DataSvcUtil utility from the command line, or you can use theAdd Service ReferenceWizard in Visual Studio. To use the DataSvcUtil utility, open a Visual Studio command prompt andtype the following command while the WCF Data Service is running:
DataSvcUtl /out:SalesClient.cs/uri:http://localhost:48000/SalesData/SalesDataService.svc