Deploy your ASP.NET Web API application to Windows Azure in 3 minutes - StrathWeb

Strath

July 8th, 2012

Deploy your ASP.NET Web API application to Windows Azure in 3 minutes

Because cloud has never been so easy to use

Over the past months I have been blogging about ASP.NET Web API a lot. One question that I haven’t really addressed, is how to send this beast to production – because it’s one thing to develop something, and completely different to have it up and running in live environment.

While so many members of the community have really enjoyed Windows Azure since it has been unveiled in the new shape recently, a lot of people are still uncertain how to work with it – because, well, “cloud” has always sounded a little enterprise-like. I thought it might help people to have a quick step-by-step guide on how you could really easily deploy your app to Azure (using Git!).

More after the jump.

Our appliciation

This is our application that will be deployed to Azure. You can see it in action at http://awesomewebapi.azurewebsites.net. It is a single page application which allows you to add/edit/modify products and its colors. It’s built around Lightspeed O/RM, knockout.js and WebAPI (plus some additional libraries such as nLog, Ninject etc.).

Getting started with Azure

Obviously you won’t go very far without an Azure account. Those guys have a fantastic offer since they offer 3 months of trial for free. Just go to Windows Azure websites and sign up. You’d need a Live ID fo that.

You’d need a feature called “web sites” enabled, and depending on the current amount of requests they have, it might take a few hours to set up. To see if your “web sites” feature is active, after registration go to Account > Preview features, and look up “Web sites” there.

Setting up the site

Once you are signed up, and have the “web sites” feature activated, login to your Azure Management portal.

At the bottom, click on New and let’s roll. Choose create with a database. Surpise, surpise, you need to give your site some name.

Create a database.

Create a new DB server.

Or choose a DB server (if you already used Azure)

Then you’d see Azure creating the website:

You can now navigate to your blank webiste.

Now, go to the database tab, and select your newly created base. On the right, click on view connection strings. Copy the ADO.NET one, as it will be needed for your app.

Migrating the DB

Now, let’s migrate your app’s databse. The easiest to do that is to use SQL Management Studio.

Open your databse using SQL Management Studio, right click on the DB, choose Tasks > Generate scripts.

The default options should be just about fine. If you want to mvoe the data as well, make ure to set Script data to true, under the Table/View Options section.

Once the wizard is done, you will get the SQL mirror of your database dumped into the Query window.

Copy the SQL script and go back to Azure management portal. From your database page, choose manage option. This will send you to the Azure SQL DB management tool. You will need to login using your credentials provided when you created the database server.

If you receive a login error, despite providing valid credentials, you’d need to add your IP tot the whitelist. Go back to the DB page under Azure Portal and do that.

Now you should be able to login successfully.

Choose new query and paste your generated SQL script, and execute. You would probably get the following errors.

That’s because SQL Azure doesn’t support some SQL features. Remove the initial USE statement (since it’s irrelevant anyway), and all ON statements. For example

becomes

Now you should be able to successfully execute the query.

Setting up Git publishing

Next step is to set up git publishing. Go back to Azure management Portal, and navigate to your website. From the right handed menu, choose Set up Git publishing.

Copy the Git repo URL that has just been generated for you and you are ready to deploy your web app using Git. Of course, you’d need Git for that but I assume you have that already.

Open up Git bash and navigate to your website folder. Init the repository and add your remote repository:

Commit your files (btw, remember to change your connection string to the one specific for Azure before doing that).

And push!

Magic! The application pushed via Git will be built and deployed. As soon as you push, the Azure Management Portal will show you the information that the app is being deployed.

If there are any build errors, you will be notified and will have access to build logs.

You can fix your issues, push again

And voila

Your app in the cloud and running.

Summary

Using IaaS (infrastructure as a service) and deploying your application to the cloud has never been as easy as it is right now with Windows Azure. This is truly a game changer for the way we deliver our web applications.

Hopefully this tutorial is helpful to someone. In the next post, we will look in details into creating from scratch the application we just deployed to Azure.

You can see the application in action at http://awesomewebapi.azurewebsites.net.

Be Sociable, Share!