Keep track of all the latest news and events on developer tools and technologies you care about
By Richard Conway
June saw the release of Windows Azure Websites which enable you to get up and running in testing your Web API code at no or little cost. Websites come in 3 flavours: Free, Shared and Reserved. For the purposes of this post we’ll stick with the free version which is the default.
The set of steps in this article will allow you to set up a continuous deployment environment to a private repository for a Web API solution.
In order to start you will need a valid Windows Azure subscription. You can sign up for a 90-day free trial at http://www.windowsazure.com. You will also need either a TFS, Github or Bitbucket account. TFS and Bitbucket accounts (for small numbers of user and projects) are free for private repositories but Github is a paid service. You can get a free Bitbucket account at http://bitbucket.org.
In Bitbucket you can create a new private repo like so using Git as opposed to Mercurial:
I now have a private Git repo in BitBucket here:
We can then navigate to https://manage.windowsazure.com and create a new Web Site from the quick new and create buttons on the bottom app bar.
The new website should take about a minute to create and you should something similar to the above image.
We can then setup Git Publishing using the right hand dashboard menu on our newly created Website
Once Git Publishing is set up we can add the BitBucket repository we’ve just created per the image below.
The first step here is to associate your BitBucket account with the new Windows Azure Website.
Once this is done we should get a repository list like so:
For now we can create a simple MVC4 Web API project to test out our continuous deployment process.
In order to test the continuous deployment simply create a new solution in Visual Studio selecting an ASP.NET MVC4 project. Add a Web API controller called EmployeeController and copy the following code.
1: public class EmployeeController : ApiController
5: public static Dictionary<int, string> EmployeeList = new Dictionary<int, string>();
7: // GET api/Employee
9: public IEnumerable<string> Get()
13: return EmployeeList.Values;
17: // GET api/Employee/5
19: public string Get(int id)
23: if (!EmployeeList.ContainsKey(id))
27: throw new HttpResponseException(HttpStatusCode.NotFound);
31: return EmployeeList[id];
35: // POST api/Employee?value=
37: public void Post(string value)
41: if (EmployeeList.ContainsValue(value))
45: var resp = new HttpResponseMessage(HttpStatusCode.Conflict)
49: Content = new StringContent(string.Format("Employee already exists in database")),
51: ReasonPhrase = "Employee already exists in database"
55: throw new HttpResponseException(resp);
59: int maxCount = EmployeeList.Keys.Count == 0 ? 0 : EmployeeList.Keys.Max();
61: EmployeeList.Add(maxCount + 1, value);
65: // PUT api/Employee?id=&value=
67: public void Put(int id, string value)
71: if (!EmployeeList.ContainsKey(id))
75: throw new HttpResponseException(HttpStatusCode.NotFound);
79: EmployeeList[id] = value;
83: // DELETE api/Employee?id=
85: public void Delete(int id)
89: if (!EmployeeList.ContainsKey(id))
93: throw new HttpResponseException(HttpStatusCode.NotFound);
As you can see this is a fairly simple test that shows CRUD operations on an underlying Dictionary<>.
You can download a Git bash from http://git-scm.com/download
Open the bash shell and change directory to your solution or project directory. Enter the following set of commands.
At this point you’ll need to enter your BitBucket username and password. And that’s it. You can check to make sure that your source is in BitBucket through the Bitbucket website. Any additional changes you make to the code can be updated via:
Every time we check in changes the deployment in Windows Azure Websites (WAWS) is updated. You can use .gitignore files because every time a change is pushed WAWS will attempt to rebuild your solution so no binaries are needed in Bitbucket. Any of the deployments can be rolled back to earlier versions through the Windows Azure Management Portal.
Lastly we can check that the output on the WAWS is correct through Fiddler. To add an employee name we can enter the following (replace with your own WAWS name).
We should have a 204 No Content message returned since we didn’t alter the default Web API behaviour.
Issuing a GET http://azurecoderws.azurewebsites.net/api/Employee will return a list of employees as below:
There you go! Whilst this example is simple and a little contrived it proves the power of Continuous Deployment in Windows Azure.
If you want to see more come and see a live demo of the above and many other amazing things you can do with Windows Azure at the UK Windows Azure Users Group meeting on Friday 7th December @ 6 pm. Register at http://ukwaug.net
My name is Richard Conway. You can follow me @azurecoder where I post mainly azure related content. I also blog at http://azurecoder.azurewebsites.net. I’m a Microsoft MVP in Windows Azure and a Windows Azure Insider. I’m also the co-founder of the UK Windows Azure Users Group which hosts monthly meetings and mini-conferences. In a professional capacity I work and co-founded a consultancy and product firm called Elastacloud where we undertake projects in Windows Azure, HPC and Hadoop (HDInsight). I’ve spent 16 years programming and have studied degrees in Sussex and the OU. I’ve written 8 books, loads of articles and spoken at various conferences around the world.