(X) Hide this
    • Login
    • Join
      • Generate New Image
        By clicking 'Register' you accept the terms of use .

Azure Services connecting Windows Phone to Data

(7 votes)
Samidip Basu
>
Samidip Basu
Joined Aug 22, 2011
Articles:   16
Comments:   24
More Articles
12 comments   /   posted on Sep 21, 2011
Categories:   Data Access , Windows Phone

Tweet This!This article would be a natural continuation of my previous post on “Connecting Azure and Windows Phone through OData” and show a different way of leveraging Azure cloud services for your Mobile applications. 

Also, Peter Kuhn (aka MisterGoodcat) did a brilliant article series on “Getting Ready for the Windows Phone 7 Exam 70-599” and I wanted to expand on the Data Access Strategy section by talking more about connectivity to Windows Azure.

Prelude

In the last article (here), we talked about how OData can really be very handy when providing cloud data backend support for a variety of mobile & web platforms. We simply had a SQL Azure database & used the SQL Azure services to expose the data through an OData feed straight out of Azure, which had full support for CRUD operations given the right roles. However, this approach might seem brute-force for some project needs. For one, we did not have any granularity over how much of a Table’s data we wanted to expose; it was all or nothing. Also, what if you did not want to expose all of the data, but just a few operations on it? And what if you really wanted to have your database on-premise; yet use the ubiquitous Azure services to empower Mobile applications to perform operations on the data? That’s what we talk about in this installment :)

Download Source Code here.

The Database

Now, let’s say we are trying to write a Windows Phone application that interacts with data housed in a remote Database. This is obviously quite a common scenario, because it allows for the data to be utilized/consumed on other platforms. Now, for this demo, I chose to use a simple Database hosted in SQL Azure, since I find it very easy to do so. It is important to note that this Database can be just about anything, on-premise or in the cloud; as long as .NET can connect to it, we should be fine. That said, here’s my Azure Portal set-up:

PatientData DB

You’ll notice that I have used one of my Azure subscriptions to create a “PatientData” Database in my SQL Azure server .. this will house our super-secret patient information :) Now, before moving on, you may want to copy the appropriate Connection String to this Database so that we may connect to it remotely. Now, let’s get inside our “PatientData” Database and create a simple table, also called “PatientData”, & fill in some dummy data like here:

Patient Table Data

The data schema is over-simplified: the ID field is an auto-incrementing integer & we just keep track of Patient “Name” & “Condition” – told you we were dealing with sensitive data :)

Again, this Database could be anywhere. What we want to do however, is to have a Service that is globally accessible from mobile applications and one that interacts with the backend Database only supporting agreed-upon operations.

The Service

We want a simple a service that interacts with our backend Database & one that can be consumed from a Windows Phone application. Again, Windows Azure seems like one of the easiest places to host such a service and make it globally accessible. So, here’s our goal:

  • Create a Windows Azure cloud project
  • Use ADO.NET Entity Data Model as an ORM Mapper to our backend Database
  • Create a simple WCF Service inside of a Azure WebRole
  • Utilize the ORM context to expose specific operations through the WCF service
  • When all is done & working, host the WCF service in Azure

This is how we start:

AzureServiceProject

WCFServiceInWebRole

Next step, we need a way to map the relational Database table into .NET world objects .. this is where an Object Relational Mapper [ORM] steps in. For our purpose, let’s add the ADO.NET Entity Data Model to our project, like below:

ORM Mapping Model

As a part of adding the ADO.NET Entity Data Model, you step through a wizard that is essentially going to point to a Database table to reflect upon & provides column-mapping as Properties to a .NET object. This is where we get to utilize the “Connection String” that we had copied from the Azure Portal, so that we can point ADO.NET Entity Data Model to our SQL Azure table, as shown in the steps below:

ORM Data Connection

ORM DBO

Generated Data Model

The last image shows generated “.edmx” file that ADO.NET Data Entity Model created, on being pointed to our Database Table. That’s it! Now we have “PatientData” as an entity in our C# code. The final project structure should look like this:

Azure Service Project

Now, let’s define the “PatientService” Interface with exactly the operations we want to support & implement them. For this demo, I chose to do two – one that gets us a list of “Patients” & one that allows for adding “Patients”. Here’s the simple Interface:

 1: namespace PatientService
 2: {  
 3:     [ServiceContract]
 4:     public interface IPatientServiceInterface
 5:     {
 6:         [OperationContract]
 7:         List<PatientData> GetPatientList();
 8:  
 9:         [OperationContract]
 10:         string AddPatient(PatientData newPatient);
 11:     }
 12:       
 13: }

And the implementation:

 1: namespace PatientService
 2: {    
 3:     public class PatientDataService : IPatientServiceInterface
 4:     {
 5:         #region "Interface Implementation"
 6:  
 7:         public List<PatientData> GetPatientList()
 8:         {
 9:             var context = new PatientDataEntities();
 10:             return context.PatientData.ToList<PatientData>();
 11:         }
 12:  
 13:         public string AddPatient(PatientData newPatient)
 14:         {
 15:             try
 16:             {
 17:                 var context = new PatientDataEntities();
 18:                 context.AddObject("PatientData", newPatient);
 19:  
 20:                 context.SaveChanges();
 21:                 return "Success!";
 22:             }
 23:             catch (Exception e)
 24:             {
 25:                 return e.Message;
 26:             }
 27:         }
 28:  
 29:         #endregion
 30:     }
 31: }

Notice how we are easily able to utilize the “PatientDataEntities” mapping provided by ADO.NET Entity Data Model as a “Context” within our service implementation. Now, with our service looking like it has all the needed pieces, we do some testing locally to make sure that the methods invoked do actually reach out to our SQL Azure Database & do what they are supposed to do. If satisfied, comes the next step of hosting our service in Azure for ubiquitous availability. This is where the “Windows Azure Project” template helps, as it has already provided an Azure wrapper for our WCF Service. Here the steps to host our simple service in Azure:

  • Right Click on the “AzurePatientService” cloud project & select “Publish
  • Then, “Create Service Packages only
  • This should build our Project & produce two deliverables
  • One is the Cloud Service Configuration File (.cscfg)
  • Other, the actual Project package (.cspkg)

The steps are demonstrated below:

Create Service Package

Package Location

Once the deployment packages are created locally, you may want to copy the directory file path, as we shall need it in the next step. Now, to actually hosting the Service in Azure – Create a new “Hosted Service” in the Azure Portal, with appropriate settings. Notice how we use our local directory file path to point Azure where to pick up the Service Configuration & Project Package from. Also, for demo purposes, we are directly making our Service live in “Production”; actual Services may benefit from being tested in “Staging” before going to “Production”.

Host Service

Azure-Hosted Service

Now, little wait till Azure spins up the resources needed to host our Service & voila, we are live in Azure! Let’s hit the WCF endpoint to make sure our Service is properly hosted & its metadata is accessible. If you’re with me so far, you now know how to deploy & host simple WCF Services in Azure! The rest will be easy :)

The Windows Phone Client

Now that we have our WCF Service hosted & accessible in Azure, consuming it from our Windows Phone Mango application will be standard procedure. Let us aim to build a simple Windows Phone client that leverages the two operations supported by our Service – show list of “Patients” & ability to add new “Patients”. My demo UI looks like the following:

WP7 Patient List  Add Patient  Updated List

As we start building our Windows Phone client, one of our first steps would be to “Add Service Reference” to our Service, so that our project knows the metadata & exposes methods from the Service. This is as simple as copying the URL to our Azure-hosted Service & adding it as a Reference as below:

Add Azure Service Ref

That’s it! .NET builds the proxies & we are ready to consume our Service methods. Here’s how we request the “List of Patients” from our Service:

 1: PatientServiceInterfaceClient PatientServiceClient = new PatientServiceInterfaceClient();
 2: PatientServiceClient.GetPatientListCompleted += new EventHandler<GetPatientListCompletedEventArgs>(PatientServiceClient_GetPatientListCompleted);
 3: PatientServiceClient.GetPatientListAsync();
 4:  
 5: void PatientServiceClient_GetPatientListCompleted(object sender, GetPatientListCompletedEventArgs e)
 6: {
 7:     ObservableCollection<PatientData> patientList = e.Result;
 8:     this.lstPatients.ItemsSource = patientList;
 9: }       

And the little XAML code to bind the results to UI:

 1: <ListBox Name="lstPatients" Margin="20,0,0,60">
 2:     <ListBox.ItemTemplate>
 3:         <DataTemplate>
 4:             <StackPanel Orientation="Vertical">
 5:                 <StackPanel Orientation="Horizontal">
 6:                     <Image Source="/Images/Medicine.png" Height="50" Width="50"/>
 7:                     <TextBlock Text="{Binding Name}" FontSize="40" Margin="10,0,0,0" Style="{StaticResource PhoneTextAccentStyle}"/>
 8:                 </StackPanel>
 9:                 <TextBlock Text="{Binding Condition}" FontSize="32" Margin="60,0,0,20"/>
 10:             </StackPanel>
 11:         </DataTemplate>
 12:     </ListBox.ItemTemplate>
 13: </ListBox>

Here’s how we use the Service to “Add Patients”:

 1: // Instantiate the client.
 2: PatientServiceInterfaceClient PatientServiceClient = new PatientServiceInterfaceClient();
 3:  
 4: PatientData newPatient = new PatientData();
 5: newPatient.Name = this.txtName.Text.Trim();
 6: newPatient.Condition = this.txtCondition.Text.Trim();
 7:      
 8: // Invoke method asynchronously.
 9: PatientServiceClient.AddPatientCompleted += new EventHandler<AddPatientCompletedEventArgs>(PatientServiceClient_AddPatientCompleted);
 10: PatientServiceClient.AddPatientAsync(newPatient);
 11:  
 12: void PatientServiceClient_AddPatientCompleted(object sender, AddPatientCompletedEventArgs e)
 13: {
 14:     if (e.Result == "Success!")
 15:     {
 16:         MessageBox.Show("Voila, Saved to the Cloud :)", "All Done!", MessageBoxButton.OK);
 17:         this.NavigationService.Navigate(new Uri("/PatientList.xaml", UriKind.Relative));
 18:     }
 19:     else
 20:     {
 21:         MessageBox.Show(e.Result, "Save Error!", MessageBoxButton.OK);
 22:         this.NavigationService.Navigate(new Uri("/PatientList.xaml", UriKind.Relative));
 23:     }
 24: }      

 

Conclusion

So, what we talked about so far was ways in which Azure can be of much benefit in hosting WCF Services, which in turn communicate with remote Databases & could easily provide CRUD operation functionality to your backend server. You could take this concept & literally make your services do anything you want .. Azure just makes hosting a snap, with high availability & failover. And, each of these Services can be easily consumed from Mobile applications, like the Windows Phone one in our case. Hope this article demonstrated some techniques & got you thinking on how to Service-enable your Mobile applications. Thanks a lot for reading & cheers to SilverlightShow.

 

About Author

ActualPic

Samidip Basu (@samidip ) is a technologist & gadget-lover working as a Manager & Solutions Lead for Sogeti out of the Columbus OH Unit. Having worked on WP7 since CTP days, he now spends much of his time in spreading the word to discover the full potential of the Windows Phone platform & cloud-based mobile solutions in general. He passionately runs the Central Ohio Windows Phone User Group (http://cowpug.org/) and can be found with atleast a couple of hobbyist projects at any time. His spare times call for travel and culinary adventures with the wife. Find out more at http://samidipbasu.com/ .

Subscribe

Comments

  • KarimAly

    Re: Azure Services connecting Windows Phone to Data


    posted by KarimAly on Apr 19, 2012 21:19

    Hi , first of all , great tutorials , i have read most of Them and liked so much :)

    but i have a problem in here if you can help me :)

    The Problem : i have deployed windows azure project as you said but still the link doesn't work

    http://xxxxxxx.cloudapp.net/Service1.svc

    It gives me 500 -Internal server error ,, Any help will be appreciated :)
  • samidip

    Re: Azure Services connecting Windows Phone to Data


    posted by samidip on Apr 20, 2012 02:45

    KarimAly,

    Thank you for the comment & kind words.

    Let's knock out potential error conditions one by one:

    1. Does adding a "/" at the end of the svc in the URL work?
    2. In your cloud project, could you right click on the svc file & view in browser? What error do you see, if any?
    3. Is the DB connection string correct?
    4. What operations does the WCF service support?
    5. Are you using latest Azure SDK?

    Lemme know.

    Thanks!

  • KarimAly

    Re: Azure Services connecting Windows Phone to Data


    posted by KarimAly on Apr 20, 2012 12:41

    Thanks for your fast reply :)
    And i think i have solved it , that's what i did :

    I made an ASP.NET Web role then added "WCF Service" item to the web role project

    I published the azureProject and it worked great -- That's before i use ADO.NET Entity Data Model --, i used just a function which returns a string and added the service reference in the WP client , everything worked just fine.

    I then Added ADO.NET Model and returned a List as your tutorial but WP couldn't read the namespace "ServiceReference1" ! : ( , the problem is not in connecting to the database cuz when i returned a string even after using ADO.NET Model , it worked fine

    So to check that : i deleted the Model and made a function that returns an array of numbers but the same problem occured again !
    i think WP Client can't use functions that returns arrays or list , i don't know if that's correct , it's just a suggestion :)

  • KarimAly

    Re: Azure Services connecting Windows Phone to Data


    posted by KarimAly on Apr 20, 2012 12:47

    Thanks for your fast reply :)
    And i think i have solved it , that's what i did :

    I made an ASP.NET Web role then added "WCF Service" item to the web role project

    I published the azureProject and it worked great -- That's before i use ADO.NET Entity Data Model --, i used just a function which returns a string and added the service reference in the WP client , everything worked just fine.

    I then Added ADO.NET Model and returned a List as your tutorial but WP couldn't read the namespace "ServiceReference1" ! : ( , the problem is not in connecting to the database cuz when i returned a string even after using ADO.NET Model , it worked fine

    So to check that : i deleted the Model and made a function that returns an array of numbers but the same problem occured again !
    i think WP Client can't use functions that returns arrays or list , i don't know if that's correct , it's just a suggestion :)

  • samidip

    Re: Azure Services connecting Windows Phone to Data


    posted by samidip on Apr 23, 2012 17:58

    Karim,

    WP should surely be able to consume services that return arrays or lists. Are you adding the ADO.NET Data Entity Model? Did the .edmx file get created ok? What is your service returning?

    If you want to take the conversation offline, you can email me @ firstname@live.com. I'll be happy look through some of your code.

    Thanks!

  • marimon

    Re: Azure Services connecting Windows Phone to Data


    posted by marimon on Jun 12, 2012 10:23

    Hello, my question is where is my .xaml file? I don't see it in my solution explorer when I created the webrole project. How can I add it so I can start developing? thanks!

  • marimon

    Re: Azure Services connecting Windows Phone to Data


    posted by marimon on Jun 12, 2012 10:30

    Hello, my question is where is my .xaml file? I don't see it in my solution explorer when I created the webrole project. How can I add it so I can start developing? thanks!

  • samidip

    Re: Azure Services connecting Windows Phone to Data


    posted by samidip on Jun 12, 2012 18:37

    Marimon,

    Not following your question. The webrole is a WCF service; why would you expect a XAML file? If consuming services from Windows Phone client, that will have XAML files as front-end UI. Please lemme know if you have more questions.

    Thanks!

  • marimon

    Re: Azure Services connecting Windows Phone to Data


    posted by marimon on Jun 13, 2012 05:20
    Thanks for the reply, I think what I am confused about is how to use the webrole service in a windows phone 7 application. Is it as simple as adding a silverlight project within the webrole project?

    Also, I am having a problem with publishing the webrole. I do not see the option “Create Service Packages only” option. When I click publish, I get a popup that has  steps: sign in, settings, and summary.

    Sorry for asking such trivial questions, I am new.

  • samidip

    Re: Azure Services connecting Windows Phone to Data


    posted by samidip on Jun 13, 2012 14:58

    Marimon,

    No worries at all .. we all learn from each other :). Now, there are two aspects of this as you already understand - the WCF service which is running in a WebRole and the Windows Phone client. The WCF service can really be hosted anywhere; but you would use the WebRole if leveraging Azure. The WCF service & the Windows Phone app actually do not need to belong in the same VS solution. 

    So first, create the WCF service in a WebRole. If using the latest Azure SDK, you can publish directly using your Azure subscription details or create a local package (right click) & then upload to Azure. Once done & hosted, grab the URL of the hosted service (ending in .svc) and add a service reference to it in your separate Windows Phone solution.

    Hope this helps!

  • RandyLim

    Re: Azure Services connecting Windows Phone to Data


    posted by RandyLim on Aug 21, 2012 05:03
    Hi, dont know can you help me, but i am stuck at the ADO.NET Entity Data Model part. My new to this so i have some problems. the connection string, how do you link it to azure? or is it some other database? I cant seems to understand. Thanks
  • samidip

    Re: Azure Services connecting Windows Phone to Data


    posted by samidip on Aug 22, 2012 05:05

    RandyLim,

    Thanks for your question. If you are connecting directly to a SQL Azure DB, the Connection String can be found on the right hand side on the Azure portal, after you have the DB server selected. If connecting to a service that is sitting on top of the DB, you do not need a direct connecting string; but rather work with the service endpoint.

    Please lemme know if this helps or of other problems you encounter.

    Thanks!

Add Comment

Login to comment:
  *      *       

From this series