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

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

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

    为 Oracle Database 开发 WCF Data Services 和 OData 应用程序 本教程包含以下部分:   目的   所需时间   概述   先决条件   创建新的网站项目   ...

  2. WCF Data Services 5.0 for OData V3

    https://www.microsoft.com/en-us/download/details.aspx?id=29306 VS 2010 下 安装 WCF Data Services 5.0 en ...

  3. [转]Consuming a OData Service in a Client Application (WCF Data Services)

    本文转自:https://msdn.microsoft.com/zh-tw/library/dd728282(v=vs.103).aspx WCF Data Services 5.0   其他版本   ...

  4. [转]访问 OData 服务 (WCF Data Services)

    本文转自:http://msdn.microsoft.com/zh-SG/library/dd728283(v=vs.103) WCF 数据服务 支持开放式数据协议 (OData) 将数据作为包含可通 ...

  5. 精进不休 .NET 4.5 (12) - ADO.NET Entity Framework 6.0 新特性, WCF Data Services 5.6 新特性

    [索引页][源码下载] 精进不休 .NET 4.5 (12) - ADO.NET Entity Framework 6.0 新特性, WCF Data Services 5.6 新特性 作者:weba ...

  6. 【2016-10-26】【坚持学习】【Day13】【WCF】【EF + Data Services】

    今天做了一个demo, EF+Data Services 先建立一个网站项目 添加一个ADO.NET 数据模型 相当于一个EF容器,用来连接MSSQL数据库 添加一个WCF Data Services ...

  7. WCF Data Service 使用小结 (一)—— 了解OData协议

    最近做了一个小项目,其中用到了 WCF Data Service,之前是叫 ADO.NET Data Service 的.关于WCF Data Service,博客园里的介绍并不多,但它确实是个很好的 ...

  8. WCF Data Service 使用小结 —— 了解OData(一)

    最近做了一个小项目,其中用到了 WCF Data Service,之前是叫 ADO.NET Data Service 的.关于WCF Data Service,博客园里的介绍并不多,但它确实是个很好的 ...

  9. WCF Data Service 使用小结(二) —— 使用WCF Data Service 创建OData服务

    在 上一章 中,介绍了如何通过 OData 协议来访问 OData 服务提供的资源.下面来介绍如何创建一个 OData 服务.在这篇文章中,主要说明在.NET的环境下,如何使用 WCF Data Se ...

随机推荐

  1. C# 通过二进制,将多个文件合并为一个。

    C# 通过二进制,将多个文件合并为一个. /// <summary> /// 合并文件 /// </summary> /// <param name="strD ...

  2. application/json 和 application/x-www-form-urlencoded的区别

    public static string HttpPost(string url, string body) { //ServicePointManager.ServerCertificateVali ...

  3. 常规Java工具,算法,加密,数据库,面试题,源代码分析,解决方案

    原文链接:http://www.tvtvso.top/?thread-5.htm 有家杂志曾对全国60岁以上的老人进行了这样一次问卷调查:你最后悔什么? 列出10项人们生活中容易后悔的事情,供被调查者 ...

  4. django-给外键关系传值,删除外键关系

    反查: 在表关系里 related_name = '反查name',自己不设置,django也会默认设置为class的小写名字+_set  , ex: book_set. 一对一关系赋值: class ...

  5. 部分流媒体协议及流媒体开发框架vitamio

    流媒体协议部分RTP.RTCP.RTSP.MMS.HLS.HTTP progressive streaming   流媒体协议:(RTP.RTCP.RTSP.MMS.HLS.HTTP progress ...

  6. 使用phpexcel导出到xls文件的时候出现乱码解决

    <?php include 'global.php'; $ids = $_GET['ids']; $sql = "select * from crm_cost_end where id ...

  7. Maven 快速构建一个项目

    参考:http://www.spring4all.com/article/266 mvn archetype:generate -DgroupId=springboot -DartifactId=sp ...

  8. Media Queries 媒体查询

    1.什么是媒体查询 媒体查询可以让我们根据设备显示器的特性(如视口宽度.屏幕比例.设备方向:横向或纵向)为其设定CSS样式,媒体查询由媒体类型和一个或多个检测媒体特性的条件表达式组成.媒体查询中可用于 ...

  9. 最简单的WebService

    写在前面的话: 当两个人碰面后,产生了好感,如果需要得到双方的信息,那么双方的交流是必不可少的!应用程序也如此, 各个应用程序之间的交流就需要WebService来作为相互交流的桥梁! 项目目的: 程 ...

  10. Graphics.Blit

    [Graphics.Blit] 需求注意第4个参数,用4个参数pass用于指定使用哪一个pass.默认值为-1,即使用所有的pass. 参考:file:///C:/Program%20Files%20 ...