WCF 4 Step By Step Chapter 15 Note(Building REST Services)

本文探讨了REST服务的两种常见架构:SOAP和REST模型,并通过具体示例介绍了如何使用REST实现数据查询、更新等功能。同时,文章还展示了如何利用WCF Data Services进行数据服务开发。

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值