[转]WCF Data Services OData

http://martinwilley.com/net/data/wcfds.html

WCF Data Services

For .net 4.5, this is replaced by Microsoft.AspNet.Odata which uses ODataController (based on WebApi ApiController).

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
时间: 2024-11-08 05:58:42

[转]WCF Data Services OData的相关文章

为 Oracle Database 开发 WCF Data Services 和 OData 应用程序

为 Oracle Database 开发 WCF Data Services 和 OData 应用程序 本教程包含以下部分:   目的   所需时间   概述   先决条件   创建新的网站项目   创建新的 ADO.NET 实体数据模型   使用 EDM 数据创建 WCF 数据服务   总结 为 Oracle Database 开发 WCF Data Services 和 OData 应用程序 目的 本教程介绍如何使用 Visual Studio 为 Oracle Database 开发 WC

CRUD Operations in MVC4 Using AngularJS and WCF REST Services

Now in this article I will show how to do Create, Retrieve, Update and Delete (CRUD) operations in MVC4 using AngularJS and WCF REST Services. The following are the highlights of this article: Create a Database. (SchoolManagement). Create a Table (St

WCF Data Service ----理论准备

WCF数据服务(WCF Data Service )是.NET Framework的一个组件. 可以使用此组件创建一些服务,利用开放式数据协议(OData)来借助具象状态传输(REST)语义通过Web或Intranet公开和使用数据. OData(Open Data Protocol)开放数据协议 OData将数据公开为可通过URI寻址的资源,可使用GET.PUT.POST和DELETE这些标准HTTP谓词来访问和更改数据. OData使用Entity Data Model的实体关系约定,将资源

调用WCF Data Service的几点Tips

使用Linq实现sql in statement的时候,用EF的时候可以通过Contains.Exists的方法实现.但是在使用WCF Data Service的context的时候,会报不支持该方法的错误.解决方案就是加上AsEnumerable(). var products = from p in ctx.V_Product.AsEnumerable() where catalogs.Exists(c => c.CatalogID == p.CatalogID) select p 主从表级

[转]Open Data Protocol (OData) Basic Tutorial

本文转自:http://www.odata.org/getting-started/basic-tutorial/ Basic Tutorial The Open Data Protocol (OData) is a data access protocol built on core protocols like HTTP and commonly accepted methodologies like REST for the web. There are various kinds of

Silverlight项目笔记1:UI控件与布局、MVVM、数据绑定、await/async、Linq查询、WCF RIA Services、序列化、委托与事件

最近从技术支持转到开发岗,做Silverlight部分的开发,用的Prism+MVVM,框架由同事搭好,目前做的主要是功能实现,用到了一些东西,侧重于如何使用,总结如下:   1.UI控件与布局     2.MVVM     3.数据绑定     4.await/async     5.Linq查询     6.WCF RIA Services     7.序列化     8.委托与事件 1.UI控件与布局 常用的主要是Grid.StackPanel.Border,其中最常用的的是Grid,是一

.svc处理程序映射缺失导致的WCF RIA Services异常

在确定代码.编译结果和数据库都正常的情况下,无法从数据库取到数据.错误提示:Sysyem.Net.WebException:远程服务器返回了错误:NotFound,监听发现请求数据库的服务异常,访问相关地址HTTP404错误. 向数据库发起的请求异常 访问svc地址后的错误信息 在确定其他各方面正常,并且修复了一遍vs,各种IIS设置检查后依然没有找到原因,而且工程在同事电脑上能够正常运行,最后确定只可能是.Net Framework或者IIS有问题,因为之前同事在机器上装测试工具,安装了.Ne

再说WCF Data Contract KnownTypeAttribute

WCF 中的序列化是用DataContractSerializer,所有被[DataContract]和[DataMemeber]标记的类和属性会被DataContractSerializer序列化.在WCF中使用Contract模式来分辨和指定序列化/反序列化的类型,它是通过http://xmlns/Class这样的命名空间来标识这个序列化的对象的,一旦在序列化过程中无法找到这样的标识(比如某个字段,或者子对象)时,序列化就会失败.KnownTypeAttribute就提供了为我们通知序列化器

Windows authentication for WCF web services error

WCF Web服务的Windows身份验证在部署到IIS时,默认网站添加应用程序的方式.浏览运行.svc文件报错. 错误代码: The authentication schemes configured on the host ('IntegratedWindowsAuthentication') do not allow those configured on the binding 'BasicHttpBinding' ('Anonymous'). Please ensure that th