Ashish Jain

ASP.Net, WCF, jQuery

Sharing assembly version across projects in a solution

In the world of Visual Studio programming, we all must be dealing with multiple projects in a solution. Multiple projects in a solution has become a business need in today’s programming world. We have moved towards multi-tiered architecture where each tier is a project in itself.

So a solution where we have multiple projects, we also have to maintain assembly versions to track the current version in production. Maintaining assembly versions for all projects becomes a pain when we have to manually modify the version for each and every project in the solution. A developer may miss modifying the assembly version of a project which may result in inconsistency and confusion with the version of product or project in production. The scenario may not occur where there are specific teams for deployment, but is a common issue with teams with small sizes where a developer has to play multiple roles starting from analysis, development, testing and deployment.

To eradicate possibility of such errors, in visual studio we have a concept of linked files. The concept of linked files can be applied practically to “Shared Assembly Info”. A Shared assembly info file is a normal file same as our AssemblyInfo.cs or AssemblyInfo.vb file and contains attributes that will be shared across projects in a particular solution.

In order to implement the Shared Assembly Info functionality, create a normal cs or vb file in your solution and name it as SharedAssemblyInfo.cs or SharedAssemblyInfo.vb in your solution folder. Now add this file as a link file in all the projects in that solution. In order to add this as a linked file, follow the steps below:

  1. Right click on the project, in which you wish to add the Shared assembly file, and select Add -> Existing Item...
  2. Select the file “SharedAssemblyInfo.cs” from the solution folder.
  3. Instead of Add, click on the the arrow next to Add and click “Add as Link”
  4. Drag down the added linked file alongside AssemblyInfo.cs in the same folder.
  5. Repeat steps 1 – 4 for all projects for which you wish to add shared assembly file.

After adding links to shared assembly info, your solution explorer would look like

Solution_Explorer

You are now ready to modify the SharedAssemblyInfo.cs file to add attributes common across the solution. I would prefer having my SharedAssemblyInfo.cs file as

   1: using System.Reflection;
   2: using System.Runtime.CompilerServices;
   3: using System.Runtime.InteropServices;
   4:  
   5: // General Information about an assembly is controlled through the following 
   6: // set of attributes. Change these attribute values to modify the information
   7: // associated with an assembly.
   8: [assembly: AssemblyCompany("ABC Corporation")]
   9: [assembly: AssemblyCopyright("Copyright © ABC Corporation 2010")]
  10: [assembly: AssemblyTrademark("")]
  11: [assembly: AssemblyCulture("")]
  12:  
  13: // Setting ComVisible to false makes the types in this assembly not visible 
  14: // to COM components.  If you need to access a type in this assembly from 
  15: // COM, set the ComVisible attribute to true on that type.
  16: [assembly: ComVisible(true)]
  17:  
  18: // Version information for an assembly consists of the following four values:
  19: //
  20: //      Major Version
  21: //      Minor Version 
  22: //      Build Number
  23: //      Revision
  24: //
  25: // You can specify all the values or you can default the Build and Revision Numbers 
  26: // by using the '*' as shown below:
  27: // [assembly: AssemblyVersion("1.0.*")]
  28: [assembly: AssemblyVersion("2.1.0.0")]
  29: [assembly: AssemblyFileVersion("2.1.0.0")]

My AssemblyInfo.cs file for a project would now look something like:

   1: using System.Reflection;
   2: using System.Runtime.CompilerServices;
   3: using System.Runtime.InteropServices;
   4:  
   5: // General Information about an assembly is controlled through the following 
   6: // set of attributes. Change these attribute values to modify the information
   7: // associated with an assembly.
   8: [assembly: AssemblyTitle("BusinessObjects")]
   9: [assembly: AssemblyDescription("This is my business objects assembly for business rules")]
  10: [assembly: AssemblyConfiguration("")]
  11: [assembly: AssemblyProduct("BusinessObjects")]
  12:  
  13: // The following GUID is for the ID of the typelib if this project is exposed to COM
  14: [assembly: Guid("08223d3a-16cc-4f3f-8ea0-adfe5fe7aa7f")]

In order to maintain same assembly version across projects, we now just need to modify the SharedAssemblyInfo.cs file and the version would be reflected across projects.

Hope this helps.

Happy Programming!!!

Comments

Anna23Wilson said:

Great stuff imho. Keep posting this way!

Anna 23Wilson

# October 1, 2011 12:01 PM

Marry23Wilson said:

Wonderful article to my mind. Keep writing this way!

Marry 23Wilson

# October 1, 2011 1:44 PM

AnnaCamson said:

Nice post imho. Keep it up!

Anna Camson

# October 1, 2011 5:21 PM