Still updating my Mix 09 Silverlight 3 + RIA Services talk with more fun stuff.    Azure represents a general trend in the industry towards a cloud computing model. As much as I love the development phase of projects, the operations side is often the most cost intensify part.  With economies of scale, cloud computing has the potential to greatly reduce those costs.  I think example I took advantage of Windows Azure to host the web-tier of the application and Sql Azure to host the data.  Those are actually independent decisions.  You could decide to host one and not the other. 

You can see the full series here.

The demo requires (all 100% free and always free):

  1. VS2008 SP1
  2. Silverlight 3 RTM
  3. .NET RIA Services July '09 Preview
  4. Windows Azure Tools for Microsoft Visual Studio July 2009 CTP
  5. Windows Azure Invitation (apply here)
  6. Sql Azure Invitation (apply here)

 

Check out the live site (of course, hosted on Azure) Also, download the full demo files

So in this example, I can going to move the data from on premises Sql Server to SQL Azure AND move the applogic code from running on my own web server to running on Windows Azure.  The main client will stay Silverlight. 

 

image

 

Setting up SQL Azure

Once you get an invite code for SQL Azure, you can go to http://sql.azure.com and add a new database, just by clicking add new Database

image

Then you get a nice connection string..

image

Then you need to populate the database.  If you have an existing database, the best way to do this likely to use Sql Management Studio to get a Sql script and then using ADO.NET to execute that script on this Sql Azure database. 

But for no good reason, I wrote a very simple bit of ADO.NET code to read the data out of my local database and insert it into the Sql Azure based one.  You can find it in the default.aspx.cs page if you want to check it out.

 

Setting up Windows Azure

That sets up our database in the cloud, now let’s create a place for our web application to run.  Once you get an invite code from Windows Azure you can go in an setup your service.  Go to windows.Azure.com and add a new service

image

Select the Hosted Service

image

 

 

 

Setting up the Project

Start up Visual Studio as an administrator… the “local cloud” support requires this right now. 

image

 

Then create a new Cloud Service

image

For this example, all we need is a Web Role

image

I renamed “WebRole1” to MyApp.Web to match the pervious examples.

Hitting F5 starts up the “local cloud” and runs it there..

image

Creating the Application

Now, let’s start building the application.  First let’s build out the data layer.   I already had a Linq2Sql model from a pervious example, so I just copied that. I did tweek a few of the datatypes to match what I loaded into Azure…   Entity Framework would have worked just as well. 

image

Then I changed the connection string in Web.config to match the connection strings of the Sql Azure database.

<add name="NORTHWNDConnectionString" connectionString="Server=tcp:jyfmd9f7te.ctp.database.windows.net;Database=northwind;User ID=[add userid];Password=[add password];Trusted_Connection=False;"
  providerName="System.Data.SqlClient" />

Now it is just like the original walk through.. 

Create the Service

image

Write some code in it…

 

public IQueryable<SuperEmployee> GetSuperEmployees()
{
    return this.Context.SuperEmployees
               .Where(emp => Convert.ToInt32(emp.Issues) > 100)
               .OrderBy(emp => emp.EmployeeID);
}
public void InsertSuperEmployee(SuperEmployee superEmployee)
{
    this.Context.SuperEmployees.InsertOnSubmit(superEmployee);
}
 
public void UpdateSuperEmployee(SuperEmployee currentSuperEmployee)
{
    this.Context.SuperEmployees.Attach(currentSuperEmployee, this.ChangeSet.GetOriginal(currentSuperEmployee));
}

Now we need to add a Silverlight Project

image

And associate it with the MyApp.Web project and enable RIA Services.

image

Now you can simply use the exact same Silverlight client we built before

image

You get sorting, paging, filtering, validated editing, batching all with no plumbing code and now, all hosted in cloud!

 

Deploying to the Cloud

Now that we have our app done, let’s deploy it to the cloud.

First we need to create the deployment package.  To do that right click on the MyApp.CloudService project and Select Publish

image

The publish directory will up in explorer with your application deployment package and a separate settings file

image

The deployment package is just a zip file that contains the silverlight xap an the code and resources to execute on the server.  The settings file contains things like number of front-end servers you want to create, etc.

Now, go back to windows.azure.com and select the project we created earlier.

There are two places for your app to run.. Staging and Production.  The idea is you can do testing in the real environment before you switch your live site over. 

Select upload to add your project to staging.

image

Select deploy..

image

The add the package and settings and give it some deployment label to help us keep track of its movements between staging and production.

image

Then click run to start the instance running. Then it will say “initializing…” for a few minutes

image

 

Finally, when it says “started” you can run the app by clicking on the test link:

 

image

 

Next swap this into the deployment stage and you are live!

image

 

And we are done! 

image

image

In this section we talked about how to deploy your RIA Services application to the cloud using Sql Azure for the data and Windows Azure for the web tier.  You can mix and match those however you want.

Also, check out Nikhil’s example on Azure, it uses Windows Azure table storage which is very cool as well.