This is part 1 of 2 from the article series 'Silverlight in the Azure cloud'.
Read part 2
Cloud computing is a hot topic nowadays. The ability to have access to an unlimited amount of resources when we need it is awesome and can help us deliver better and more stable applications, while keeping the cost low. Many vendors, including Microsoft, Amazon and Google have their own cloud implementations.
Since PDC 2008, Microsoft jumped on the cloud-computing bandwagon and introduced Windows Azure. Microsoft envisions the cloud as the future. There are many great advantages that come with cloud computing, including nearly unlimited scaling, high availability and low cost to get started, all offered by Azure. When combining the power, scalability and high availability of Azure with a rich client platform like Silverlight, we can create even more compelling experiences.
Silverlight works great with Azure and vice versa. From a professional point-of-view, I have for my company done a few migrations of existing Silverlight applications towards the Azure platform. Moreover, I know use Azure for delivering intermediate builds of my Silverlight projects to the customer, without all the hassle of attaining and installing a server, making it available outside the domain etc. With the tools I have at my disposal for Azure, I can deploy to the cloud in a snap and we only pay for the days the server is online.
This all inspired me to give a webinar on SilverlightShow titled Switching on the cloud for Silverlight, delivered on March 23 2011 and available for on-demand viewing here. While there’s a lot of information in the talk, I decided to write this 2-part article series as an extra companion to the webinar. Or maybe you don’t have an hour to spend and just want to read about Silverlight and Azure, well in that case, these articles will give you the information you need to get started.
In this first part, we’ll be looking at how we can migrate a regular Silverlight application to the cloud. In the second part, I’ll be focusing on the more advanced stuff, such as migrating RIA services apps, XAP hosting and accessing cloud services from Windows Phone.
The demo for this part can be downloaded here.
Before we go ahead and look at how we can migrate applications to the cloud, let me start by explaining you what you need to know to get started with Azure. I’ve noticed that almost all .NET developers know what Windows Azure is, but not everyone is clear on all the parts that make up Windows Azure.
Things the Silverlight developer needs to know about Azure
To get rolling with Azure, it’s vital that you at least know what Windows Azure is. Basically, it’s Microsoft’s implementation of a cloud platform. It is an operating system for the cloud – a cloud that runs on Microsoft’s datacenters, which are spread all over the world. Roughly said, Azure provides us computing power, storage and the capability to connect our on-premise systems with machines in the cloud. The computing block is a place where code can be crunched and the storage can be used to store an unlimited amount of files with high availability and replication. Since the applications we will want to host on Azure are web applications, it’s important for a Silverlight developer to know how to use the cloud: we may want to host services on Azure, store image files in Azure storage etc.
When starting with Azure, one of the things you need to know is what are the parts it encompasses and how can they be useful for me as a Silverlight developer. Let’s take a look.
Parts of Azure
- Hosted services provide a way of executing applications. Web roles (a role can be seen as an instance) can be used to run an ASP.NET application, a service application. Worker roles can execute other EXEs for example, such as a different web server or runtime.
- Storage gives us a way to store data in the cloud. Blob storage gives the ability to store files in the cloud. Each file that’s stored is replicated so it’s safe to say that the file is difficult to lose once stored in blob storage. Table storage provides a way of storing unrelational data and queue storage provides a way to allow different roles to communicate and store messages in queue.
- SQL Azure is a SQL Server database in the cloud
- AppFabric includes services we can use in our own applications. Access Control allows us to integrate with other authentication systems such as GMail or Facebook. Caching allows us to use a distributed cache. The Service Bus is a messaging system that allows exchanging messages between cloud and on-premise systems.
The Silverlight developer should certainly take a look at Hosted Services, Storage and SQL Azure, since these can make his life a lot easier! Hosted services can be used to run a website from Azure: this can be a site that hosts WCF services as well as a site that hosts the Silverlight application. Azure Storage can be useful to store images, videos and access them from Silverlight. Finally, SQL Azure can replace an on-premise database server.
Migrating an existing Silverlight application to the cloud
To demonstrate the use of hosted services and SQL Azure in a Silverlight scenario, let’s follow the steps to migrate an existing application to Azure. The application, a typical Line-Of-Business application exists out of the following components:
- Data hosted in a SQL Server 2008 R2 database
- Data is made available over a Silverlight-enabled WCF Service (using a BasicHttpBinding)
- Silverlight application is hosted in an ASP.NET application
- Silverlight Navigation application uses the data exposed from the WCF service. It has a service reference to this service
Let’s take a look at the different steps to migrate this application.
Step 1: the database
Up first is the database. The screenshot below shows the current – local – database schema.
In this local database, we already have data.
To migrate this to the cloud, we need to start by creating a new database in SQL Azure. Assuming you already have an account on Windows Azure, we can create and manage these databases from the portal. In the screenshot below, a new database named CityHotels was created. Since the database is rather small, I selected web as type.
With the database created, we need to migrate our existing database schema and the data to the cloud database. A useful tool for this is the SQL Azure Migration Wizard, available from CodePlex. By following this wizard-driven tool, we can analyze and migrate a database. The screenshot below shows the tool in action.
It’s recommended to allow the tool to do a migration first. Not everything that a regular SQL Server database can do can be copied and this analysis will find exactly those things. I remember at one customer that we had to perform a manual change on a table from ASP.NET Membership. After the tool is ready, the database should be available.
To finish this first step, we can change the connection string in the web.config of the services so it refers to the database in the cloud. Nothing else needs to change to allow this to work, as this database is accessible from our application.
<connectionStrings>
<add name="CityHotelsEntities"
connectionString="metadata=res://*/CityHotelModel.csdl|res://*/CityHotelModel.ssdl|res:
//*/CityHotelModel.msl;provider=System.Data.SqlClient;provider
connection string="data source=vextmrs0m2.database.windows.net;initial
catalog=CityHotels;user id=gillcleeren;
password=SOMEPASSWORD;multipleactiveresultsets=True;App=EntityFramework""
providerName="System.Data.EntityClient" />
</connectionStrings>
Step 2: the services
Once the database is moved to Azure, we can continue our migration quest by moving the service layer to Azure as well. Assume like in the following screenshot that the services are in a separate project called ServiceHostingSite.
To move this site to the cloud, we need to add a new Windows Azure Project (called here CityHotelBrowserCloud). We don’t need to have the template create any empty projects, so when adding the project, just click OK to continue.
In the Solution Explorer, the new project is added. Now right-click on the Roles folder and select Add Web role project in solution. Go ahead and select the ServiceHostingSite project.
By adding this role, we basically configured an instance to execute this code. If we right-click on the project and select Properties, we can configure how the role should behave. In the screenshot below, I have configured 2 instances, meaning that 2 virtual machines will be created that both run my code independently. Should one fail, the second one is still up while the first one reboots. We can also select VM size here. It’s easy to see that the more instances you configure, the more expensive things will be. Also, selecting larger instances will also increase the cost.
If we now run this, we see the emulator start up.
The Compute Emulator show the running instances.
If we look at the address in the browser bar, we can see this site (the ServiceHostingSite) is now running on http://127.0.0.1:81/. We therefore need to update the endpoint address in the ServiceReferences.clientconfig in Silverlight as well:
<endpoint address="http://127.0.0.1:81/CityHotelService.svc"
binding="customBinding" bindingConfiguration="CustomBinding_CityHotelService"
contract="CityHotelService.CityHotelService"
name="CustomBinding_CityHotelService" />
Finally, we can publish the service. On the cloud project, right-click and select Publish. Select the “Create Service Package Only” option. This will generate 2 files: a package containing the site and a configuration file.
Now head back to the Azure portal and select Hosted Services. On the same page, select new Hosted Service and create a name for your service. At the bottom, browse for your package and configuration file. The screenshot below shows the filled-in screen.
After your service was deployed, remember to update the service reference in the Silverlight application to the “live” version of the services.
Step 3: the Silverlight application
We are now ready to move the Silverlight application itself to Azure as well. Create a new web role and select the Silverlight hosting site (CityHotelBrowser.Web) to be the second web role. Alternatively, you can also create a new cloud project and add a web role in there. The latter would in real world scenarios be the best choice; however, the first choice will be cheaper but will be hosting 2 sites on different ports. For simplicity reasons, I’ve selected to host both in the same project here.
When publishing this second web role, the ClientBin including the XAP file will be included and uploaded as part of the hosted service. What happens is that all files are packaged, so the XAP file will be included as well. When a user now browses to the hosting website the Silverlight XAP file will be downloaded from the cloud server instead of our own server.
This does however pose a problem. When we want to update just the XAP file, we can’t just change that one file. Instead, we need to republish. More on this in the second part of this series!
Summary
In this first part, we looked at how we can easily migrate a typical Line-Of-Business application to the Windows Azure cloud. As can be seen, for Silverlight itself, not really a lot is changing. For the server-side code however, quite a lot actually is, mostly behind the scenes. By moving to the Azure cloud, our services, database and hosting website benefit from the high availability and scalability from Windows Azure.
Stay tuned for the second part where I’ll be looking at more advanced scenarios.
About Gill
Gill Cleeren is Microsoft Regional Director (www.theregion.com), Silverlight MVP (former ASP.NET MVP), INETA speaker bureau member and Silverlight Insider. He lives in Belgium where he works as .NET architect at Ordina. Passionate about .NET, he’s always playing with the newest bits. In his role as Regional Director, Gill has given many sessions, webcasts and trainings on new as well as existing technologies, such as Silverlight, ASP.NET and WPF at conferences including TechEd Berlin 2010, TechDays Belgium, DevDays NL, NDC Oslo Norway, SQL Server Saturday Switserland, Spring Conference UK, Silverlight Roadshow in Sweden… He’s also the author of many articles in various developer magazines and for SilverlightShow.net. He organizes the yearly Community Day event in Belgium.
He also leads Visug (www.visug.be), the largest .NET user group in Belgium. Gill recently published his first book: “Silverlight 4 Data and Services Cookbook” (Packt Publishing). You can find his blog at www.snowball.be.
Twitter: @gillcleeren