zoukankan      html  css  js  c++  java
  • [转]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
  • 相关阅读:
    453. Minimum Moves to Equal Array Elements
    CDH的安装
    Java注解
    BeanUtils--内省加强
    Java内省
    ant工具
    log4j的配置及使用
    武汉科技大学ACM:1010: 零起点学算法89——母牛的故事
    武汉科技大学ACM:1009: 华科版C语言程序设计教程(第二版)例题5.4
    武汉科技大学ACM:1007: 不高兴的津津
  • 原文地址:https://www.cnblogs.com/freeliver54/p/6912548.html
Copyright © 2011-2022 走看看