title: Versioning .NET builds
body: <p>Just wondering what's the best approach to versioning of .NET builds?</p>
  
  <p>I use:</p>
  
  <ul>
  <li>TFS 2013 for version control</li>
  <li>TFS gated check-ins</li>
  <li>Wix 3.8 to package code to MSI files</li>
  </ul>
  
  <p>I want to set version of:</p>
  
  <ul>
  <li>assemblies (AssemblyInfo.cs, or a shared one referenced in all projects)</li>
  <li>MSI packages (in Wix code)</li>
  <li>documentation (for example inside readme.txt file in the final output of the build)</li>
  <li>etc</li>
  </ul>
  
  <p>Ideal version number would allow tracing installed software back to the exact source code.<br>
  Something like:</p>
  
  <pre><code>&lt;Major&gt;.&lt;Minor&gt;.&lt;TFS_changeset_number&gt;
  </code></pre>
  
  <p>First 2 parts of the version I want to store in some simple text \ XML file in version control near the solution, as I believe they should live together. Developers will update this file manually (for example following Semantic Versioning approach). Each build will read this version file, get 3d part of the version from the calling CI tool, and update all the necessary files with the version.</p>
  
  <p><strong>What's the best way to implement this?</strong></p>
  
  <p>I've used a few approaches in the past:</p>
  
  <p>1) NAnt \ MsBuild wrapper that does this version work, then calls MsBuild for the solution. It could be called from CI tool (Jenkins \ TeamCity \ etc).</p>
  
  <p><em>Problem</em> - integration with TFS gated check-in is ugly as I build solution twice.</p>
  
  <p>2) customize TFS build process template</p>
  
  <p><em>Problem</em> - it's not that simple, and causes some merge work on TFS upgrades. Also changeset number doesn't exist yet in gated check-ins, so we can only use the previous changeset id.</p>
  
  <p>3) A separate MsBuild project in solution, which does only this versioning task, and is configured to run first in Project Build Order of the VS solution.</p>
  
  <p><em>Problem</em> - need to reference this meta-project in all other projects (including all future ones) which feel ugly</p>
  
  <p>I know different MsBuild and TFS extension packs that can simplify updates. This topic is not about which one is the best. The question is more methodological than technical.</p>
  
  <p>I also think that it would be ideal if Microsoft include something for versioning in their standard TFS build template. Other CI tools already have this functionality (AssemblyInfo patcher).</p>
  
  <p><br><br><br>
  <strong>UPDATE 11/09/2014</strong></p>
  
  <p>I've decided to clearly express the <strong>Versioning Principles</strong> that will conform to the best practices of Agile \ Continuous Delivery:</p>
  
  <p>1) Ability to reproduce any historic build</p>
  
  <p>2) As a consequence of 1) and according to CD principles everything (source code, tests, app configs, env configs, build\package\deploy scripts, etc) is stored under version control and so has a version assigned to it</p>
  
  <p>3) Version number is stored tightly together with the source code it applies to</p>
  
  <p>4) People are able to update version according to their business\marketing logics</p>
  
  <p>5) There is only 1 master copy of the version, which is used in all parts of automated build\packaging process</p>
  
  <p>6) You can easily say which Version of the software is currently installed on the target system</p>
  
  <p>7) Version of the installed software must unambiguously identify the source code that was used to build it</p>
  
  <p>8) It's very simple to compare versions to say which is lower and which is higher - to control which upgrade\downgrade scenarios are allowed and implementation specifics of them</p>
  
  <p><br><br><br>
  <strong>UPDATE 15/09/2014</strong></p>
  
  <p>See my own answer <a href="https://stackoverflow.com/a/25806365/902415">below</a>.<br>
  I was lucky to find the solution that meets all my requirements!</p>