Will the real programmer please stand up?

Over the last year or two, I’ve been reading articles that describe this strange world where software developers are Gods. They are paid so much money that it’s ridiculous. They get flown across the US and invited to parties with naked girls in spa pools and all the booze in the world.

Hipster

Image sourced from http://www.quora.com/Brogramming/How-does-a-programmer-become-a-brogrammer

Ok. I feel like I’m missing something here. I’m a software developer. I’ve been doing this for the past 10 years or so. I’ve worked in NZ, Australia, London and now Amsterdam, just to mix things up. I’ve even worked primarily in the e-commerce space. So why on earth am I not living this crazy extravagant lifestyle?! Is it because I don’t live in the US? Waaaaahh!!

Then I read this article and the penny dropped.

Who are the “fat guys who know C++”, or as someone else put it, “the guys with neckbeards, who keep Google’s servers running”?

What?? What do you mean who are they? Isn’t this a perfect description of a software developer? If it isn’t, then what is??

The more I read, the more this felt like this article was describing me, right down to the “games nights”. And that’s when I got it – the aforementioned articles weren’t talking about the people that I know as software developers. They were talking about this new breed of ‘hipster’ developers. They are talking about the cool front-end scripters, the JavaScript, PHP, Rails and Ruby developers. They aren’t talking about the back-end developers that code in one of those archaic programming languages like C, C++, C# or Java. Which happens to include me and most of the software developers I know. Bummer, I guess that’s why I still haven’t been invited to any of these crazy parties.

Until this point, I’ve been living in a world where I thought the developers that apparently belong to this ‘secret Guild of nomadic craftsmen’ were the real developers, the ones that people refer to when they mention software developers. Apparently I’m wrong and I’m now a part of something that not everyone is even aware exists! To most people, developers are actually cool hipsters that have a great fashion sense and certainly aren’t spock-like.

When did this shift in definitions happen?

If you liked this post, share it!
Facebook Twitter Linkedin Delicious Digg Stumbleupon Email
Read More

Macro Photography – Take 1

For Christmas, my awesome partner Jason got me a Tamron SP AF 90mm f/2.8 Di Macro 1:1 lens for my Nikon D90. I really enjoy taking close up photos of insects and flowers and although the kit lens I already had does a pretty good job at it, a macro lens can do much better. I haven’t really learnt how to use it very well yet but here is my first attempt – a few photos I took at the Changi Airport ‘Butterfly Garden’ in Singapore.

Apart from re-sizing these photos, I haven’t edited them at all.

Butterfly
Butterfly
Butterfly
Butterfly

All imagery is copyright Annie Luxton 2007-2012. Images may not be reproduced without prior permission.

If you liked this post, share it!
Facebook Twitter Linkedin Delicious Digg Stumbleupon Email
Read More

SQL Server Performance Tuning

Things in the “software development” world have changed a fair bit over the last 10 years. In the past, developers used to have to think carefully about the SQL they wrote, making sure that they used well optimized stored procedures on well designed databases with the right indexes on the right columns because let’s face it, the database was often a bottleneck.

Nowadays, with tools like ORMs being used more and more liberally, I get the feeling that some of us are getting a bit lazy and forgetting that the database is still a potential bottleneck. I bet that a lot of developers reading this won’t have had to hand-roll any SQL for a while and instead rely on what the ORM of choice generates for them. Mature frameworks like NHibernate and Entity Framework do a great job at providing us with CRUD statements but if we have a complex domain, a lot of rows, a heavy load or unique requirements then perhaps they aren’t good enough.

Although I’m by no means a DBA or SQL / SQL Server expert, I think it’s important for developers to take some responsibility over data retrieval when writing applications. To get the most out of your application and hardware, you should be aware of which queries are run most often and which are taking the longest to complete. In a perfect world, your DBAs could be providing you with this information on a weekly basis but there are some tools that you as a developer can use to preempt performance problems as well. In this post I’m going to list out some of the tools that I use to help me performance tune and monitor SQL Server, the DBMS that I am most familiar with.

Use stored procedures over executing SQL statements

In previous versions of SQL Server (version 6.5 and earlier), one of the main advantages of using stored procedures over executing SQL statements was that SQL Server would partially precompile a single execution plan for stored procedures upon their creation and cache it for reuse. However, the last couple of versions of SQL Server no longer precompile execution plans for stored procedures upon creation but instead upon their first execution, like is done for any other SQL statement. Also, execution plans for all T-SQL statements (stored procedures and SQL statements) are now stored in the procedure cache, not just stored procedure execution plans. These changes reduce the overall performance benefit that used to be gained by using stored procedures.

There are still some other performance gains to be made by using stored procedures though. For example, when you call a stored procedure from code, you only have to send the EXECUTE stored_procedure_name arguments down the wire instead of the whole statement. You can also perform a lot more logic within one stored procedure thereby saving you many round trips between the client and the server.

There are also other non-performance-related benefits to using stored procedures over SQL statements such as maintainability, abstraction, security.

Execution Plans

Before releasing a new or updated stored procedure, it’s a good idea to have a look at the planned and actual execution plans for it. If you’re making a change, you should look at the execution plan before and after your change to make sure you haven’t made anything worse! The main things to look for here are:

  • Table or clustered index scans. If the table over which the scan is being performed is small, this isn’t generally a problem. If it is a large table then it may mean that your table is missing indexes, or you need additional indexes, or that the optimizer has ignored your index for some reason. If you really need to (and you know what you’re doing!), you could use query hints to force SQL Server to use a particular index.
  • Repetitive scans. Try to avoid any repetitive table or index scans by rewriting your queries. There is usually more than one way of writing any batch of SQL statements.
  • Highest cost queries. Concentrate on optimizing the queries with the highest relative cost.

Traces – SQL Profiler vs Server Side

Traces record what’s going on in your database and are invaluable when it comes to analyzing what’s going on under the covers, especially when there is a performance issue.

SQL Profiler is a great GUI for running traces and analyzing how your code is actually communicating with your database(s). Even if the queries themselves are well optimized and running smoothly, if you’re repeatedly calling them hundreds of times in a row then you may encounter some performance issues. However, as a developer, you should only ever SQL Profiler to run traces on development and test databases. Running a trace on a production database from the SQL Profiler GUI can seriously overload it. You should also make sure you’re capturing the information you need and not too much more otherwise it becomes impossible to find what you’re looking for in all the trace output. I generally set mine up like this:

SQL Server Profiler

According to a very talented SQL Server DBA ex-colleague and friend of mine, @trademe_dave you can also run traces on the server side which apparently have much less impact than running them through the SQL Profiler GUI, but it really all depends on how many events you trace (3 or 4 should do the trick) and how long you leave it running for. Safest option is to leave this sort of thing up to the DBAs :)

Dynamic Management Views

If you can’t get your hands on a trace, you can try using Dynamic Management Views to show you some execution statistics (including CPU time, physical and logical reads, etc) on all queries. From what I understand, it isn’t perfect – for example, it only shows you aggregated data on queries that have actually finished executing, but that’s still much better than nothing! Here is a great article on how to use sys.dm_exec_query_stats to show you the top (most often run, longest execution time) queries.

Analyze Statistical Information

If you execute the statement SET STATISTICS IO ON before running a query or executing a stored proc (in SQL Server Management Studio), SQL Server will output some information regarding the amount of disk activity generated by the SQL statements being executed. To turn it off again, you simply execute SET STATISTICS IO OFF. These statements affect the existing connection so if you don’t execute a SET STATISTICS IO OFF command, any other queries you run using the same connection will also produce this performance tuning data.

The syntax looks a little like this:

1
2
3
4
5
SET STATISTICS IO ON
 
SELECT * FROM dbo.Articles
 
SET STATISTICS IO OFF

The information it outputs regarding the disk activity can be found in the ‘messages’ table, right next to the ‘results’ tab in mgmt studio:

SET STATISTICS IO

What you’re looking for in particular are the number of scan counts and logical reads. For a simple query or stored proc that selects data out of only one table, you’ll generally find a scan count of 1. However, if you’re joining tables then you’ll see that some of the tables involved might have much larger scan counts. I don’t know off the top of my head what a good value for ‘scan counts’ is but your DBA should be able to help with that. More importantly, while you fine-tune your query / stored proc you should keep an eye out on this value to see if it goes up or down with the changes you make – the higher the number, the worse the performance.

With respect to the logical reads, they are a measure of the number of pages that SQL Server had to read the data cache from in order to produce the results specified by your query. Again, a higher number is worse than a lower number so while you are tuning your query / stored proc, you should make sure that you don’t end up with a higher number of logical reads than you initially had!

I’ve also read that it’s a good idea to issue the following two commands before executing your query / stored proc with SET STATISTICS IO ON / OFF around it so that you clear out SQL Server’s data and procedure caches:

1
2
DBCC DROPCLEANBUFFERS
DBCC FREEPROCCACHE

This article has a lot more detail on SET STATISTICS IO ON / OFF (and SET STATISTICS TIME ON / OFF which I personally haven’t used). And here is an MSDN article on SET STATISTICS IO, just for good measure.

That’s all for now folks!

I’m sure there are many other tools that developers can and frequently do use to performance tune their SQL queries and stored procedures. Those listed above are just the ones I use often – if you know of any others please feel free to leave a comment below!

But please, developers, don’t forget that under all your awesome code there is still a database struggling to keep up with the load you’re throwing at it. Be smart about how you access your data and don’t stop analyzing, benchmarking and optimizing your SQL statements.

If you liked this post, share it!
Facebook Twitter Linkedin Delicious Digg Stumbleupon Email
Read More

How asynchronous is SmtpClient.SendAsync?

In a previous role, I was tasked with writing a newsletter email sender. How hard can this be, I thought to myself, and set off to complete my mission.

Initial Thoughts

We were probably going to be sending tens of thousands of emails at a time so although I figured I’d need to use some threading, I thought I’d start by using the asynchronous version of the SmtpClient.Send method, SmtpClient.SendAsync instead of the blocking SmtpClient.Send. I figured that way I’d be able to send batches of emails asynchronously and that way we’d be able to get through all the emails super fast.

The Test

I wrote some basic prototype code using SmtpClient.SendAsync and ran a test to send a couple hundred emails in it. Although my test ran without errors, I pretty quickly discovered that I wasn’t receiving all the emails that I was apparently sending by calling SmtpClient.SendAsync! At first I wondered if I’d somehow overflowed my inbox or something but that didn’t seem to be the problem. Then I started doing a bit more research and discovered something which explained the behaviour I was seeing…

The Findings

From the ‘remarks’ section of the MSDN documentation on SmtpClient.SendAsync:

After calling SendAsync, you must wait for the e-mail transmission to complete before attempting to send another e-mail message using Send or SendAsync.

And…

To receive notification when the e-mail has been sent or the operation has been canceled, add an event handler to the SendCompleted event.

OH! So in order to successfully send multiple emails asynchronously, I must add an event handler to the SmtpClient.SendCompleted event and wait until the first SmtpClient.SendAsync has completed before triggering the next one. Hmm… this does not seem all that asynchronous to me! I realize that there must be good reasons why it was implemented this way but in theory, using SmtpClient.SendAsync to send multiple emails really isn’t all that much more asynchronous than lining up a bunch of synchronous calls to SmtpClient.Send.

Conclusion

I guess it all comes down to your interpretation of what ‘asynchronous’ means – in this case, SmtpClient.SendAsync is indeed asynchronous in that it allows the program to carry on executing without blocking. This is great in most cases, unless what you want to do next is send another email.

So to sum up, it seems the only way to send multiple emails at the same time using .NET’s SmtpClient is to use threading after all. Spawn up a few worker threads with a separate instance of the SmtpClient in each and just send the emails using SmtpClient.Send.

If you liked this post, share it!
Facebook Twitter Linkedin Delicious Digg Stumbleupon Email
Read More

ASP.NET Response.Redirect

In ASP.NET web applications, the Response.Redirect(string url) method is often used to control the flow of an application (both user browsing and logic) by redirecting the client to a different URL. For example, on a page that requires an authentication user, it is quite standard to first check if the user is logged in and if they aren’t, call Response.Redirect(“login.aspx”). This is all great, except that many developers probably don’t know what’s actually going on under the covers.

Under the covers

Internally, Response.Redirect (and Server.Transfer for that matter) calls Response.End to end the page’s execution (rather abruptly) and then shifts the execution to the Application_EndRequest event in the application’s Global.asax. We rely on this behaviour to bail out of wherever we were when we decided to call Response.Redirect. What most people probably don’t realize is that Response.End raises a ThreadAbortException exception. Exceptions are expensive, and if you’re calling Response.Redirect often, you might find this particular behaviour detrimental to the performance of your site.

So, how do I avoid these exceptions?

Well, Response.Redirect actually has an overload that takes two parameters – Response.Redirect(string url, bool endResponse). If you use this overload instead and pass false to the endResponse parameter, the internal call to Response.End will be suppressed and as such, no ThreadAbortException exceptions will be raised.

Great! But wait, there’s more…

There is a side effect! Using this overload and passing false so that Response.End doesn’t get called means that any code after the call to Response.Redirect will now be executed where it wouldn’t have previously. This makes perfect sense – if Response.End isn’t called then the execution isn’t shifted to the Application_EndRequest event and if the execution isn’t shifted, then it will carry on executing wherever it currently is. That’s right – although you no longer get ThreadAbortException exceptions being thrown, Response.Redirect no longer controls the flow of your logic.

This is fine if you are calling Response.Redirect at the end of a method at the bottom of your call stack because there was probably no more code to be executed anyway. On the other hand, if you are calling this from inside a method that isn’t at the bottom of your call stack, you have to be careful with the remaining stack calls. Even if you call return straight after you call Response.Redirect, where are you ‘returning’ to? Another method that executes more code? Eeek!

In conclusion

None of this is news – it’s all explained in this Microsoft Support article. However, I really don’t feel that it emphasizes enough the negative effects that switching over to the Response.Redirect overload may have on your application. You simply can’t go through your application and replace every Response.Redirect with it’s overload without thinking carefully about what this might do to the logic of your application. The ‘flow’ may still look like it’s working as it should because the user will still be redirected to another page, but under the covers you might find that you’re now executing code that you weren’t before and that may have some pretty nasty unwanted effects.

If you liked this post, share it!
Facebook Twitter Linkedin Delicious Digg Stumbleupon Email
Read More

Localization and skinning in ASP.NET MVC 3 web applications

Working in Europe has introduced me to the world of localization and skinning web applications. Not only do we have to support several different locales for the same web application, but in some cases, depending on the host url, the site should be skinned completely differently. A common reason for needing to do this is if your e-commerce website also sells products under the guise of a different company in certain locales. Obviously, skinning sites involves a lot of css styling, but you may also need to provide different values for titles, labels, urls, etc, for each skin. You really don’t want to have to build and deploy several copies of your website to do this, and luckily, you don’t have to!

Localization in .NET using resource files

In .NET, localization is generally done via resource files. For each language / locale you wish to support, you should have a SiteA.resx file, e.g.

  • SiteA.resx (This is the fallback resource file, in case no other more specific resource files match the current culture)
  • SiteA.es.resx (Spanish)
  • SiteA.de.resx (German)
  • SiteA.nl.resx (Dutch)
  • SiteA.nl-BE.resx (Belgian Dutch)

If this is all you need, then .NET makes localization pretty straight forward. When you build your solution, .NET automatically creates a strongly-typed resource class with the same name as your resource files (without the extensions). In my example above, .NET would have created a class called SiteA in the Resources namespace, with public readonly string properties for each key in my resources files.

When you access one of the properties in this resource class, as long as you have set your current culture correctly, .NET will take care of picking exactly which resource file to read the values from. How it picks which resource file to use is done via a fallback mechanism which is handled by the convention-based naming. Scott Hanselman provides a pretty good explanation of how this works here.

Localizing skinned sites using resource files

If you also want to use resource files to provide different localized values for each of your skinned sites, you’ll need to create a set of resource files for each skin, e.g. SiteB.resx and SiteC.resx:

  • SiteA.resx
  • SiteA.es.resx
  • SiteA.de.resx
  • SiteB.resx
  • SiteB.es.resx
  • SiteB.de.resx
  • SiteC.resx

From these resource files, .NET creates three separate strongly-typed resource classes for you to use:

Auto generated resource classes

However, you cannot edit these classes manually, they do not inherit from a common base class nor do they implement a common interface, so how do you elegantly switch between skins without having some massive switch statement everywhere you need to read a value from the resource files?

As with any programming problem, I’m sure there are many ways to do. I’m going to explain how I solved this particular problem in an ASP.NET MVC 3 web application.

ActionFilterAttributes and a LocalizedResourceManager

First of all, I needed something to intercept every action in my application and figure out which culture to use. In my application, I allow the user to change the language of the site by setting a ‘lang’ parameter in the querystring. If that parameter isn’t set, then I look in the user’s browser languages for the main supported language. If you wanted to, you could also read and write a user’s chosen culture from a cookie, but I didn’t need to implement this. I also needed to figure out which skin to display based on the host url that the user requested.

I was able to do this by sub-classing the ActionFilterAttribute class and overriding it’s OnActionExecuting method:

ActionFilterAttribute.cs

You’ll see that on line 18 of the LocalizationAttribute, I resolve the site that the user has requested. I do this by reading the host url. Once I know which site the user has requested, I initialize my LocalizedResourceManager by setting it’s CurrentSiteName property.

LocalizedResourceManager.cs

The LocalizedResourceManager is very simple – it only exposes one method, GetResource(string), to which you pass the name of the resource key that you wish you read out of your resource files. Internally, this method calls HttpContext.GetGlobalResourceObject(string, string) to actually read the property out of the resource files. This method here is really the crux of my solution.

HttpContext.GetGlobalResourceObject(string, string) takes two string parameters: className and resourceKey. In essence, this method allows you to pick which resource class to use and then uses the normal fallback mechanism to figure out which localized resource file to actually read the value from based on which culture is currently set.

All that’s left to do now is to register the LocalizationAttribute as a global filter in the Application_Start() method of Global.asax.cs. I also provide a new route which takes a lang parameter so that the user can change which language they wish to see the site in.

Global.asax.cs

And finally, to make use of this setup, all I have to do is call the static LocalizedResourceManager.GetResource(string) method from within my controllers or views, passing it the resource key name for which I would like to get a localized (both locale and skin-wise) value. By this time, the ActionFilterAttribute will have done it’s magic by setting a current culture and the LocalizedResourceManager.CurrentSiteName property, thereby telling the LocalizedResourceManager which resource class to use and letting .NET use it’s fallback mechanism to pick the right localized resource file as per the current culture.

Index.cshtml

If you liked this post, share it!
Facebook Twitter Linkedin Delicious Digg Stumbleupon Email
Read More

My Running Log

I’ve been a member of several gyms over the years but in all honesty, I’ve never been very good at actually going to them. I start off pretty good but after a few weeks I lose interest and like most people, my gym gear sits in the corner of my room gathering dust. Excuse after excuse, the end result is that I just don’t go often enough.

Anyway, in October 2011, Jason and I joined a gym literally around the corner from our flat in Amsterdam. It’s more expensive than other gyms around the city but considering it’s about a 2 minute walk for us to get there and it’s really well equipped, we figured it was worth it. At least this way we wouldn’t really be able to use the ‘Oh but it’s cold and the gym is so far away, let’s just stay at home watch TV instead..’ excuse.

So far we’ve been pretty good – going to the gym between 2 and 3 times a week, most weeks. Every time I go to the gym, I try to run on a treadmill for at least 20 minutes and I also do some weights. We did go on holiday to Santiago, Chile for 2 weeks during November but we managed to get a couple of gym sessions in while we were there and I even took part in a 5km race with my dad and brother!

Running

SantiaGo Run Skechers, 2011 – 5km – http://www.santiagorun.cl/

In an effort to force myself to keep up this good habit, I’ve also halfheartedly agreed to run a half marathon sometime late 2012. This gives me a goal – to be able to run 20kms (without passing out). In order to track my progress, I’ve been keeping a note of my running. Every time I’ve run at the gym, I’ve noted down how long I’ve run for (minutes), the distance (kms) and from that I’ve calculated my average pace (minutes/km). I thought it might be cool to graph these numbers and watch my progress over the months.

Anyway, I’ll keep updating these graphs as I go. Perhaps having these numbers out there in the wild will encourage me to keep getting fitter, stronger and faster!


Running Log - Time (minutes)
Running Log - Distance (kms)
Running Log - Average Pace (minutes/km)
[Last updated on 9/5/2012]
If you liked this post, share it!
Facebook Twitter Linkedin Delicious Digg Stumbleupon Email
Read More

What I learnt at University

I had a great time at uni. I spent 4 years working my ass off trying to do as well as I could in my sometimes-impossibly-hard Comp Sci and Maths courses. Sure, I also met a lot of fun people, did a lot of fun stuff, held down a part-time Comp Sci tutor job and all that other stuff, but I studied really hard too. Most of my papers had weekly assignments hard enough to make most people want to quit and a lot of my papers weren’t worth many credits. I don’t know, sometimes it felt like my courses were harder and more work than some of my other fellow students.

Great, but was it relevant?

After being a Software Engineer in several different roles in the IT industry for 8 years now, I have come to the conclusion that I’ve never used (and probably never will) most of what I learnt at University. Academia is like that – you learn crazy hard stuff for the sake of learning. I’m not complaining though – I really enjoyed all the theory I learnt, I’m just not sure how useful / relevant it is in today’s typical software development roles.

Anyway, I’ve still got a few of my assignments from one of the hardest papers I did – COMP473 – Formal Aspects of Concurrent Systems. I’m not even sure they’re still running this paper at Victoria University of Wellington anymore, but here they are. I seriously haven’t done anything like this in any of my roles in the last 8 years:

Sadly, nowadays, I’m not sure I understand a word of what my answers actually meant! I can’t help but think that I’m getting dumber… but then I realize that most of the work I do these days isn’t rocket science and I’m just out of practice at solving complex problems. Perhaps I should take a new University course?

If you liked this post, share it!
Facebook Twitter Linkedin Delicious Digg Stumbleupon Email
Read More

Software deployment processes

During my career as a Software Engineer, I have seen a variety of different approaches to ‘deployments’. This is one aspect of the software development life cycle that wasn’t touched on at all during my 4 year Computer Science degree and seems to be a point of contention for a lot of software companies.

In my experience, a lot companies seem to almost fear deployments, leading to unmanageable 6-week deployment cycles, middle-of-the-night deployments that take hours and inevitably result in unplanned outages, a lot of error-prone manual configuration, unnecessary friction between developers and sys admins, no rollbackability, no version history, etc etc.

One of those days...

Image sourced from http://sgale.blogspot.com

Deployments shouldn’t be that hard

Really, they shouldn’t. If you have a well-planned, stable and automated deployment process, you should be able to deploy early and often and hopefully without any glitches. Your customers will be happier and you won’t end up looking like the poor cat in the picture above.

Below is a list of what I consider to be the most important qualities of a good deployment strategy (not necessarily in order of priority):

  • Manageability
    Deployments really shouldn’t be as complicated and scary as a lot of companies make them out to be. It should be a one-button action and it shouldn’t require taking the whole system offline for hours in the middle of the night by the whole sys admin team. The number of steps involved in deploying may vary depending on your system and how major the deployment is, but they should be easy enough to follow.
  • Configurability
    It should be fairly easy to deploy any of your projects – from static files to binaries, web applications that need IIS to be recycled and even windows services that need to be uninstalled, installed then restarted.
  • Visibility
    Developers, sys admins and anyone else in the company who wants to know should be to quickly and easily be able to see a history of all deployments, what the last version deployed was, what the changes were, who worked on it and when exactly it went live.
  • Rollbackability
    For those odd occasions when a production release doesn’t go quite as expected, it should be relatively easy to rollback to a previous version. I say relatively easy because it really depends on how major the deployment was and how complicated your software is.

My deployment system

In one of my previous roles, I was asked to come up with a process for deploying the various components that made up the software we were building. The components that needed to be deployed (sometimes together, sometimes independently) were .NET web services, ASP.NET MVC web apps, windows services and static files. What I ended up building was a deployment process that involved Teamcity, MSBuild scripts, custom MSBuild tasks, a .NET ‘deployment web service’ and last but not least, a page on our intranet to show version history and comments.

Since building this process, I’ve been asked to describe it several times so I thought to myself, why not blog about it?!

Requirements:
I built this system for one company, with their requirements in mind. It may not suit everyone’s deployment needs. For example, my deployment system did not deploy SQL scripts as this was not a requirement. Any scripts / tables that needed to be created or altered / migration scripts would be run manually before deploying the code. This became a part of the greater deployment process, but it was not handled automatically. And finally, I only built the code aspect of this system and wired it all up through Teamcity – our sys admins set up all required VPN tunnels, FTP servers, etc etc…

Process:
For every code solution, we had an MSBuild script that Teamcity would use to build and publish the binaries / static files on dedicated build agents. Apart from giving Teamcity instructions on how to build the solutions, these MSBuild scripts would also zip up the binaries and static files using the version number as the filename and then copy the zip files to a build server. The sys admins set up an FTP server pointing to all these zip files so that the servers we deployed to had access to them.

The next step was to set up new Teamcity build tasks that used different MSBuild scripts to actually deploy the zipped up binaries and static files. We had one MSBuild task per solution and environment. These MSBuild scripts used custom MSBuild tasks to call the deployment web service on a particular server / environment. In cases where a particular solution was load balanced, we would pass the host names of each server hosting the solution to the custom MSBuild task which would then spin up a bunch of threads and call the deployment web service on each of those servers.

Deployment Process

The Deployment Web Service – the brains of the system:
The ‘deployment web service’ was the brains of the system – the rest of the stuff I just described simply wired everything up. The first step in setting up a new server was always to deploy and configure the deployment web service. Once it was installed and configured properly, this is more or less what the deployment web service would do when it was called:

  • Download the appropriate zip file containing the built solution from the FTP server onto the server you are deploying to.
  • Unzip the zip file into a temporary folder.
  • Modify the config files – depending on the environment being deployed to, the deployment web service would pick the right config file, delete all the others and rename the remaining one to Web.config (or App.config…).
  • If the solution being deployed was a windows service, the deployment service would now stop and uninstall it if it were already installed and running.
  • Copy all the files from the temporary directory over the top of the actual binaries using robocopy.
  • If the solution being deployed was a website or a web service, the deployment service would now recycle the app pool that the site was running under.
  • If the solution being deployed was a windows service, the deployment service would now install it.
  • Delete all temporary files.

Simple, right? Seriously though, that was the brains of the system and it really didn’t do anything out-of-this-world. Deploying binaries and/or files really isn’t that hard!

But what about environment-specific configuration?

This is a much debated point and I’m not claiming that the way we dealt with environment-specific configuration is the only way to go, but it was certainly simple and enabled us to have automated stress-free deployments.

For every application we built, we would maintain separate config files for each environment. That’s right – we had a Web.config (for localhost), Web.Development.config, Web.Test.config, Web.Staging.config and Web.Production.config. I know some of you reading this will be rolling your eyes right about now and thinking… “So every time I need to add a new key to my config file, I have to add it to 5 different files?!? What a nightmare!!”. Trust me, it really isn’t that hard to keep all these files in sync and seriously, it’s much easier than trying to manually merge config files at the last minute before deploying. Think about it this way – once a system is up and running, you don’t add keys anywhere near as often as you have to deploy (x number of environments you’re deploying to).

So how did the right config file get deployed to each environment? Well, all these environment-specific config files would be included as part of the project to be deployed. Each instance of the deployment web service knew which environment it was sitting in and deploying to (this was configured via it’s own web.config) so as I described above, the deployment web service would pick the correct config file based on it’s filename, delete all the others and rename the correct one to Web.config (or App.config). Easy peasy.

Benefits

Having used several different techniques for deploying code through environments and out to production over the years, this system certainly demonstrated various benefits and in particular, satisfied all of the qualities held by a good deployment process which I listed above.

  • Manageability
    Since this system was triggered from Teamcity, it really couldn’t get any easier to manage deployments. Deployments became a matter of pushing one button and waiting for a couple of minutes to see the result. There was no manual merging of config files, no remoting onto the server to restart an app pool. Just one button click.
  • Configurability
    In terms of set up, although this system is made up of several components, it was fairly easy to configure a new project to be deployed. Sure, there were a few steps involved but they were all simple and more or less a copy-and-paste of an existing project’s deployment scripts. It was also pretty straight-forward to set up a new server to deploy to. This involved setting up the deployment web service on the new server and updating any MSBuild scripts with the hostname of the new server for projects that you want to deploy to it.
    Because it was highly configurable, the system was able to deploy several components at the same time, to various different servers.
  • Visibility
    Because we used Teamcity to trigger the deployments, we had all the whole build log that Teamcity produces. I put some logging into the custom MSBuild tasks so that we could see what was going on from within Teamcity’s build logs. Later on, I added some code to update a version databases so that deployments were visible from within our intranet.
  • Rollbackability
    Rolling back to a previous version was a matter of looking up which version number you wish to roll back to and setting this version number in the parameters passed to the deployment MSBuild script by Teamcity. The custom MSBuild tasks and deployment web service would then pick up that particular version and re-deploy it.

Final thoughts

In over a year of daily deployments of several different web sites, web services, windows services and static files, my deployment system never failed. Having this system in place enabled us to focus on writing cool software and getting it out often and early instead of planning stressful deployments. Everyone benefited from this system – developers and testers could easily and quickly deploy to dev, test and staging as often as they wanted, without waiting for sys admins to be free, and production deployments were no harder. Sys admins didn’t need to know anything about how to deploy, managers were able to see when a new version had been deployed to a particular environment and all the automation meant that there was very little room for human error. A definite win for everyone!

If your team doesn’t have an automated deployment system already in place, I implore you to either build your own (like we did) or use an off-the-shelf one. It may take a bit of effort to set it all up at the beginning, but it’s definitely worth it in the long run.

If you liked this post, share it!
Facebook Twitter Linkedin Delicious Digg Stumbleupon Email
Read More

Deploying ASP.NET MVC 3 to IIS 6

I recently built a new ASP.NET MVC 3 (.NET 4) web application which unfortunately has be hosted on Windows Server 2003 running IIS 6. Guess I’d figured getting it running on IIS 6 would be as easy as installing .NET 4 and ASP.NET MVC 3 on the server… but it wasn’t. This isn’t a step by step tutorial on how to get this working, but more of a list of things to look out for and some links to helpful material which all helped getting my site up and running.

Installations

  • Install the .NET 4 Framework.
  • Install ASP.NET MVC 3. Apparently you can avoid installing ASP.NET MVC 3 on the server and just deploy all the appropriate MVC assemblies in the bin folder of your app instead, but I couldn’t get this to work.

IIS Configuration

  • Make sure ASP.NET v4.0.30319 is ‘Allowed’ under Web Service Extensions in IIS – it is ‘Prohibited’ by default.
  • Create a new app pool for your web site to use – IIS 6 app pools cannot run sites under different frameworks so make sure this app pool is only used for your new site.

IIS Website Configuration

Right click on your new website and select ‘Properties’:
  • Under the ‘ASP.NET’ tab, make sure that the ASP.NET version is set to ‘4.0.30319’.
  • Under the ‘Home Directory’ tab, make sure that ‘Execute permissions’ is set to ‘Scripts only’ (at least).
  • Click on the ‘Configuration’ button in the ‘Home Directory’ tab and make sure that all the Application extensions are mapped to .NET 4 versions of the dlls.
  • Add a ‘Wildcard application mapping’ – click on ‘Insert’ and enter ‘C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll’ as the ‘Executable’. Leave ‘Verify that file exists’ unchecked – checking this made my routes stop working.

Permissions

If you’re running under an account with limited permissions, you may need to grant it ‘List Folder Contents’ and ‘Read’ permissions on the Windows Temp directory. I needed to do this because I’m calling out to web services and .NET creates temporary classes in the Windows Temp directory with which to serialize objects from XML. Not sure if this is needed if you’re not doing any serializations, but worth mentioning.

Conclusion

I kept getting 403 and 404 errors when I first deployed my new ASP.NET MVC 3 (.NET 4) web application to IIS 6, but after doing / checking all of the above, it all seems to be working smoothly.

And last but not least: if you’re allowed to, try recycling your application pool and resetting IIS if some changes don’t seem to be making much difference.

Useful links

http://haacked.com/archive/2010/12/22/asp-net-mvc-3-extensionless-urls-on-iis-6.aspx
http://johan.driessen.se/posts/getting-an-asp.net-4-application-to-work-on-iis6
http://blog.stevensanderson.com/2008/07/04/options-for-deploying-aspnet-mvc-to-iis-6/

If you liked this post, share it!
Facebook Twitter Linkedin Delicious Digg Stumbleupon Email
Read More