Author: paulthecyclist

My Application.targets

This post sets out a way to enable TFS to copy and group applications files as well as web project files.


I have a Visual Studio solution with multiple projects. In particular I have a website, a web service (WCF) and a console application. I have TFS running onsite and a build definition associated with my solution. On check in, units tests are run and the build produces build output into a defined drop folder. TFS handily packages the website and web service into their own folders, living inside the _PublishedWebsites folder. The output of the console app is just dumped into the root of the drop folder. Ideally, we want it to be in its own folder and ideally in a folder like _PublishedApps. From here it can easily be picked up by our continuous delivery tooling to be deployed.

Distributed Solution

There are a couple of solutions out there on the web, lurking inside stackoverflow answers and suggesting themselves in other peoples’ blogs.  None answered it as clearly as I want and I’ve since lost the links to them, so this post is here to help me remember how to do it.

This Solution

I suspect there may be other ways of solving this problem. Please do let me know yours in the comments section.The crux of this solution lies in understanding how your web project files get copied to the PublishedWebsites folder in the first place.

The answer lies in the webproject .csproj file. Near the bottom of the file there is a line:

<Import Project=”$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets” Condition=”false” />

It turns out that WebApplication.targets file (comes with Visual Studio install I think) does all the fun stuff.

Find that file on your pc, on mine it’s here:

C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v10.0\WebApplications

At this point I confess my knowledge about msbuild scripts comes largely from google. I understand that it’s copying files needed for the website to a directory; this directory is defined near the top:

<WebProjectOutputDir Condition=”!$(WebProjectOutputDirInsideProject)”>$(OutDir)_PublishedWebsites\$(MSBuildProjectName)</WebProjectOutputDir>

TargetYou need a new targets file         

In order for our console application to copy its files to its own output folder, I wanted a .targets file for applications, not web sites.

There isn’t one out of the box, like there is for websites. So we need to create it. Fortunately there’s a shortcut; I call it Ctrl C, Ctrl V. seriously… make a copy of the WebApplication.targets file. Place it inside your solution file structure and name it something different, like Application.targets, for instance.

Next make two amends:

In the file find, <WebProjectOutputDir Condition=”!$(WebProjectOutputDirInsideProject)”>$(OutDir)_PublishedWebsites\$(MSBuildProjectName)</WebProjectOutputDir>

Change the name of the output folder, to _PublishedApps, for instance:

Find <WebProjectOutputDir Condition=”!$(WebProjectOutputDirInsideProject)”>$(OutDir)_PublishedApps\$(MSBuildProjectName)</WebProjectOutputDir>.

Inside <Target Name=”_CopyWebApplication”>, add: <Copy SourceFiles=”$(OutDir)$(TargetFileName).config” DestinationFolder=”$(WebProjectOutputDir)\bin” SkipUnchangedFiles=”true” />

The first is just a change to the directory name where files should be output too.
The second is a change to help it copy and rename the app.config file.

The final part of the puzzle is to amend your console app project to use the new .targets file.
Open the project file and immediately before the <target name=”beforebuild”> section add:

<Import Project=”..\build\Application.targets” />

Getting the relative path to your targets file correct. Save it all, check it all in (inc. the .targets file) and you should get a new output folder in your drop location, specifically for your console application’s files.

Written in August 2015, using TFS 2013 and Visual Studio 2013.

Image credit: Flood G

For the love of…

This post is inspired from a code review with a colleague, covering the many ways we, as c# developers, can write code to simply call a method three times.


You want to perform an action three times. You could just call the statement three times

Console.WriteLine("Hello Reader");
Console.WriteLine("Hello Reader");
Console.WriteLine("Hello Reader");

Chances are, this may make you twitch a little. It’s not DRY and not very scalable – you might chose to do something like this

public void DoSomething(int count)
    for (int i = 0; i < count; i++)
        Console.WriteLine("Hello Reader");

What’s wrong with this? Nothing as far as the compiler is concerned and it will of course call your method three times (when a 3 is passed into the function). The picky part of me thinks this isn’t good enough though.
In particular, we wanted to call a method three times, so for this simple task why do we have to declare an integer? Why is it called ‘i’? Surely those days belong to the days of a fax machine?

C# is a pretty rich language. We can do better.

Who doesn’t love ForEach?

If I had a collection with three items, I could simply call ForEach, right?

private static void EnumerableRangeForEach(int count)
    foreach (var number in Enumerable.Range(1, count))
        Console.WriteLine("Hello Reader");

Looks a bit better, but I don’t need an ordered collection of integers really, just a collection with (3) things in it. Like this:

private static void EnumerableRepeatForEach(int count)
    foreach (var item in Enumerable.Repeat(typeof(bool), count))
        Console.WriteLine("Hello Reader");

At least our code doesn’t declare an ordered integer array now; now we have a collection of three booleans. However, we still have the annoyance of needing to declare var item to please the for loops gods. We can do away with the named variable (ish) if we add in the additional expense of converting it to a List

  Enumerable.Repeat(typeof(bool), count)
                      .ForEach(item => Console.WriteLine("Hello Reader"));

Everyone loves a one liner. At least the author of it does, reading someone else’s isn’t always such a joy. My main concern is that we had to create a collection, then convert it to a list in order to iterate over it, using List’s .foreach extension.

Please the recursive gods

Many years ago, I had no beard and when not sleeping through lectures at university learnt to program, functionally. The lecturers in their wisdom thought functional programming was the way forwards and once upon a time I was proficient in ML (F# has some of its origins here) and Prolog. Naturally I used all these skills, when I started out as VB programmer after graduating…

I digress. Our desire to do something three times, can be written like.

private static void RecursiveLoop(int loopCount)
    if (loopCount == 0)

    Console.WriteLine("Hello Reader");
    var nextCount = loopCount - 1;


Granted this isn’t the smallest method here. On the plus side, we are not forced to declare an implicit collection and we don’t have any variables that aren’t doing anything explicit. You can of course argue that we are still declaring a variable (nextCount) to keep track of where we are, similar to our var number sacrifice earlier.

If you are unfamiliar with recursive functions, I should point out that failure to declare a base case (if loopCount == 0 in our case) that will always be hit, will result in a StackOverflow. Similarly in some circumstances if you’re are looking at doing thousands of recursive calls and you are using tail recursion, then current limitations with the c# compiler will also result in a stackoverflow. Lots of posts about this on the web, this one ain’t bad:

What’s best

As ever in computing, it depends should be the right answer. If the business case strongly suggests that you only ever need to call something three times and it’s unlikely to change, I can see a case for the non dry example at the top. Very efficient and easy to read and I’ve seen code bases a lot worse in terms of dryness.

More realistically, this number three is likely to be changeable, so you want a collection and the ability to iterate over it. Personally I like the style of the recursive function and have some negative opinions on for (int i = 0; i< upperBound; i++) – just seems so primitive and unimaginative. However, the compiler’s going to reduce them to basically the same thing so as long as your method is clean, easy to read and easy to test, it’s all good.

What’s your preference?


Of course there’s a big except to my last paragraph. Not all for loops are the same when you really need to squeeze your method and get it crunching 1000s and every nano second counts. I’ll cover this in my next post.

Cycling attitudes

It was a dark and stormy night… and there I was cycling into town on an errand. It’s 5.30pm. I’m almost at my destination I signal my intention to go left. I’m turning left (going across the bike lane in the other direction) and then wham, he hits me. I didn’t see him (obviously). Neither did he see me. buckledwheel His impact caught my front wheel with a good deal of force, I don’t believe he even applied his brakes. My front wheel crumpled under the impact. He didn’t strike me with enough force to knock me off my bike, but the effect of coming to a standstill so quickly, combined with the shock of being hit, I lost my balance and came off the bike. Balance in a stationary position is tricky – especially when I have my four year old daughter on the back! The other cyclist also fell off his bike in the collision. No-one was hurt, my front wheel never recovered though. None of us was wearing cycle helmets.

If I were still in London and wrote the last sentence, people would think me very irresponsible to cycle without a helmet and especially to transport my daughter without one. However, I live in the Netherlands now and here, unless you’re on a racing bike, decked head to toe in lycra, you don’t wear a helmet. Cycling, for the non racing types, is more just an efficient way of walking. People cycle to work in suits…

I’m not against cycle helmets and clearly in the game of head vs pavement, I’d rather play it with a helmet on. However the game of head vs pavement should not be thought of as only for cyclists, as in practice this is not the case as @BezTeets tweeted recently:

Ben Goldacre posted this in the British Medical Journal, showing that there are a lot of factors that determine what makes cycling safer. What role the cycle helmet plays is hard to state.

So why don’t the Dutch wear helmets?

It helps that the infrastructure here is fantastic – plenty of segregated (physical curb or similar from road and pedestrian path) cycle paths, often only in one direction is a massive plus. However, there are plenty of roads, where the cycle path is similar to that in UK (painted along the side of the road) and of course many roads with no cycle path at all. It’s what happens here that I find interesting. On the road, cyclists have the right of way. So, I can be cycling down a narrow side street (no cycle lane) with a van coming the opposite direction. Often the van will slow down, potentially even stop to make it easy for me to ride past. If I need to turn left in a (non cycle paved) road, I can go into the middle of the road, signalling my intention and do it. In the UK, this in theory is also true. In practice, cars tend to sound horns, shout abuse at you and drive on the right or left of you as you might be costing them 5 seconds of their journey time.

Cyclists here are generally commuting, as are many UK ones I imagine. Here you do so in ordinary clothes and at such a pace as to not really break a sweat (helped by no hills of course!). In London my experience was mainly also commuting. However, many there interpret the word commuting as racing. Having these cyclists daily trying to improve on their personal best times, in rush hour is not a healthy thing. I’ve had the misfortune to cut some of these cyclists up, vis versa too I hasten to add. Wow, do some of these cyclists get real angry about such a nothing event. So much rage…

Here, along narrow bike paths, you’ll often come across two people cycling side by side chatting. If you want to go faster, ring your bell and they will allow you to pass. Cycling here is safer and more pleasant and slower. Everyone does it, not just men 20-45 in lycra, but women, grandparents and kids. Heck, even some of the 4 year olds in my daughter’s school ride to school (next to mummy/daddy of course), in the bike lanes/on the roads.

My answer

The answer lies somewhere between the Dutch have a good cycling infrastructure and attitudes. This result is cycling is safe and pleasurable here. I started this post by explaining how I was hit by an irresponsible (in my opinion) cyclist who was going too fast (by Dutch standards) based on traffic conditions. Collisions happen. However, if those collisions are not at high speeds and the parties involved are comparable sizes, then chances are serious injuries are much less likely. Think about that time you were walking on the pavement and bumped into someone; it was a nothing event, right? Now if only cyclist collisions could be like that, outside of the Netherlands.