This is the last part 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 4: Deploying the WCF service to Windows Azure
Create a service package
Go to your service project in Visual Studio 2010. Right click the project in the solution explorer and click “Publish”, select “Create service package only” and click “OK”. A file explorer window will pop up with the service package files that got created. Keep this window open, you need to browse to these files later.
Create a new hosted service
Go to https://windows.azure.com and login with the account you used when signing up for the SQL Azure. Click “New Hosted Service” and a pup up window will be displayed. Select the subscription you created for the SQL Azure database, enter a name for the service and a url prefix. Select a region, deploy to Stage and give it a description. Now you need to browse to the two files that you created when you published the service in the step above and click “OK”. Your service will be validated and created (might take a while).
Using the hosted service in your Windows Phone project
In the Management Portal for Windows Azure you will now see the service you deployed in Hosted Services. Click the deployment and you can see information about it on the right side. Copy the URL you see in the DNS name field. Go to your Windows Phone project and open ServiceReferences.ClientConfig. You can see that the end point address in this file points to the local service you ran earlier. Replace the end point address with the DNS name you just copied. NB. the address must end with “/Service1.cs”.
If your hosted services have status Ready you can now run your WP7 application and it will use the hosted service.
If you have followed the four parts in this series you now have a SQL Azure Database, Windows Azure hosted WCF service and a WP7 application consuming the hosted service. You know have a good base for continuing developing and building complex applications where storage and services are placed in the Cloud. Your apps will be able to scale unlimited and you will only pay for your current usage. Happy coding!