Having intelligible version numbers is one of the easiest ways for developers to keep track of their software in the wild. However, having to maintain version numbers manually across multiple projects can be an annoying, error-prone process, especially if you’re trying to build and release often.
So how can we automate this task away?
The Goal
I have a Visual Studio solution with multiple projects, that therefore generates multiple assemblies for my app. In no particular order, here’s what I want:
- Every assembly has the exact same version number
- The option to manually specify a version number (say for infrequent builds that I release to the general public)
- The option for VS to auto-generate a build number (say for frequent builds that are released to beta testers)
So, what are my options?
Sharing one version number
Let’s tackle the first requirement. By default, every code project in a Visual Studio solution has its own AssemblyInfo.cs, and each specifies their own version number that needs to be updated and maintained.
But there’s no reason we have to stick with that. A better idea is to:
- Remove the AssemblyVersion attribute from AssemblyInfo.cs in all of your projects
- Create a new file, say SharedInfo.cs in one of your projects (typically the one the builds first) and put the AssemblyVersion attribute there.
- For all of your other projects, simply add that SharedInfo.cs as a link. (In the “Add Existing Item…” dialog, click the arrow next to the “Add” button and choose “Add As Link”.)
Now you only have one file to update your version information in, making it much more manageable. Bonus, you can move all common assembly tags you want in your SharedInfo.cs, like Copyright, Product, etc.
Automatic versions
Being able to manually specify a version number is easy enough. Whether you keep the default Visual Studio AssemblyInfo.cs paradigm or the SharedInfo.cs method above, it’s just a matter of you picking and setting a new version number when you feel like it.
But now let’s see what we can do about having automatic versions.
Option 1: Use the built-in wildcards
The first option is given to us right in the default AssemblyInfo.cs, letting us know that we can simply set our version to Major.Minor.* and let Visual Studio auto-generate the Build and Release numbers.
Visual Studio then sets the Build based on the number of days that have passed since January 1st, 2000, and sets the Release to the number of two-second intervals that have elapsed since midnight. Sounds good so far, and of course you can always switch back to a manual version at any time. But there are a couple of caveats:
- This only works with AssemblyVersion, not AssemblyFileVersion. To make this work for both, you have to specify just the AssemblyVersion (comment out or delete the AssemblyFileVersion line) and then Visual Studio is smart enough to use the same value for AssemblyFileVersion.
- Both numbers are generated at the exact time the particular project was built. So if you have multiple projects in your solution and any one takes more than two seconds to build, you will end up with different versions for different assemblies.
Option 2: Use an extension
The next option, one I used for years, is to simply hand over the responsibility of generating versions to the VS extension Automatic Versions.
If features a very nice GUI and there are lots of styles of automatic versions you can specify for your projects. It resolves both of the issues with using the built-in wildcards, letting you specify AssemblyVersion and AssemblyFileVersion, and also making sure that every project has the same version number (whether you share a linked file or not).
Downsides?
- You might not find a version style that you like. While there are lot of version number patterns to choose from, if you have a specific format you need to adhere to (say to match existing releases), you might be out of luck.
- You’ve just added a dependency to your code. Are you sharing your code with other developers? Now they have to install the extension too.
Adding dependencies means adding risk. For over a year, the VS Performance Profiler simply would not work for me, throwing an error and crashing whenever I tried to analyze my applications. I thought maybe my VS 2013 install was simply borked, but the problem followed me to VS 2015.
Turns out Automatic Versions was the culprit. Now, to be fair, once I reported the bug the developer very quickly issued a fix and now the Profiler is fine. But I searched for a solution to that Profiler error for a year, and at no point did it occur to me that an extension might be causing the problem.
Option 3: Use a T4 text template
The last option I want to talk about, is using a T4 template. What are T4 templates? From Code Generation and T4 Text Templates on MSDN:
In Visual Studio, a T4 text template is a mixture of text blocks and control logic that can generate a text file.
I’ve you’ve ever made a website in ASP.NET or PHP, then you’ll have no problem with T4 text templates. Basically, you can create a template file with some in-line blocks of C# that will get run when you build your project to generate your actual file.
So, instead of creating a straight SharedInfo.cs like we did before, now we create a SharedInfo.tt and write a little bit of code to handle generating new version numbers.
Simply add a new T4 text template to your project paste in the following to get started:
<#@ template debug="false" hostspecific="false" language="C#" #> <#@ output extension=".cs" #> <# int major = 1; int minor = 0; int build = 0; int revision = 0; // TODO: Write code here to automatically generate a version string version = String.Format("{0}.{1}.{2}.{3}", major, minor, build, revision); #> // This code was generated by a tool. Any changes made manually will be lost // the next time this code is regenerated. using System.Reflection; [assembly: AssemblyVersion("<#= version #>")] [assembly: AssemblyFileVersion("<#= version #>")]
Now, if you’re having trouble reading this, basically, the first couple lines are directives that the template is going to create a .cs file. The code in the <# #> blocks will be run when the project is built, and where you’re going to need to decide how you want to generate your build numbers. After that is the static template text of the output file, and you can see where you see <#= version #>, that’s where the value of the version string will be inserted.
As it stands, if you created a SharedInfo.tt with the above template, you’ll get a SharedInfo.cs with the following:
// This code was generated by a tool. Any changes made manually will be lost // the next time this code is regenerated. using System.Reflection; [assembly: AssemblyVersion("1.0.0.0")] [assembly: AssemblyFileVersion("1.0.0.0")]
And just like before, you can then add links to that SharedInfo.cs (not the .tt file!) into your other projects. So all you need to do is write the code that generates the automatic versions. What does that code look like? It’s up to you!
In my projects, I usually have a “manual” mode whereby the version numbers I entered are used for public production builds, and overwritten by something based on DateTime.UtcNow for private or test builds. If you want plain, auto-incrementing numbers, you might go with reading in your current SharedInfo.cs to parse out the previous version number, increment, and save it back out.
Well, there you have it, completely customizable automatic versioning in Visual Studio solutions. Like it? Have a different approach? Sound off in the comments!
/jon
P.S. If you want to see a “real-world” example of how I pick version numbers check out What the Chordious version numbers mean.
The “sharing one version number” paragraph was all I needed. So simple. So brilliant. #facepalm
Option 5: MSBump
https://github.com/BalassaMarton/MSBump
How to auto-increment version number of assembly in .NET
Try this solution : http://techblogger.info/blog/auto-increment-version-number-assembly-net/
with video
Hmm this leads to a bad site in Chinese or some asian language.
how get that version..
i want version to placed in View
Jon, Do you have a way to trigger the generate the t4 file on build?
I don’t, because generally I only want to bump the version when I want to release a build, to avoid unnecessary updates to the generated cs files with every git commit. However you could add a pre-build step that calls TextTransform.exe (with your t4 file) directly.
Don’t forget to set:
false
in project files (*.csproj)
otherwise you get compile errors!
Set XML Tag Deterministic to false.
Wordpress has deleted my XML-Tags, above…