Mobile Zone is brought to you in partnership with:

Per Ola is currently working as CTO in mBricks (www.mbricks.no) and is a Microsoft Device Application Development MVP. Per Ola got his bachelor degree in Software Development back in 2005. Since 2005 he has been passionately working with mobility projects on a wide range of technologies and areas of use. Driven by his passion for this craft he spends a lot of his time off work to further explore technology possibilities or new programming languages or techniques. He finds himself privileged to be able to make a living from one of his hobbies. Per Ola has experience from a wide range of projects covering developing applications for the consumer market and for the enterprise market. He has had several different roles: developer, architect, team leader, project manager, technical resource in pre-sale and strategic planning. Per Ola is a DZone MVB and is not an employee of DZone and has posted 26 posts at DZone. You can read more from them at their website. View Full User Profile

How to use WCF services to access SQL Azure Database from Windows Phone 7 app – Part 2

02.15.2011
| 9042 views |
  • submit to reddit
This is part two in a series of four and will step by step explain how to use WCF services to access SQL Azure Database from Windows Phone 7 app. As an example I will develop a Windows Phone app where the user can create an account and later on log in to the account by user name and password. The accounts are saved in SQL Azure and I am using WCF for communication between the WP7 app and SQL Azure Database.

The book Beginning Windows Phone 7 Development has a very detailed chapter about using SQL Azure Database.

Part 1: Signing up to Windows Azure and create your SQL Azure Database

Part 2: Creating a cloud service (WCF service) to connect to the SQL Azure Database

Part 3: Creating a Windows Phone 7 app using WCF to connect to the SQL Azure Database

Part 4: Deploying the WCF service to Windows Azure

Creating a Windows Azure project in Visual Studio 2010

You need to create a Windows Azure project in Visual Studio 2010. Click  “New project” and select Cloud template. You need to have installed Windows Azure SDK before doing this. When you create your Cloud project you get prompted about .NET Framework 4 roles. For this project you will select WCF Service Web Role. We now have a Cloud project ready to use.

Creating Object Model

We will now implement a layer to interact with the database and we are using Entity Framework to do so. By using refactor I rename the “WCFServiceWebRole1″ to “CloudExampleServiceRole”. Right click the CloudExampleServiceRole in the solution explorer and click “Add” and “New item”. Select the “Data” template and “ADO .NET Entity Data Model”, give it a name and click “OK”. An Entity Data Model wizard will pop up, select “Generate from database” and “Next”. Select “New Connection” , choose “Microsoft SQL Server” and “Continue”. Then you need to add connection properties to the SQL Azure Database. Enter the database name, use SQL Server Authentication with the user name and password you created for your SQL Azure Database. Select the database you want to connect to and click “OK”. Select “Yes, include sensitive data in the connection string” and click “Next”. Select the User table and click “Finish”.

You have now created the object model for the User table in your SQL Azure Database.

Create a WCF contract

We will create a WCF contract so that we can add a user and login a user from the database.

Replace the auto generated code in IService1.cs with the code below.

using System;
using System.ServiceModel;

namespace CloudExampleServiceRole
{
[ServiceContract]
public interface IService1
{
[OperationContract]
void AddUser(string fullName, string userName, string password);

[OperationContract]
string LoginUser(string userName, string password);
}
}

Creating the service

We will now create the service with a method for AddUser and a method for LoginUser.

Replace the auto generated code in Service1.svs.cs with the code below.

using System;
using System.ServiceModel;
using System.Data.Objects;

namespace CloudExampleServiceRole
{
public class Service1 : IService1
{
public void AddUser(string fullName, string userName, string password)
{
using (var context = new CloudExampleEntities())
{
context.AddToUser(new User()
{
UserName = userName,
Password = password,
FullName = fullName,
});
context.SaveChanges();
}
}

public string LoginUser(string username, string password)
{
string query = @"SELECT value User.FullName FROM CloudExampleEntities.User AS User WHERE User.UserName = @username AND User.Password = @password";
ObjectParameter[] parameters = new ObjectParameter[2];
parameters[0] = new ObjectParameter("username", username);
parameters[1] = new ObjectParameter("password", password);

using (var context = new CloudExampleEntities())
{
ObjectQuery<string> results = context.CreateQuery<string>(query, parameters);

foreach (string result in results)
{
if (result != null)
{
return result;
}
}
}
return null; ;
}
}
}

Running the Azure WCF locally on your machine

We have completed the service and can run it by pressing F5 in Visual Studio, this will launch the service in your browser (do not close while using the service locally) and Windows Azure Emulator will run in the background. Note that this might differ depending on what OS your are using, I am using Windows 7.

Next step

We have now created the Azure project with object model and WCF services. The next step now is to consume these services from the Windows Phone 7 application. Part 3: Creating a Windows Phone 7 app using WCF to connect to the SQL Azure Database.

 

References
Published at DZone with permission of Per Ola Sæther, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)