When you create a Windows Phone application you might have some data to store. Where do you store it?
- Well, there’s always Isolated Storage. I discuss that here.
- There’s always a local SQL Database. I discuss that here.
But, what if on-the-phone isn’t good enough? …to the cloud! I discuss that here [there’s no link because this is the article! :)].
Why the cloud?
There are lots of great reasons. Here are a few:
- Your data needs to be updated or refreshed, call the cloud
- Your data needs to be saved for roaming users, call the cloud
- Your data is huge and phones only need a bit, call the cloud
Before you do anything, you might want to check if there’s even a network to use. To detect the network, WiFi, and so much more, read this article.
Creating a Service Reference to some remote Web Service is a snap.
MSDN: The Internet is host to an extensive variety of web and data services that you can use in your Windows Phone applications to create compelling new user experiences. This topic introduces networking and web services, and describes the primary classes and utilities that you can use for building web-integrated Windows Phone applications. [snip/] Because the vast majority of web services published on the Internet are based on HTTP, you can use the HttpWebRequest and WebClient classes to access web services from Windows Phone applications. To help ease the task of generating the additional code that web services often require, you can use the Silverlight Service Model Proxy Generation Tool (SLsvcUtil.exe) or the Visual Studio Add Service Reference feature to generate a proxy class.
When connecting to a web service that requires an application key, do not store the application key with an application that will be run on a device. Instead, you can create a proxy web service to authenticate a user and call an external cloud service with the application key. For more information about security recommendations, see Web Service Security for Windows Phone.
Let’s see it!
In the following video, we’ll create both the phone application and the web service:
In the video above, I create a service-based Windows Phone application. I’m the kind of guy who likes to have samples be simple so I can get the technique without having to understand some complex back story. Hopefully, that rings helpful for you like it does for me. Here are the key steps:
- Create the Windows Phone application project
- Create the Windows Communication Foundation (WCF) service application
- Implement the logic in the WCF service
- Add a Service Reference to the WCF service in the WP application
- Implement the UI in the WP application
- Call the service and watch the magic
Adding cloud services to your windows phone application can add a lot of flexibility and power to your application. The better your application, the less likely users will hit that UNINSTALL button – after all, that’s what we are working against, here, right?
A few gotchas to remember:
- Using the cloud means you need a network
- The user might be out of signal reach
- The user might be in airplane mode
- Be a good bandwidth user – don’t overdo it
Adding a Service Reference creates the asynchronous proxy code for you – then, as the developer, the rest it up to your creativities.
Best of luck!