introduction to wcf data service and odata

Post on 02-Jun-2018

230 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

8/10/2019 Introduction to WCF Data Service and ODATA

http://slidepdf.com/reader/full/introduction-to-wcf-data-service-and-odata 1/21

Introduction to WCF Data service andODATA

Creating WCF Data ServiceConsuming WCF Data Service

WCF Data Service, Project Astoria, OData and ADO.Net Data Service are samething.

What is ODATA?

• ODATA is HTTP or web protocol to quer ing or manipulating data!• ODATA uses HTTP" #OS$ or ATO%!

• ODATA can be implemented t&roug& an plat'orm supports" HTTP" (%) or #SO$!

• ODATA can e*pose data+in'ormation 'rom relational database" File s stems" Websites" services etc!

• ODATA speci,cation available under %icroso't-s Open Speci,cation Promise .OSP/so t&ird parties" including open source pro0ects" can build Open Data Protocolclients and services!

What is WCF Data Service? • WCF Data service 'ramewor1 provides an API t&at allows data to be created and

consumed over HTTP using 23ST'ul service!• WCF Data service supports all database operations using 42I!

• WCF Data service can e*pose an entit model via an 42I!

• WCF Data service is 23ST'ul service to support C24D operations on database!

• WCF Data service could be consumed b an t pe o' client li1e Windows"Silver)ig&t" Web" A#A( and console!

8/10/2019 Introduction to WCF Data Service and ODATA

http://slidepdf.com/reader/full/introduction-to-wcf-data-service-and-odata 2/21

Bloc diagram e!"laining #or ing o$ WCF Data Service

5! Create a Data Access la er using ADO!$et 3ntit model or )I$6!7! %a1e sure Data model is implementing I6uer able and I4pdateable !

8! 3*pose t&e data model as 23ST service using WCF Data service!

C%&D O"erations and 'TTP ver(

8/10/2019 Introduction to WCF Data Service and ODATA

http://slidepdf.com/reader/full/introduction-to-wcf-data-service-and-odata 3/21

S)""orted *essage Format

Sam"le on WCF Data Service

8/10/2019 Introduction to WCF Data Service and ODATA

http://slidepdf.com/reader/full/introduction-to-wcf-data-service-and-odata 4/21

)et us sa " we &ave a table called Student o' below structure below in our database

In Student table 2oll$umber is a primar 1e !

)et us create WCF Data Service w&ic& will e*pose t&e above table as 23ST service

Ste" + Create a We( A""lication

Create a new pro0ect and select ASP!$et Web Application pro0ect template 'rom Web tab! 9ive a meaning 'ull name to t&e web application!

8/10/2019 Introduction to WCF Data Service and ODATA

http://slidepdf.com/reader/full/introduction-to-wcf-data-service-and-odata 5/21

Ste" - Create a Data *odel

We can create a Data %odel" w&ic& can be e*posed as WCF Data Service in t&ree wa s5! 4sing ADO!$et 3ntit model!7! 4sing )I$6 to S6) class!

8! Custom Data %odel!

For our purpose" I am going to use ADO!$et 3ntit model to create t&e data model! So tocreate an entit model

5! 2ig&t clic1 on web application and add a new item

8/10/2019 Introduction to WCF Data Service and ODATA

http://slidepdf.com/reader/full/introduction-to-wcf-data-service-and-odata 6/21

7! Select ADO!$et 3ntit model 'rom Data tab!

8! Since we &ave table in data base! so we are going to c&oose option " select 'romdatabase!

8/10/2019 Introduction to WCF Data Service and ODATA

http://slidepdf.com/reader/full/introduction-to-wcf-data-service-and-odata 7/21

:! 3it&er c&oose t&e data base 'rom drop down or create a new data connection!

8/10/2019 Introduction to WCF Data Service and ODATA

http://slidepdf.com/reader/full/introduction-to-wcf-data-service-and-odata 8/21

In above connection string StudentD;3ntities is name o' t&e connection string! I'we want" we can c&ange t&is connection string as per our requirement! I' ourrequired data base is not listed in drop down t&en" ou can create a new dataconnection! To create new data connection clic1 on $ew Connection!

8/10/2019 Introduction to WCF Data Service and ODATA

http://slidepdf.com/reader/full/introduction-to-wcf-data-service-and-odata 9/21

<ou can give t&e data base server name and press re'res&! A'ter pressing 2e'res&"ou can c&oose t&e data base 'rom t&e drop down! A'ter selecting t&e data baseclic1 on Test Connection to test connection establis&ed success'ull or not=

>! Select tables" views and stored procedure 'rom data base ou want to ma1e ast&e part o' our data model! Since we are &aving onl one table so we areselecting one table!

8/10/2019 Introduction to WCF Data Service and ODATA

http://slidepdf.com/reader/full/introduction-to-wcf-data-service-and-odata 10/21

I' ou want ou can c&ange name o' t&e data model! ; de'ault it is name o' t&edata base appended b t&e term model! Clic1 on Finis& button to complete and

create t&e data model!?! $ow we can see t&at StudentData%odel!edm* &as been created in t&e designer!

Since t&ere is onl one table" so t&ere is onl one table model at design sur'ace!

8/10/2019 Introduction to WCF Data Service and ODATA

http://slidepdf.com/reader/full/introduction-to-wcf-data-service-and-odata 11/21

$ow we &ave created t&e data model w&ic& can be e*posed as WCF Data Service! $ow ou can see in solution e*plorer" ou &ave Student%odel!edm* andStdent%odel!Designer!cs ,les!

Ste" Creating WCF Data Service 5! 2ig&t clic1 on Web Application pro0ect and add a new item!7! Select WCF Data Service 'rom Web tab! 9ive an meaning'ul name! I am leaving

t&e de'ault name &ere!

8! A'ter adding t&e WCF Data Service" we can see a service ,le wit& e*tension !svc&as been added to t&e solution e*plorer! W&en we clic1 on !svc!cs ,le" we can see

t&e code generated 'or us!

8/10/2019 Introduction to WCF Data Service and ODATA

http://slidepdf.com/reader/full/introduction-to-wcf-data-service-and-odata 12/21

@er ,rst we need to put data source name! To do so uncomment t&e ,rstcommented line and put t&e data source name! In our case name o' t&e model"w&ic& we created in 7nd step is t&e data source! Our data source name isStudentD;3ntities

$ow we need to set access rules 'or entit or entit set! Since we &ave onl onetable" so eit&er we can give name o' t&e table e*plicitl or i' we want to set t&esame access rule 'or all t&e table in t&e data model or data source we could put !

So we are setting t&e access rule t&at" on t&e entit in data source per'orm all t&eoperations!

Ste" / %)n the WCF Data Service

#ust press F> to run t&e WCF Data Service! Data Service will be &osted in t&e de'aultASP!$et server called Cassini!

On running ou can see" one table is listed! T&at table is Student

T&e above is t&e data in ATO% message 'ormat!

Note B I' our browser is not s&owing t&e e*pected result" ma1e sure Feed reading o'browser is o ! To do 'rom menu o' I3 and select tool and t&en Internet Option t&enContent

8/10/2019 Introduction to WCF Data Service and ODATA

http://slidepdf.com/reader/full/introduction-to-wcf-data-service-and-odata 13/21

8/10/2019 Introduction to WCF Data Service and ODATA

http://slidepdf.com/reader/full/introduction-to-wcf-data-service-and-odata 14/21

*etaData

• One 'eature o' WCF data service is t&at" t&e provide operation to retrieve%etadata about t&e service and o er o' t&e service!

• T&is is ver use'ul in determining structure be'ore requesting t&em!

• WCF Data service provides metadata accessible!

• %etadata e*pose all t&e resource and custom services!

• %etadata could be accessed b appending metadata to service 42I!

So" t&e 42I to access metadata is

&ttpB++local&ostB7E: E+Wc'DataService5!svc+ metadata

&%0 1)er2 o"tions

8/10/2019 Introduction to WCF Data Service and ODATA

http://slidepdf.com/reader/full/introduction-to-wcf-data-service-and-odata 15/21

We can per'orm various queries in t&e browser itsel' using t&e 42I quer options!

O"tions

O"tions Descri"tions

e*pand 2equest set o' related entities to be retrieved!

orderb Indicates t&e sequence entit to be retrieved

s1ip S1ip number o' data items!

top At most returns top number o' data items

,lter Applies t&e ,ltering condition

O"erators

O"tions Descri"tions

eq 3qual

ne $ot 3qualgt 9reater T&an

ge 9reate t&an or equal to

lt )ess t&an

le )ess t&an or equal to

and )ogical A$D

or )ogical O2

not )ogical $OT

*ath O"erators

O"erators Descri"tions

add add

sub Subtract

mul multipl

div Division

mod remainder

3ario)s 4)eries

5! Fetc& all t&e records 'rom t&e table Student

42IB &ttpB++local&ostB7E: E+Wc'DataService5!svc+Students

2esult

8/10/2019 Introduction to WCF Data Service and ODATA

http://slidepdf.com/reader/full/introduction-to-wcf-data-service-and-odata 16/21

7! Fetc& t&e student details wit& roll number 5

42IB &ttpB++local&ostB7E: E+Wc'DataService5!svc+Students= ,lterG2oll$umbereq-5-

8! Fetc& top 7 record 'rom t&e student table

42IB &ttpB++local&ostB7E: E+Wc'DataService5!svc+Students= top G7

8/10/2019 Introduction to WCF Data Service and ODATA

http://slidepdf.com/reader/full/introduction-to-wcf-data-service-and-odata 17/21

:! Fetc& t&e records o' t&e student wit& roll number 5 or 8

42IB &ttpB++local&ostB7E: E+Wc'DataService5!svc+Students=. ,lterG2oll$umbereq-5-/or. ,lterGrollnumber eq -8-/

Different Access rules on the entity set

5n)merator Descri"tions

All All 2ead and Write are permitted

All2ead All 2ead permitted

AllWrite All Write is permitted

$one $o access is permitted

2ead%ultiple 2eading multiple row is permitted

2eadSingle 2eading a single row is permitted

WriteAppend Creating $ew data is permitted

8/10/2019 Introduction to WCF Data Service and ODATA

http://slidepdf.com/reader/full/introduction-to-wcf-data-service-and-odata 18/21

WriteDelete Deleting data is permitted

Write%erge %erge updating is permitted

Write2eplace 2eplace updating is permitted

As we discussed above we can set access rule 'or a particular entit or w&ole entit set in

data source!

config.SetEntitySetAccessRule( "*" , EntitySetRights .All);

T&is will permit all rig&t access on all entit set!

config.SetEntitySetAccessRule( "Students" , EntitySetRights .AllWrite);

T&is will permit all write access on Student entit set!

Creating a client to cons)me WCF Data Service

Ste" +

Add a new pro0ect in t&e same solution o' t pe console application!

Ste" -

Add t&e service re'erence o' WCF Data Service! To add rig&t clic1 on t&e console pro0ectand add a service re'erence! Since WCF Data Service and client pro0ect in t&e samesolution " clic1 on Discover to discover t&e service in t&e solution!

8/10/2019 Introduction to WCF Data Service and ODATA

http://slidepdf.com/reader/full/introduction-to-wcf-data-service-and-odata 19/21

Add t&e re'erence o' S stem!Data!Service!Client in client pro0ect also!

Ste"

8/10/2019 Introduction to WCF Data Service and ODATA

http://slidepdf.com/reader/full/introduction-to-wcf-data-service-and-odata 20/21

Add namespaces

Ste" /

$eed to create instance o' DataConte*t and StudentD;3ntities! T&ese two classes ta1eas parameter in t&eir constructor! Parameter is base 42I o' t&e WCF Data Service!

%etrieving all the records o$ ta(le St)dents

Adding one record in the ta(le St)dent

&"dating a record in the ta(le St)dent

8/10/2019 Introduction to WCF Data Service and ODATA

http://slidepdf.com/reader/full/introduction-to-wcf-data-service-and-odata 21/21

Delete a record in the ta(le St)dent

top related