WCF Data Services 基础
把最近使用的WCF Data Service和WCF RIA Service的使用例子發(fā)布在站點(diǎn)http://dskit.codeplex.com , 本系列文章就把WCF Data Service和WCF RIA Service涉及的主要方面描述一下,希望通過(guò)這些內(nèi)容,可以比較順暢的把這些框架使用起來(lái),以提高開發(fā)的效率。
?
WCF Data Services中幾個(gè)基礎(chǔ)的概念
Tenets of REST
- HTTP method
?Method information is kept in the HTTP header, not in the body of the request.
- Resource based
?Resources are expressed as “services” that clients consume.
- Addressability
The scoping information is kept in the URL. ?Operations such as $filter $top $skip
- Relationships
How the data relates to each other is expressed in ADO.NET Data Services by resource links according to the CSDL [ specified in the Entity Data Model (EDM)]. A relationship link element such as <link href=”Customers(‘ALFKI’)/Orders
- Statelessness
No stateful messages are exchanged between resources
MIME類型
承載的服務(wù)可以以JSON和ATOM返回【請(qǐng)求時(shí)說(shuō)明MIME類型】用的返回類型如下:
| MIME | MIME Type | 應(yīng)用程序類型 |
| JSON | application/json | ASP.NET, AJAX, Silverlight ? |
| Atom | application/atom+xml | .NET Clients, Silverlight, ASP.NET mashups |
HTTP Operation Success Return Codes
根據(jù)REST的定義對(duì)于不同的操作定義的服務(wù)端返回值情況:
Operation Success Code
GET 200 OK
POST 201 Created
PUT 204? No- Content
DELETE 204? No- Content
MERGE 204? No- Content
簡(jiǎn)單例子
VS2010中新建一個(gè)WEB工程,按如下步驟建立一個(gè)例子項(xiàng)目
?
建立微軟SQL例子數(shù)據(jù)庫(kù)NorthWind
1、? 添加 Ado.net Entity: 選擇SQL最初的例子NorthWind數(shù)據(jù)庫(kù)
2、? 添加WCF Data Services:WcfDataService.svc
3、? 在WcfDataService.svc.cs配置訪問(wèn)規(guī)則:
????????????config.SetEntitySetAccessRule("*",?EntitySetRights.AllRead);
????????????config.SetServiceOperationAccessRule("*",?ServiceOperationRights.All);
????????????config.DataServiceBehavior.MaxProtocolVersion?=?DataServiceProtocolVersion.V2;
以上的部分是配置服務(wù)的可訪問(wèn)性和權(quán)限、行為等各種參數(shù),具體參考MSDN,如果不對(duì)互聯(lián)網(wǎng)公布的,默認(rèn)配置也可
?
4、服務(wù)訪問(wèn)測(cè)試:Build解決方案后即可訪問(wèn)
下表是幾個(gè)訪問(wèn)的實(shí)例
| 發(fā)布的服務(wù)描述 http://localhost:1206/WcfDataService.svc/ |
| 多個(gè)實(shí)體實(shí)例 http://localhost:1206/WcfDataService.svc/Suppliers |
| 單個(gè)實(shí)體實(shí)例 http://localhost:1206/WcfDataService.svc/Suppliers(1) |
| 對(duì)象關(guān)系導(dǎo)航 http://localhost:1206/WcfDataService.svc/Suppliers(1)/Products |
【工程可從http://dskit.codeplex.com下載】
?
可以看到發(fā)布一個(gè)數(shù)據(jù)并進(jìn)行查詢等處理寫的代碼簡(jiǎn)直是很少了,而提供的功能卻非常強(qiáng)大,后續(xù)對(duì)查詢等繼續(xù)進(jìn)行說(shuō)明。
總結(jié)
以上是生活随笔為你收集整理的WCF Data Services 基础的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 续——《RedHat/Fedora等非u
- 下一篇: GIS人眼中的“云GIS”