Cloud and Windows 7 phones are two common terms that you can hear from many tech-savvy people. So I thought it was the right time to integrate these
technologies. In this article, I have tried my best not to focus on the theoretical aspects. Instead, I will present a step-by-step approach on how to
consume Data from cloud in a Windows 7 phone application.
In this article, we will cover
- Migrating local Database to SQL Azure
- Creating Data Model on Database in Cloud
- Expose Data Model as WCF Data Service
- Consumption of Data from cloud in Windows 7 phone through WCF Data Service.
The block diagram for the same is given below. We have a School Database on our local (In premise) SQL Server. We will be moving the local school
database along with the schema and data to SQL Azure. Then, we will create a Data Model on the school database in the cloud and expose that Data model as WCF
Data Service. Once the data from cloud is exposed as WCF Data Service, we will fetch that in the Windows 7 phone
So, actually, there are two major
- Exposing Data from cloud as WCF Data Service
- Consuming WCF Data Service in Windows 7 Phone
Exposing Data from cloud as WCF Data Service
There are two steps mainly involved in this process:
- Creating local Database and migrating to SQL Azure
- Exposing SQL Azure Database as a WCF DATA Service
Step1: Creating local Database and migrating to SQL Azure
The first step is to create the database. We are going to use School database. Script of sample School Database copy from here
Right click on School Database and select Tasks. From Tasks, select Generate Script.
From the Pop up, select
Set Scripting option.
Give the file a name by selecting Save to file option.
Now the main thing to be noticed here is that we need to so some change in the
advanced setting. For that, click on Advanced options.
And in types of data to script, select Script and Data - both options.
After that, click next and Finish. You can see that a SQL file is created,
and we will be using this script to migrate our in-house school database to SQL Azure.
Create School Database in SQL Azure
Login the SQL
Azure portal with your live credential
Click on the SQL Azure tab.
You will get the project you have created for yourself.
Click on the project. In my case, the project name is
debugmode. After clicking on project, you can list the
entire database created in your SQL Azure account.
Here, in my account, there are two databases that have been already created. They are the master and student database.
Master database is the default database created by SQL Azure for you.
Click on Create Database.
Give the name of your database. Select the edition as Web and specify the
max size of database.
You can select the other option also for the edition as business
Next, click on Create, you can see on the Databases tab that Demo1 database
has been created.
Run the Script in SQL Azure
Open the SQL Server management Studio.
You will get Connect to server dialog box. Click on cancel.
After cancelling the dialog box,
click on New Query (top left corner).
On clicking New Query, the Connect to Server dialog box appears again.
Now, here you need to provide the Server name of SQL Azure and Login credential of
To know what is the database server name of SQL Azure portal, login to Windows Azure portal with your live credential and then click on
the SQL Azure tab
will get the server name in form of
where abc is name of your SQL Azure server. We need to provide this
server name at the local SQL server management studio.
Make sure to select SQL Server Authentication and provide login user name and password of your SQL Azure database portal.
After that, before clicking Connect, click on Option
From Option, select School database.
Now once you have successfully connected to School Database in SQL Azure, copy the script and Run
as given below.
successfully runn script, run the below command and all the tables name will get listed.
In this way, you have successfully migrated database to SQL AZURE.
Step2: Exposing SQL Azure Database as WCF DATA Service
Create a new project and select ASP.Net Web Application project template from the Web tab. Give a meaningfull name to the web application.
We can create a Data Model, which
can be exposed as WCF Data Service in three ways
- Using ADO.Net Entity model.
- Using LINQ to SQL class.
- Custom Data Model.
Here, I am going to use ADO.Net Entity model to create the data model. So to create an entity model, do the following:
- Right click on web application and add a new item
- Select ADO.Net Entity model from Data tab.
Since we have table
in SQL Azure DataBase, we are going to choose the option Select from database.
Choose a new connection.
After clicking on New Connection, this is the important step. We need to give extra care
So provide the information as below,
Click on Test Connection to test if the connection established successfully or not? After this, you will get prompted if
the Connection string contains sensitive data regarding if you want to keep that in configuration file or mange through program. You can select any one of
these options according to what you need.
On providing your option, click on Next button; you can see that all the tables, Views, and Stored Procedures' are available to be
chosen as a part of the data model for WCF Data Service.
Select tables, views and stored procedure from the database you want to make as the part of your data model.
Creating the WCF Data Service
Right click on Web Application project and add a new item.
Select WCF Data Service from Web tab.
Give any meaningful name. I am leaving the default name here.
After adding the WCF Data Service, we can see a service file with extension .svc has been added to the solution
First, we need to put data source name. To do so, uncomment the first commented line and put the data source name. In our case, the name of
the model which we created in the 2nd step is the data source. Our data model name is
Now we need to set access rules for
the entity or entity set. Since we have only one table; so either we can give name of the table explicitly or if we want to set the same access rule for all
the table in the data model or data source, we can put *.
So we are setting the access rule that, on the entity in data source perform all the operations.
So finally, the svc file will look like
Run the WCF Data
Just press F5 to run the WCF Data Service. The Data Service will be hosted in the default ASP.Net server.
On running, you can see
all the table is listed there.
Append the URL with People and you will get all the records from People table
If your browser is not showing the expected result, make sure that the Feed reading of the browser is off. To do so, from the menu of
IE, select Tools-> Internet Options -> Content
So we have exposed data from cloud
using WCF Data service. Now any OADTA client can consume the data from cloud by consuming WCF Data Service.
Consuming WCF Data Service in Windows 7 Phone
First, we need to do is to create proxy of WCF Data service for Windows 7 phone. So to do this,
- Open command prompt in administrator mode.
- Navigate to folder C:\Windows\Microsoft.Net\FrameWork\V4.0. Version of framework may vary depending on .Net framework you are working on.
- Once navigated to this particular folder, run the below command to create proxy class. We will add this proxy class in Windows 7 mobile
- There is space between all /out, /uri and /version
- We are redirecting the output to D drive and Temp folder. So make sure that the Temp folder exists in the D drive.
- School.cs is name of proxy class. You can give any name of your choice here. Once the command runs successfully, you will get School.cs class in
Temp folder of D drive.
- After uri tag, give the uri of WCF data service.
Now after the execution of this command, we will have School.cs in Temp folder of D Drive.
Download the ODATA client library for Windows 7
phone from the below link and save the dll on local drive. We will add this DLL in Windows 7 phone application.
Create a windows 7
phone application. Open Visual Studio and select the Windows phone Application project template from Silverlight for Windows Phone tab.
Right click on the project and add
existing item. Then navigate to Temp folder where you have created the WCF Data Service proxy class. In our case, it is School.cs. We created this in the
need to add ODATA client library reference in the Windows 7 Phone application. Right click on the project and add reference. Navigate to the folder where you
Display Data from Person Table in Windows 7 Phone application
We are going to display records of Person table in the list
box. So first, let us create an entity class at client side. Right click and add a class called Person
Listing 1: Person.cs
Create a list box and we will bind the data in this list box. On click event of the Button, Data will get bound to the list box.
Listing 2: MainPage.Xaml
Add the namespace,
Now on click event of the button, we need to call the WCF Data Service and bind the list box
Listing 3: MainPage.Xaml.cs
Output will get as
- Hosting of WCF Data Service on Windows Azure Web Role
- Hosting of WCF Data Service on IIS
- Hosting of WCF Data Service on App Fabric.
If we are hosting WCF Data Service on Windows Azure Web Role or App Fabric, then Data and service part will be completely in the cloud. In the next
article, we will host WCF Data Service as Windows Azure web role to convert our existing application as a complete tryst of Cloud and Windows 7 phone
Dhananjay Kumar is a developer who blogs at http://debugmode.net/. He is Microsoft MVP ,Telerik MVP and Mindcracker MVP. You can follow him on twitter @debug_mode
This author has published 8 articles on DotNetSlackers. View other articles or the complete profile here.
Please login to rate or to leave a comment.