Hi Folks! This blog will cover aspects of creation of OData service in WCF (they are known as WCF Data Services) and how to consume them on Windows Phone Device.
What is OData?
OData (Open Data Protocol) is a web protocol to perform querying and updating data. The protocol allows a consumer ( clients built on .Net, Java, PHP, JavaScript, Mobile devices like Windows Phone, iPhone etc.) to query a database over the HTTP protocol and get back the result in different formats like XML,JSON or ATOM, including pagination, ordering or filtering of the data.
WCF Data Services
WCF Data Services (previously known as ADO.Net Data Services) provides developers with client libraries for .Net, Silverlight, AJAX, PHP and Java. Microsoft is supporting OData in SQL Server 2008 R2, Windows Azure Storage, Excel 2010 (through PowerPivot) and SharePoint 2010.
WCF Data Services support both relational data (via Entity Framework 4.0 models and 4.1 Code First Frameworks) and non-relational data-sources which can be exposed as EDM and exposed as a data service.
Exposing data using WCF Data Service
Let us take a simple example -
1. We will create a small table lets say Organization in SQL Server.
2. We will expose this table using Entity Framework Models.
3. We will expose the entities of Entity Framework Models via WCF Data Services.
4. Make changes on Windows Phone Client side to invoke and retrieve data from WCF Data Services.
I have created a table named organization in SQL Server in ./SQLExpress -
create table Organization
(OrganizationID
int PRIMARY KEY identity(1,1),
OrganizationName varchar(255),
CountryName varchar(200)
)
It has three records as of now -
Now, we will create a WCF Data Services Project in VS 2010 -
As seen in the diagram, the data service name is WcfDataService1.svc
Now, we will add an entity framework model (edmx) in this project. Right click on the project --> Click on Add --> New Item. Choose "ADO.Net Entity Model" from Data templates.
You will get an option whether to create a model from scratch or create from database. In this blog post, we will go ahead with creating an entity model from the database directly.
Click Next and then set the connection strings to the appropriate server and database so that the wizard can access the table and create entity model corresponding to that -
Choose the table "Organization" from the list of database objects -
Click "Finish" button to add the entity model to the project.
Now in order to expose this entity model as OData service, you need to make the following changes in WcfDataService1.svc.cs -
1. Inherit the WcfDataService1 with DataService<T> where T represents data sourcepublic class WcfDataService1 : DataService<LoggingEntities>
2. Make changes to the InitializeService method -
public static void InitializeService(DataServiceConfiguration config)
{
config.SetEntitySetAccessRule("Organizations", EntitySetRights.AllRead);
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
}
As seen in the method, the entity "Organizations" is being exposed with rights as Read.
Run the project by pressing F5, the Url - http://localhost:47836/WcfDataService1.svc/ will be -
<?xml version="1.0" encoding="UTF-8" standalone="true"?>
<service xmlns="http://www.w3.org/2007/app" xmlns:app="http://www.w3.org/2007/app" xmlns:atom="http://www.w3.org/2005/Atom" xml:base="http://localhost:47836/WcfDataService1.svc/"> -<workspace> <atom:title>Default</atom:title> -<collection href="Organizations"> <atom:title>Organizations</atom:title> </collection> </workspace> </service>
If you try reading the data with respect to Organizations, type in the following on the browser -
http://localhost:47836/WcfDataService1.svc/Organizations/
The following will be output of this (list of all organizations) -
In the next part of this blog, i will integrate this OData service with Windows Phone applications.
What is OData?
OData (Open Data Protocol) is a web protocol to perform querying and updating data. The protocol allows a consumer ( clients built on .Net, Java, PHP, JavaScript, Mobile devices like Windows Phone, iPhone etc.) to query a database over the HTTP protocol and get back the result in different formats like XML,JSON or ATOM, including pagination, ordering or filtering of the data.
WCF Data Services
WCF Data Services (previously known as ADO.Net Data Services) provides developers with client libraries for .Net, Silverlight, AJAX, PHP and Java. Microsoft is supporting OData in SQL Server 2008 R2, Windows Azure Storage, Excel 2010 (through PowerPivot) and SharePoint 2010.
WCF Data Services support both relational data (via Entity Framework 4.0 models and 4.1 Code First Frameworks) and non-relational data-sources which can be exposed as EDM and exposed as a data service.
Exposing data using WCF Data Service
Let us take a simple example -
1. We will create a small table lets say Organization in SQL Server.
2. We will expose this table using Entity Framework Models.
3. We will expose the entities of Entity Framework Models via WCF Data Services.
4. Make changes on Windows Phone Client side to invoke and retrieve data from WCF Data Services.
I have created a table named organization in SQL Server in ./SQLExpress -
create table Organization
(OrganizationID
int PRIMARY KEY identity(1,1),
OrganizationName varchar(255),
CountryName varchar(200)
)
It has three records as of now -
Now, we will create a WCF Data Services Project in VS 2010 -
As seen in the diagram, the data service name is WcfDataService1.svc
Now, we will add an entity framework model (edmx) in this project. Right click on the project --> Click on Add --> New Item. Choose "ADO.Net Entity Model" from Data templates.
You will get an option whether to create a model from scratch or create from database. In this blog post, we will go ahead with creating an entity model from the database directly.
Click Next and then set the connection strings to the appropriate server and database so that the wizard can access the table and create entity model corresponding to that -
Choose the table "Organization" from the list of database objects -
Click "Finish" button to add the entity model to the project.
Now in order to expose this entity model as OData service, you need to make the following changes in WcfDataService1.svc.cs -
1. Inherit the WcfDataService1 with DataService<T> where T represents data sourcepublic class WcfDataService1 : DataService<LoggingEntities>
2. Make changes to the InitializeService method -
public static void InitializeService(DataServiceConfiguration config)
{
config.SetEntitySetAccessRule("Organizations", EntitySetRights.AllRead);
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
}
As seen in the method, the entity "Organizations" is being exposed with rights as Read.
Run the project by pressing F5, the Url - http://localhost:47836/WcfDataService1.svc/ will be -
<?xml version="1.0" encoding="UTF-8" standalone="true"?>
<service xmlns="http://www.w3.org/2007/app" xmlns:app="http://www.w3.org/2007/app" xmlns:atom="http://www.w3.org/2005/Atom" xml:base="http://localhost:47836/WcfDataService1.svc/"> -<workspace> <atom:title>Default</atom:title> -<collection href="Organizations"> <atom:title>Organizations</atom:title> </collection> </workspace> </service>
If you try reading the data with respect to Organizations, type in the following on the browser -
http://localhost:47836/WcfDataService1.svc/Organizations/
The following will be output of this (list of all organizations) -
<?xml
version="1.0" encoding="utf-8" standalone="yes" ?>
<feed xml:base="http://localhost:47836/WcfDataService1.svc/" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
<title type="text">Organizations</title>
<id>http://localhost:47836/WcfDataService1.svc/Organizations/</id>
<updated>2011-11-19T20:35:29Z</updated>
<link rel="self"
title="Organizations" href="Organizations"
/>
<entry>
<id>http://localhost:47836/WcfDataService1.svc/Organizations(1)</id>
<title type="text" />
<updated>2011-11-19T20:35:29Z</updated>
<author>
<name />
</author>
<link rel="edit"
title="Organization" href="Organizations(1)"
/>
<category term="LoggingModel.Organization" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<content type="application/xml">
<m:properties>
<d:OrganizationID
m:type="Edm.Int32">1</d:OrganizationID>
<d:OrganizationName>Data Logistics</d:OrganizationName>
<d:CountryName>Germany</d:CountryName>
</m:properties>
</content>
</entry>
<entry>
<id>http://localhost:47836/WcfDataService1.svc/Organizations(2)</id>
<title type="text" />
<updated>2011-11-19T20:35:29Z</updated>
<author>
<name />
</author>
<link rel="edit"
title="Organization" href="Organizations(2)"
/>
<category term="LoggingModel.Organization" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<content type="application/xml">
<m:properties>
<d:OrganizationID
m:type="Edm.Int32">2</d:OrganizationID>
<d:OrganizationName>Karan Corp.</d:OrganizationName>
<d:CountryName>India</d:CountryName>
</m:properties>
</content>
</entry>
<entry>
<id>http://localhost:47836/WcfDataService1.svc/Organizations(3)</id>
<title type="text" />
<updated>2011-11-19T20:35:29Z</updated>
<author>
<name />
</author>
<link rel="edit"
title="Organization" href="Organizations(3)"
/>
<category term="LoggingModel.Organization" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<content type="application/xml">
<m:properties>
<d:OrganizationID
m:type="Edm.Int32">3</d:OrganizationID>
<d:OrganizationName>United Corp.</d:OrganizationName>
<d:CountryName>United States</d:CountryName>
</m:properties>
</content>
</entry>
</feed>
In the next part of this blog, i will integrate this OData service with Windows Phone applications.
No comments:
Post a Comment