Thursday, October 14, 2010

Use a Single Version File For All Projects in a Solution

It’s a good idea for all the assemblies in a single solution to have the same version number, but the default in Visual Studio is for each project to have it’s own AssemblyInfo.cs file, each with it’s own version number. However it’s easy to share a single Version.cs file between all the projects.

First create the Version.cs file as a solution item (right click on the solution, Add, New Item). It should look something like this:

using System.Reflection;

[assembly: AssemblyVersion("2.0.1.442")]
[assembly: AssemblyFileVersion("2.0.1.442")]

version_file_solution_explorer

Now remove those same lines (AssemblyVersion and AssemblyFileVersion) from the AssemblyInfo.cs file in each project in the solution.

The next step to add a link to the Version.cs file to each project. Right click on the project and choose ‘Add New Item’ and then ‘Existing Item’. Browse to the Version.cs file in the solution root directory and add it as a link (click on the little drop down arrow on the ‘Add’ button in the file dialogue):

version_add_link

Do this to all your projects. Now you only have to maintain the version number for the whole solution in one place. Nice!

It’s common practice to integrate version numbering with continuous integration. This usually works by having the CI build process update the version number with the build number before each build. Having the number represented in one place makes that much easier.

One last thing you should always do is provide a way for anyone to discover the version number of a running instance of your application. With an ASP.NET MVC web application, it’s a simple as this little controller action:

public ActionResult Version()
{
    var version = Assembly.GetExecutingAssembly().GetName().Version;
    var versionMessage = string.Format("Suteki Shop. Version {0}.{1}.{2}.{3}", 
        version.Major, version.Minor, version.Build, version.MinorRevision);
    return Content(versionMessage);
}

Now if ever I need to know the version of a running instance I can just type the …/Version URL:

suteki.shop_versionnumber

12 comments:

Octoberclub said...

This does seem to be the best way in VS.Net. For some reason our developers often mix C++ or VB.net and C# in the same solution requiring up to 3 files. If you have a deployment project it also needs updating with a separate file.

Alan Dean said...

FYI: I blogged on a closely related subject a few months back.

http://alandean.blogspot.com/2010/06/consistent-assembly-versioning.html

Mike Hadlow said...

Hi Michael, Yeah, I didn't consider the multi-language solution story here. I guess there's no option but to have multiple version files.

Hi Alan, Thanks for that. A very nice demonstration of how to integrate build number versioning into the build process.

Peter Gfader said...

Hi Mike

You can make it even more simpler.
Remove AssemblyFileVersion

If not given, the AssemblyVersion is used.

.peter.gfader.
http://blog.gfader.com

Mike Hadlow said...

Hi Peter, Great suggestion, thanks for that.

Mad Man Writing said...

Hi Mike... MadManWriting here (aka Robin Harris). Like the Blog - good to see you're still banging out the code... Hope all is well with you and yours.

Mike Hadlow said...

Hi Robin, Great to hear from you, please drop me a line: mikehadlow at yahoo dot com.

Anonymous said...

Very, very helpful! Thank you.

Andy Baker said...

You can do search and replace across the AssemblyInfo files in your solution using this regular expression to remove the entries that you're replacing with the common version.cs

^\[assembly\: Assembly(Company|Product|Copyright|Version|FileVersion)\(".*"\)\]$

David Homer said...

Hi,

Great post I am very keen on standardising version numbers across a project when creating major releases so this is very helpful.

It's nice because you can modify the "Assembly Information" properties within Visual Studio and the version numbers are just left blank. If a developer mistakenly enters a version number it fails to build with a duplicate version error.

Very nice.


Thanks,


Dave
http://centrel-solutions.com

Anonymous said...

I have a website project and added a assemblyinfo.cs file to the appcode. When i deployed the files, the page level dlls were all 0.0.0.0

Unknown said...

Thanks for posting this, it's exactly what I was looking for.