WCF Data Service Quickstart

Source: Internet
Author: User
Tags management studio sql server management sql server management studio

Odata is a Web protocol for querying and updating data. Odata is created based on international standards such as HTTP and atompub. It provides a cross-platform Data Communication solution. Odata uses Web technologies such as HTTP, Atom publish protocol (atompub), and JSON to provide different applications.Program, Service and storage information access. SharePoint 2010, SQL Server 2008 R2, workbook, Windows azure table storage, and third-party products such as IBM's WebSphere extreme scale all use odata.

First, WCF Data Services is a WCF Service, so you can use all existing WCF knowledge. Secondly, WCF Data Services has implemented the odata topology, so you can focus on the representation of your data format in your program, rather than the real data format transmitted on the network by atompub/JSON. In addition, WCF Data Services focuses on data transmission rather than data storage. Your data can be stored anywhere: local databases, cloud databases, external web services, XML files, and so on. No matter how the data comes from, you can publish/use them in the same way.

Using the WCF data service in Visual Studio 2010 is the restful Service released by odata. The following example shows the WCF data service in 2010.

1. Create a database, use SQL Server Management studio, and run the script.

A database is created, including two tables categories and products, and two records are inserted in the two table headers.

2. Create a web application and an Entity Data Model, as shown in

3. Create a data service to expose our model:

Using system;
Using system. Collections. Generic;
Using system. Data. Services;
Using system. Data. Services. Common;
Using system. LINQ;
Using system. servicemodel. Web;
Using system. Web;

Namespace mywebsite
{
Public class productservice: dataservice <gettingstartedwithupdateentities>
{
// This method is called only once to initialize service-wide policies.
Public static void initializeservice (dataserviceconfiguration config)
{
// Todo: set rules to indicate which entity sets and service operations are visible, updatable, etc.
// Examples:
Config. setentitysetaccessrule ("*", entitysetrights. allread );
Config. setserviceoperationaccessrule ("*", serviceoperationrights. All );
Config. dataservicebehavior. maxprotocolversion = dataserviceprotocolversion. V2;
}
}
}

4. Verify the productservice service and view productservice. SVC in the browser, as shown in:

The content of is the atompub protocol. First, you initiate an authenticated GET request to the atom link of the server to obtain the description of the current available server. The server returns an XML file (description) of the atom service, which lists some workspace columns. Each workspace contains the collection set. A Workspace may be a blog, wiki namespace, content set, and other resources that can be accessed through your user name/password.

Each workspace can contain five types of sets: entries, categories, templates, users, and generic resources ).

Since a workspace is a product, a product generally contains a series of items, products, and so on. All these sets process and respond in the same way as the HTTP predicate (get, postd, elete, put. All these (sets) Support paging. Therefore, the server can return collections in an easy-to-process data block mode. You can also query by date (SET). Therefore, you can filter collections by the start and end date filters.

To obtain a collection, you can initiate a GET request to the URL (the address listed in 'href 'of the Service document collection. The service documentation specifies a URI for each collection. If you send a GET request to a URI, you will get an XML file containing the atom collection, which lists the first X members in the collection. If there are more than X members in the collection, the file will also contain Uris pointing to the next batch of members, which you can use to obtain the next batch of members. You can also specify a date range through range in the HTTP header, so that the returned collection can only contain entries between the start and end dates.

 

 

We already have a service productservice. SVC that exposes the odata protocol of V2. Below I create a WPF Application to consume this service.

5. Create a WPF Application and add the service reference of productservice. SVC.

You can use a plug-inOpen Data Protocol visualizerView the odata data returned by the service. For more information about how to obtain and install this tool, see the vs2010 extension. View in digoal referenced by the Service.

 

Is the odata model Brower of productservice. svc:

6. Add a viewmodel, encapsulate dataservicecontext, and act as the intermediary for interaction between the WPF form and data service.

Public class viewmodel
{
Private gettingstartedwithupdateentities _ CTX;
Private category [] _ categories;
Private dataservicecollection <product> _ products;

Public viewmodel ()
{
_ CTX = new gettingstartedwithupdateentities (
New uri ("http: // localhost: 1812/productservice. svc/% 22 ));
Load ();
}

Public dataservicecollection <product> Products
{
Get
{
Return _ products;
}
}

Public category [] categories
{
Get
{
Return _ categories;
}
}

Public void savechanges ()
{
_ CTX. savechanges ();
Load ();
}

Public void load ()
{
_ Categories = _ CTX. categories. toarray ();
_ Products = new dataservicecollection <product> (_ CTX );
_ Products. Load (from P in _ CTX. Products. Expand ("category ")
Select P );
}
}

7. Create a WPF form

<Window X: class = "myclientapp. mainwindow"
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation%22
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml%22
Title = "products catalog" Height = "400" width = "425">
<Grid>
<Stackpanel orientation = "horizontal">
<Grid margin = "140," name = "grid1" width = "">
<ListBox itemssource = "{binding Path = products }"
Name = "Products"
Issynchronizedwithcurrentitem = "true">
<ListBox. itemtemplate>
<Datatemplate>
<Stackpanel orientation = "horizontal">
<Textblock text = "{binding Path = Name}" fontweight = "bold"/>
</Stackpanel>
</Datatemplate>
</ListBox. itemtemplate>
</ListBox>
</GRID>
<Stackpanel orientation = "vertical" width = "260">
<Stackpanel orientation = "horizontal">
<Label name = "lblname" width = "100">
<Textblock width = "150"> name: </textblock>
</Label>
<Textbox name = "txtname"
TEXT = "{binding elementname = products, Path = selecteditem. Name, mode = twoway }"
Width = "150"/>
</Stackpanel>
<Stackpanel orientation = "horizontal">
<Label name = "lblcost" width = "100">
<Textblock width = "150"> Cost: </textblock>
</Label>
<Textbox name = "txtcost"
TEXT = "{binding elementname = products, Path = selecteditem. Cost, mode = twoway }"
Width = "150"/>
</Stackpanel>
<Stackpanel orientation = "horizontal">
<Label name = "lblcategory" width = "100">
<Textblock> category: </textblock>
</Label>
<ComboBox name = "cmbcategory"
Itemssource = "{binding Path = categories }"
Displaymemberpath = "name"
Selectedvaluepath = "."
Selectedvalue = "{binding elementname = products, Path = selecteditem. Category, mode = twoway }"
Width = "140"/>
</Stackpanel>
<Button Height = "23"
Horizontalalignment = "right"
Name = "btnsavechanges"
Verticalalignment = "bottom"
Width = "136"
Click = "btnsavechanges_click"> Save changes </button>
</Stackpanel>
</Stackpanel>
</GRID>
</WINDOW>

Compile the following codebehind:Code:

Namespace myclientapp
{
/// <Summary>
/// Interaction logic for mainwindow. XAML
/// </Summary>
Public partial class mainwindow: Window
{
Viewmodel = new viewmodel ();

Public mainwindow ()
{
Initializecomponent ();
This. cmbcategory. datacontext = viewmodel;
This. grid1.datacontext = viewmodel;
}

Private void btnsavechanges_click (Object sender, routedeventargs E)
{
Viewmodel. savechanges ();
This. grid1.datacontext = viewmodel;
}

}
}

 

 

The running result is as follows:

 

Download this article: http://files.cnblogs.com/shanyou/MyWebsite.zip

 

Reference resources:

    • breaking down 'data silo'-the Open Data Protocol (odata)
    • getting started with the Data Services Update for. Net 3.5 SP1-Part 1
    • getting started with the data service update for. Net 3.5 SP1-Part 2
    • introducing the Microsoft Open Data Protocol visualizer (ctp1)
    • http://weblogs.asp.net/cazzu/archive/2010/06/08/wcf-data-service-pipeline.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.