次の方法で共有


Business Apps Example for Silverlight 3 RTM and .NET RIA Services July Update: Part 23: Azure

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 https://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.

Comments

  • Anonymous
    August 21, 2009
    Awesome tutorials. I can't read them all, but I know there're here now in case I need them someday. Thanks for the user education resource.

  • Anonymous
    August 21, 2009
    Thanks for the tutorials they're awesome (can't wait for the next one). I still haven't received my token for the SQL Azure service but can't wait to get cracking with this. How about a tutorial on having both a webrole and a workerrole for this demo. How would you handle multiple workerroles that read from the queue and submit data? ex. user imports an excel document to the SQL server and the webrole reads each row and sends it to the queue. On the other end the workerroles are listening and storing the data. That would be useful :) thanks  nisbus

  • Anonymous
    August 21, 2009
    Btw, Add new doesn't seem to work in the live demo?

  • Anonymous
    August 21, 2009
    > Btw, Add new doesn't seem to work in the live demo? nisbus  - Add seems to work in my testing.. I just add a super hero named "Brad Abrams"..  Remeber number of issues needs to be greater than 100 to show up..  

  • Anonymous
    August 21, 2009
    Brad, these are great, thanks. I know you gave a link to Nikhil's blog that has an example using Azure Tables, but it's not in the same format or style as yours and has lot's of other different topics as well. Can you add to your series how to just move your RIA sample to use Azure Tables? I also agree with nisbus - I know enough about Azure Queues to know that I don't understand them and that I should be using them. How would I migrate your samples to use Queues?

  • Anonymous
    August 22, 2009
    lol, sry add new works just fine. I didn't realize there was more than 1 page and I was on the last page :) my bad

  • Anonymous
    August 22, 2009
    good tutorial, r'u have a Pdf version ?

  • Anonymous
    August 23, 2009
    The comment has been removed

  • Anonymous
    October 20, 2009
    The comment has been removed