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:

clip_image002

I now have a private Git repo in BitBucket here:

https://bitbucket.org/azurecoder/waws-test-repo

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.

image

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

clip_image005

Once Git Publishing is set up we can add the BitBucket repository we’ve just created per the image below.

clip_image007

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:

clip_image008

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
   2:   
   3:  {
   4:   
   5:  public static Dictionary<int, string> EmployeeList = new Dictionary<int, string>();
   6:   
   7:  // GET api/Employee
   8:   
   9:  public IEnumerable<string> Get()
  10:   
  11:  {
  12:   
  13:  return EmployeeList.Values;
  14:   
  15:  }
  16:   
  17:  // GET api/Employee/5
  18:   
  19:  public string Get(int id)
  20:   
  21:  {
  22:   
  23:  if (!EmployeeList.ContainsKey(id))
  24:   
  25:  {
  26:   
  27:  throw new HttpResponseException(HttpStatusCode.NotFound);
  28:   
  29:  }
  30:   
  31:  return EmployeeList[id];
  32:   
  33:  }
  34:   
  35:  // POST api/Employee?value=
  36:   
  37:  public void Post(string value)
  38:   
  39:  {
  40:   
  41:  if (EmployeeList.ContainsValue(value))
  42:   
  43:  {
  44:   
  45:  var resp = new HttpResponseMessage(HttpStatusCode.Conflict)
  46:   
  47:  {
  48:   
  49:  Content = new StringContent(string.Format("Employee already exists in database")),
  50:   
  51:  ReasonPhrase = "Employee already exists in database"
  52:   
  53:  };
  54:   
  55:  throw new HttpResponseException(resp);
  56:   
  57:  }
  58:   
  59:  int maxCount = EmployeeList.Keys.Count == 0 ? 0 : EmployeeList.Keys.Max();
  60:   
  61:  EmployeeList.Add(maxCount + 1, value);
  62:   
  63:  }
  64:   
  65:  // PUT api/Employee?id=&value=
  66:   
  67:  public void Put(int id, string value)
  68:   
  69:  {
  70:   
  71:  if (!EmployeeList.ContainsKey(id))
  72:   
  73:  {
  74:   
  75:  throw new HttpResponseException(HttpStatusCode.NotFound);
  76:   
  77:  }
  78:   
  79:  EmployeeList[id] = value;
  80:   
  81:  }
  82:   
  83:  // DELETE api/Employee?id=
  84:   
  85:  public void Delete(int id)
  86:   
  87:  {
  88:   
  89:  if (!EmployeeList.ContainsKey(id))
  90:   
  91:  {
  92:   
  93:  throw new HttpResponseException(HttpStatusCode.NotFound);
  94:   
  95:  }
  96:   
  97:  EmployeeList.Remove(id);
  98:   
  99:  }
 100:   
 101:  }
 102:   

clip_image010
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.

  • git init
  • git remote add azure https://bitbucket.org/<bitbucket account name>/<repository name>
  • git add .
  • git commit –m “First checkin”
  • git push azure master

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:

  • git commit –am “another comment”
  • git push azure master

clip_image012

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).

POST http://azurecoderws.azurewebsites.net/api/Employee?value=Bob

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:

clip_image013

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

Useful Links

About the Author

richardMy 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.