Click here to Skip to main content
Click here to Skip to main content

The Right Way to Version Your Assemblies

By , 13 Feb 2012
 

No, I'm not talking about whether you have Major, Minor, Patch, and Build numbers. I'm not talking about when you increment which number. My suggestion is to do whatever works for you in those cases, though I reserve the right to blog about that in the future. What I am talking about is how you can structure your solution and project files in Visual Studio to make updating your assemblies' version number much easier. This blog post makes some assumptions:

  • You are using version numbers in your assemblies
  • You are using a recent version of Visual Studio (I know these steps work with both Visual Studio 2008 and 2010)
  • You have multiple assemblies in one Visual Studio solution
  • You want to update all of your assemblies' version numbers at the same time and to the same version number

1-OriginalSolutionExplorer

Those assumptions cover a vast majority of the projects I've worked on, but I'll admit that there may be projects out there that might not fit into the mold I've set forth. I'm OK with that. This blog post isn't for you if your mold is different. So let's get started. I created a solution file which contains several projects. There's an MVC2 web application project, its associated test project, a business objects project, and a data access layer project. These are all empty projects for this example—only the structure of the solution and projects is important for my purposes. Solution explorer for this project is shown here. The first step is to separate out the version information (and any other information that is common from assembly to assembly if you wish, like company name, product name, and so on) into its own file. Open up one of the AssemblyInfo.cs files to see what I'm talking about.

2-OriginalAssemblyInfo

Notice that the settings are assembly attributes. Some people don't realize that you don't have to put all the assembly attributes in one file. I'm going to take advantage of that fact to accomplish what I set out to do: change the version in one place and have all projects updated at once. The other key is using a solution-level file and linking to is from each project. Visual Studio supports files at the solution level as well as at the project level. You can put anything you want at the solution level and it will be carried along with the solution, though it won't get included in any output unless you write some custom build rules to grab the file and do something with it. On some projects I've included a Word document about the solution and its projects as a solution level file. For this example, we're going to create a new C# source file and include it at the solution level. I'm going to call this new file, "VersionInfo.cs".

  • Step 1: Create a new file and call it "VersionInfo.cs". Do this by choosing "File | New | File…" or press Ctrl-N in Visual Studio. Choose "Visual C# Class" as the file type.
  • 3-CreateVersionInfoCs

  • Step 2: Erase everything in this file. Save it as VersionInfo.cs in the same folder as the solution file.
  • Step 3: Highlight the version information lines from the AssemblyInfo.cs file from earlier, cut it from that file (Ctrl-X) and paste it in VersionInfo.cs (Ctrl-V). Add "using System.Reflection;" to the top of VersionInfo.cs to avoid compilation errors.
  • Step 4: Add the VersionInfo.cs file to the solution by right-clicking on the solution file, choosing "Add | Existing Item…", then selecting "VersionInfo.cs" (find it in the solution's folder where you just saved it), and then click "Add". You should see a new "Solution Items" folder under your solution name with the VersionInfo.cs file in it (see screenshot).
  • Step 5: Add the VersionInfo.cs file to each project as a linked file. See below for instructions on how to do this.
  • Step 6: Remove the version information from the other projects' AssemblyInfo.cs files.

4-SolutionItemsFolder

At this point you should be able to build your solution successfully, and each assembly should have the same version information—the information found in VersionInfo.cs. So what is a "Linked File" and how do I create one? Read on. Right-click on the project and choose "Add | Existing Item…", navigate up one folder level, and click once (don't double-click!) on the VersionInfo.cs file. [This assumes that your solution folder is one level up from your project 5-LinkedFilefolder!] Now instead of clicking "Add", click the little down-arrow next to the "Add" button. Choose "Add As Link". You should now see VersionInfo.cs in your project, but with a "shortcut" icon overlaid on top of the file's icon. Once you have completed this step for each project, you can build your solution without errors. Now to test it out. Build the solution and look in the bin folder (or "folders" if you haven't added the other projects as references in the web project) for the DLLs. Look at the version numbers. They should all read "1.0.0.0". Now change the version number in the VersionInfo.cs file information. Rebuild the solution then check the version numbers. All of the assemblies should now have the new version number. Notice, however, that it is the "File Version" that you see when hovering over the DLL in Windows Explorer, so if you change only the "Assembly Version" the you'll have to go to the DLL's properties to see your change. [I must admit, I don't recall which version number is used when putting DLLs into the Global Assembly Cache. Please leave a comment if you have that information handy!]. So that is the procedure. If you have an automated build system then you can make use of the fact that the version number is in its own file and update it automatically. Then each build will produce DLLs with distinct version numbers. That is a great way to identify the build that a DLL came from if you happen to get a report from the field that there is an error in a specific DLL. But I'm sure that's never happened to you…right?

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

StevenLJackson1
Architect
United States United States
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionLinked FilememberClark Kent12316 Feb '12 - 2:03 
You mentioned in your article
Quote:
So what is a "Linked File" and how do I create one? Read on.

But you never exactly explain what a linked file is. I guess I can infer what a linked file is by the steps of creating one, but I may be wrong. Maybe you can post a link to MSDN about what link file is?
 
Good article by the way. You got my 5. Thumbs Up | :thumbsup:
AnswerRe: Linked FilememberStevenLJackson116 Feb '12 - 2:53 
Good point...
 
A quick search on "Visual Studio Linked File" gave me this link: http://support.microsoft.com/kb/306234[^]. In the Knowledge Base article it hints, too, at what a linked file is:
 
If the file in question is shared between several projects and you want any changes that you make from any project to be reflected in the file, then you should link the file. If not, then you should copy the file so that each project has its own distinct copy.
 
Basically, the file is treated as if it is part of each project that links to it, but there is only one physical copy of the file on your hard drive, and it could be in a different directory than the project which links to it.
 
Does that make sense? Want me to try to explain it some more? I'm glad to help if you still need me to.
GeneralRe: Linked FilememberClark Kent12316 Feb '12 - 3:27 
I appreciate the reply and the informed answer. The reason why I posted my question was not to the affect that I am unable to search MSDN, but rather I noticed an unanswered question in your article.
 
I hope this does not come off the wrong way, but my critique was to mention for those people who wanted to know more about What a Linked File is that you could merely link to an MSDN for those who want to go more in depth at the end of the article.
 
I am not trying to knock you because as I mentioned before I gave you a 5. I enjoyed your article and keep up the good work. Thumbs Up | :thumbsup:
GeneralRe: Linked FilememberStevenLJackson116 Feb '12 - 3:32 
I understand completely! Thanks again!
GeneralMy vote of 5memberJustin Helsley15 Feb '12 - 6:59 
Thanks for this. Started using it immediately
GeneralMy vote of 5memberFMtHtG14 Feb '12 - 2:36 
Pretty simple when you know how Smile | :)
GeneralMy vote of 5memberkosmoh14 Feb '12 - 2:00 
Thanks, I did needed something like that Smile | :)
QuestionMy vote of 5memberDoncp13 Feb '12 - 8:13 
Excellent. Works great with Visual Studio 8.
AnswerRe: My vote of 5memberStevenLJackson116 Feb '12 - 3:03 
Thank you for verifying!
GeneralMy vote of 5 [modified]memberwayne.net13 Feb '12 - 5:12 
Very informative article. I learned a lot of new things. Thank you for taking the time to share with us.

modified 13 Feb '12 - 13:42.

GeneralRe: My vote of 5memberStevenLJackson113 Feb '12 - 6:19 
Thank you very much! You have the distinct honor of giving me my first votes here on The Code Project! Wink | ;-)
GeneralRe: My vote of 5membertkrafael_net14 Feb '12 - 9:13 
There is a way (i dont remember how), you can modify this file (versioninfo.cs) and update the build revision using msbuild scripts (see AssemblyInfoTask in search sites).
BTW, congratulations. Its a good way to ensure the compilation is the same for all assemblies in the solution.
GeneralRe: My vote of 5memberStevenLJackson114 Feb '12 - 9:35 
I intentionally did not include information about how to update the version number(s) in the VersionInfo.cs file. That is definitely build-specific. If you're using MSBuild then there are tasks out there that can do that. Other build/continuous improvement systems work differently. I think there is a similar task for CruiseControl, and I'm sure that other systems have similar procedures for updating the version number. If not built-in then there is most likely an active developer community which has produced a contrib project to solve this problem. After all, it is a common problem that needs solving.
 
Thanks for your feedback!
GeneralRe: My vote of 5memberStevenLJackson116 Feb '12 - 3:02 
I found this link on Jeremy Jameson's blog for incrementing the automatically assembly version if you're using TFS 2008:
 
http://blogs.msdn.com/b/jjameson/archive/2010/03/25/incrementing-the-assembly-version-for-each-build.aspx[^]
 
Here are similar instructions for TFS 2010:
 
http://blogs.msdn.com/b/jjameson/archive/2010/11/29/incrementing-the-assembly-version-for-each-build-in-tfs-2010.aspx[^]
 
Each blog post mentions that there is a custom build task in the MSBuild Community Tasks Project[^] which increments the version number.
 
I haven't tried either approach, so if you decide to try them out, please post back here (or at Jeremy's blog--or better yet, BOTH) and let us know what you find out.
 
Thanks!
QuestionNice article however, seems to be in the incorrect category of c++ / mfc.membercoderforfood13 Feb '12 - 4:26 
See subject above.
AnswerRe: Nice article however, seems to be in the incorrect category of c++ / mfc.memberStevenLJackson113 Feb '12 - 4:45 
Thanks for pointing that out. The Code Project just pulled the article from my blog and made its best guess as to the category. I updated the categories to better reflect the content and audience.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 13 Feb 2012
Article Copyright 2012 by StevenLJackson1
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid