[转]WCF Data Services OData
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[转]WCF Data Services OData相关的知识,希望对你有一定的参考价值。
http://martinwilley.com/net/data/wcfds.html
WCF Data Services
About
Formerly "Astoria" and "ADO.Net Data Services", this is the .Net ODATA implementation - ODATA being RESTful HTTP/JSON/ATOM xml access to databases.
OData
HTTP verbs: GET is get, POST is insert, PUT is update, MERGE is partial update, DELETE is delete
NB: PUT,MERGE and DELETE may be blocked by proxies/firewalls, so add the X-HTTP-Method header can be added to a POST request
- Use http://localhost/wcfds.svc/$metadata to get metadata (like ?wsdl)
- http://localhost/wcfds.svc/customers(‘ALFKI‘)
get by primary key - http://localhost/wcfds.svc/customers/$count
- http://localhost/wcfds.svc/customers?$orderby=Price desc
- http://localhost/wcfds.svc/customers?$skip=20&$top=5
- http://localhost/wcfds.svc/customers?$filter= Name eq ‘Bill‘ and Price gt 10
- http://localhost/wcfds.svc/customers?$filter= startswith(name, ‘B‘) and year(Date) gt 2011 and round(Price) = 5
- http://localhost/wcfds.svc/customers?$expand=Orders/Order_Details
Eager loading
Server code
Reference System.Data.Services.Client.dll. The VS2010 item template for the endpoint is "WCF Data Service". The svc code behind is a DataService<T> with a static InitializeService() method which should expose a IQueryable<T> getter.
DataService<T> can be an Entity Framework ObjectContext, which exposes the ObjectSets (tables).
For custom classes that are exposed, add a [DataServiceKeyAttribute(pk)] to the class to denote the primary key (for http://localhost/wcfds.svc/custom(‘ALFKI‘) gets). For POCOs, add [DataContract(IsReference=true)] so properties are serialized as objects, not values (i.e. product.Category refers to a Category object).
Security
You can secure with the standard IIS/web.config settings including Windows, asp.net forms etc.
In the client library, you can set context.Credentials = new NetworkCredential(username, password) or use the context.SendingRequest event to set request headers such as Authorization.
In server InitializeService, configure SetEntitySetAccessRules
Service Operations
In InitializeService, config.SetServiceOperationAccessRule("*", ServiceOperationRights.All); as applicable/
Expose sprocs as service operations: [WebGet]/[WebInvoke] methods- input parameters must be primitives, return must be primitive or IEnumerables/IQueryable.
Client
Adding a service reference to an ODATA service creates a proxy derived from DataServiceContext which looks like a normal EF ObjectContext. (MSDN concepts)
- context.SaveChanges(SaveChangesOption.Batch) will batch changes (also: ContinueOnError, ReplaceOnUpdate)
Catch DataServiceQueryException, DataServiceRequestException - It uses ATOM - to use JSON, use a WebClient with client.Headers["Accept"] = "application/json"
- In a partial class, add [QueryInterceptor("Customers")]public Expression... OnQueryCustomers()
- [ChangeInterceptor("Customers")] can also be used for validation (or subscribe to regular EntityChanging event)
For data-binding, wrap the objectsets in DataServiceCollection which extends ObservableCollection.
Queries
- The entity collections (ObjectSets in EF) are DataServiceQuery<T> but otherwise it‘s mostly normal IQueryable
- Eager load (ODATA $expand) with context.Orders.Expand("Order_Details")
- Explicit load (lazy load) with context.LoadProperty(order, "Order_Details")
- Explicitly executing the query returns a QueryOperationResponse.
When paging, this has a GetContinuation property which includes the url of the next page
NB: you must iterate the query result to use the continuation
以上是关于[转]WCF Data Services OData的主要内容,如果未能解决你的问题,请参考以下文章
Microsoft.Data.Services.Client.dll 与 System.Data.Services.Client.dll
用Spring.Services整合 thrift0.9.2生成的wcf中间代码-复杂的架构带来简单的代码和高可维护性
[Programming WCF Services]Chapter 1. WCF Essentials - Metadata Exchange
How To Easily Call WCF Services Properly z
Windows authentication for WCF web services error
使用 Amazon Web Services (EC2) 和 c# Windows Service/WCF 进行远程调试