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
CREATE TABLE \[dbo].[Product\](
\[Id\] \[uniqueidentifier\] NOT NULL,
\[Name] [nvarchar\](max) NOT NULL,
\[Price\] \[float\] NOT NULL,
\[Quantity\] \[int\] NOT NULL,
\[CreatedOn\] \[datetime\] NOT NULL,
\[UpdatedOn\] \[datetime\] NOT NULL,
PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD\_INDEX = OFF, STATISTICS\_NORECOMPUTE = OFF, IGNORE\_DUP\_KEY = OFF, ALLOW\_ROW\_LOCKS = ON, ALLOW\_PAGE\_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
becomes
CREATE TABLE \[dbo].[Product\](
\[Id\] \[uniqueidentifier\] NOT NULL,
\[Name] [nvarchar\](max) NOT NULL,
\[Price\] \[float\] NOT NULL,
\[Quantity\] \[int\] NOT NULL,
\[CreatedOn\] \[datetime\] NOT NULL,
\[UpdatedOn\] \[datetime\] NOT NULL,
PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD\_INDEX = OFF, STATISTICS\_NORECOMPUTE = OFF, IGNORE\_DUP\_KEY = OFF, ALLOW\_ROW\_LOCKS = ON, ALLOW\_PAGE\_LOCKS = ON)
)
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:
git init
git remote add azure <YOUR\_AZURE\_GIT\_REPO\_ADDRESS>
Commit your files (btw, remember to change your connection string to the one specific for Azure before doing that).
git add .
git commit -m "My first commit"
And push!
git push azure master
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.