WCF 4 Step By Step Chapter 15 Note(Building REST Services)
2012-04-15 22:17
417 查看
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
webHttpBinding
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
Using WCF Data Services
ADO.NET Entity Framework entity model+WCF Data Services
SalesDataService.svc.cs
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:
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
相关文章推荐
- WCF 4 Step By Step Chapter 10-12 Note (Reliable Sessions,ProgrammaticallyControlling, One-Way ...)
- WCF 4 Step By Step Chapter 1,2 Note
- WCF 4 Step By Step Chapter 5 Note (Authentication in Internet)
- WCF 4 Step By Step Chapter 7 Note(Maintaining State and Sequencing Operations)
- WCF 4 Step By Step Chapter 6 Note (Maintaining Service Contracts and Data Contracts)
- WCF 4 Step By Step Chapter 3 Note (Fault Handling)
- WCF 4 Step By Step Chapter 4 Note (Transport and Message Security + Authentication in Organization)
- WCF 4 Step By Step Chapter 9 Note (Transaction)
- MDX Step by Step 读书笔记(六) - Building Complex Sets (复杂集合的处理) - TopCount - BottomCount - Head - Tail - Item 函数的使用
- C# step by step 学习笔记8 CHAPTER 9 使用枚举和结构创建值类型
- Step by Step WCF—Instance Management
- [Step By Step]如何在SAP Business Object Data Services中连接到SAP HANA 数据库
- [Step By Step]在SAP Business Objects Data Services中使用Key Generation Transform主键列生成并导入到SAP HANA中(Key Generation Transform)
- WCF自定义行为 Step by Step (四)第二个自定义behavior
- Building websites with Xoops: A step-by-step tutorial
- SharePoint 2010之部署WCF(deploy WCF service to SharePoint 2010 via VS 2010 step by step)
- Building your Recurrent Neural Network - Step by Step参考答案
- WCF REST Configuration for ASP.NET AJAX and plain REST Services
- Building Web Services the REST Way
- 手把手建立Simulink四分之一车辆模型/Building a quarter car model step by step!