WCF instance-custom dataservice data model (1)

Source: Internet
Author: User

ADO. net dataservice exposes the EDM data model by default (e.g. entity Framework) to provide a data service based on the odata protocol, but also supports custom data models, you can select another data source to implement a dataservice. Of course, as a custom data model, the query needs to return the object implementing the iqueryable interface, and the update needs to implement the iupdatable interface.

Create a WCF Service Application

Delete the default service1.svc and iservice. CS, and add a wcfdataservice. SVC project.

View the markup of the SVC file.
Dataservicehostfactory:

<%@ Servicehost Language = "C #"
Factory ="System. Data. Services. dataservicehostfactory, System. Data. Services, version = 4.0.0.0, culture = neutral, publickeytoken = b77a5c561934e089 "service =" wcfcustomerdataservice. wcfdataservice1"
%>

When developing a non-iis host application, use webservicehost to host the service. For more information, see hosting the Data Service (ADO. NET data services)

1. Define the object:The dataservicekeyattribute must be used in the object to mark the key. Otherwise, dataservice reports an error.

[Dataservicekeyattribute ("orderid")] public class order {public int orderid {Get; set;} Public String customer {Get; set;} public ilist <item> items {Get; set ;}} [dataservicekeyattribute ("product")] public class item {Public String product {Get; Set ;}public int quantity {Get; Set ;}}

2. Define the data model:
(1) Some initial data is added to the static constructor of datamodel, where you can expand the data source at will.
(2) Two attributes are defined: orders and items. They return the iqueryable <t> result.

Public class datamodel {# region populate service data static ilist <order> _ orders; static ilist <item> _ items; static datamodel () {_ orders = new list <order> {New Order () {orderid = 1, customer = "Wendy Wu", items = new list <item> ()}, new Order () {orderid = 2, customer = "John Gu", items = new list <item> ()}, New Order () {orderid = 3, customer = "balance Yao", items = new list <item> () }}; _ items = new list <item> {new item () {Product = "Chang ", quantity = 4}, new item () {Product = "aniseed syrup", quantity = 5}, new item () {Product = "Toy", quantity = 7 }, new item () {Product = "car", quantity = 1}, new item () {Product = "ball", quantity = 6 }}; _ orders [0]. items. add (_ items [0]); _ orders [1]. items. add (_ items [1]); _ orders [1]. items. add (_ items [2]); _ orders [2]. items. add (_ items [3]); _ orders [2]. items. add (_ items [4]) ;}# endregion public iqueryable <order> orders {get {return _ orders. asqueryable <order> () ;}} public iqueryable <item> items {get {return _ items. asqueryable <item> ();}}}

3. Implement Service

 
Public class wcfdataservice1: dataservice <datamodel> {public static void initializeservice (dataserviceconfiguration config) {config. setentitysetaccessrule ("*", entitysetrights. all); config. setserviceoperationaccessrule ("*", serviceoperationrights. all); config. dataservicebehavior. maxprotocolversion = dataserviceprotocolversion. v2 ;}}

Run. Enter http: // localhost: 50480/wcfdataservice1.svc/orders in the browser to view the result: XML data of the atom protocol is returned.


PS:Data Service requestsAccept
To determine the return format.
For example, if I use Fiddler to query and use accept: Application/JSON, the returned data is in JSON format:

Of course, in addition to implementing attributes in datamodel to provide data query services, you can also implement methods in service to provide services.
For example, in the preceding wcfdataservice1, add the following method: the query is written according to the odata protocol during the call.

[Webget] [singleresult] public order toporder () {return this. currentdatasource. orders. orderbydescending (O => O. items. sum (I => I. quantity )). first ();}

Data Service has some specific restrictions on service operations

  • This method can only accept the [in] parameter. If you define a parameter, the type of each parameter must be of the base element type.
  • This method must return void, ienumerable <t>, iqueryable <t>, T, or primitive class (such as integer or string ). T must be a class, which indicates an entity type in the data model that the data service will publish. To support query options (such as sorting, paging, and filtering), the service operation method should return iqueryable <t>.
  • The [webget] or [webinvoke] attribute must be used to add annotations for this method. [Webget] enables the caller to call this method by using GET requests. [webinvoke] enables the caller to call this method by using put, post, or delete requests.
  • You can use singleresultattribute to add comments for service operations, specifying that the return value of this method is an entity rather than an entity set. This difference determines the generated response serialization. For example, when atompub is used for serialization, a single resource type instance is represented as an entry element, and a single instance set is represented as a feed element.

4. Client call
Create a console project, add service reference to generate client proxy, and call dataservice:

Static void main (string [] ARGs) {var svcuri = new uri ("http: // localhost: 50480/wcfdataservice1.svc"); // query var CTX = new datasvc. datamodel (svcuri); var order = CTX. orders. where (O => O. orderid = 3 ). first (); console. writeline (Order. customer); // query by customer method 'toporder' var toporder = CTX. execute <datasvc. order> (New uri ("/toporder", urikind. relative )). first (); console. writeline (toporder. customer); console. read ();}

Reference: http://msdn.microsoft.com/zh-cn/library/dd723653.aspx

(Updated on February 30)

Queryinterceptorattribute and changeinterceptorattribute can be used to intercept requests.
Reference: http://msdn.microsoft.com/zh-cn/library/dd744837.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.