Click here to Skip to main content
12,452,344 members (56,732 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

10.8K views
Posted

MSBuild: Reliably Retrieving the Assembly Version (without locking the file!)

, 28 Jun 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
I’ve recently had to look at dynamically assigning an assembly versions based in part on the date of compilation and the version of a compiled 3rd Party library. This looked like a simple job for the MSBuild.ExtensionPack.Framework.Assembly task in the MSBuild Extension Pack.

I’ve recently had to look at dynamically assigning an assembly versions based in part on the date of compilation and the version of a compiled 3rd Party library.

This looked like a simple job for the MSBuild.ExtensionPack.Framework.Assembly task in the MSBuild Extension Pack.  However, this had a serious downside it locked the Assembly for the duration of the build!. 

Erkk.

This is the Devil Line that causes the problem:

   1: protected override void InternalExecute()
   2: {; 
   3:     // Implementation ommitted
   4:     this.assembly = Assembly.LoadFrom(this.NetAssembly.GetMetadata("FullPath"));; 
   5:     // Implementation ommitted
   6: }; 
   7: /* Taken From MSBuild.ExtensionPack.Framework.Assembly using Reflector
   8: protected override void InternalExecute();; 
   9:  
   10: Declaring Type: MSBuild.ExtensionPack.Framework.Assembly
   11: Assembly: MSBuild.ExtensionPack, Version=3.5.0.0 
   12: */;

It looks innocent enough, but it loads the assembly into the current AppDomain (MSBuild’s build process) which then prevents any other process from accessing the file – even the build process itself. 

Fail.

The Workaround

To get around this, I knocked up a quick custom MSBuild Task:

   1: using System;
   2: using System.Linq;; 
   3: using System.Reflection;
   4: using Microsoft.Build.Framework;; 
   5: using Microsoft.Build.Utilities;
   6:    
   7: namespace MartinOnDotNet.MSBuild.Tasks
   8: {; 
   9:     /// <span class="code-SummaryComment"><summary>
</span>

The main different between my implementation and the MSBuild Extension Pack task is this line:

1: AssemblyName assname = AssemblyName.GetAssemblyName(NetAssembly.GetMetadata("FullPath"));;

This line retrieves the assemblies name object (version, public key, etc, etc) without actually loading the assembly into the AppDomain, hence – no locking!  Sweet.

This can now be referenced in your build scripts, like so:

 1: <!-- Register Task -->
 2: <UsingTask AssemblyFile="..\3rd Party\MartinOnDotNet Build Tasks\MartinOnDotNet.MSBuild.Tasks.dll";
 3:                      TaskName="MartinOnDotNet.MSBuild.Tasks.AbbreviateVersionTask" />
 4:
 5: <!-- And Now Use in Build Target -->
 6: <Target Name="MergeVersions">;
 7:     <!-- Implementation Ommitted -->
 8:
 9:     <MartinOnDotNet.MSBuild.Tasks.AbbreviateVersionTask NetAssembly="$(HelpersLibrary)">
10:         <Output TaskParameter="AssemblyVersion";
11:                         PropertyName="HelpersAssemblyVersion" />
12:         <Output TaskParameter="Major";
13:                         PropertyName="HelpersMajor" />
14:         <Output TaskParameter="Major";
15:                         PropertyName="HelpersMajor" />
16:         <Output TaskParameter="Minor";
17:                         PropertyName="HelpersMinor" />
18:         <Output TaskParameter="Build";
19:                         PropertyName="HelpersBuild" />
20:         <Output TaskParameter="Revision";
21:                         PropertyName="HelpersRevision" />
22:     </MartinOnDotNet.MSBuild.Tasks.AbbreviateVersionTask>;
23:     <Message Text="Helpers Version: $(HelpersAssemblyVersion)"
24:                      Importance="high" />;
25:     <ItemGroup>
26:         <AssemblyInfoFiles Include="..\Freestyle.Helpers.Ektron\Properties\AssemblyInfo.cs"/>;
27:     </ItemGroup>
28:
29:     <Message Text="Assembly Info File: %(AssemblyInfoFiles.FullPath)"
30:                      Importance="high" />;
31:     <MSBuild.ExtensionPack.Framework.AssemblyInfo AssemblyInfoFiles="@(AssemblyInfoFiles)"
32:                      AssemblyFileMajorVersion="$(HelpersMajor)";
33:                      AssemblyFileMinorVersion="$(AbbreviatedVersion)"
34:                      AssemblyFileBuildNumberType="NoIncrement";
35:                      AssemblyFileBuildNumber="$(HelpersBuild)"
36:                      AssemblyFileRevisionType="NoIncrement";
37:                      AssemblyFileRevision="$(HelpersRevision)"
38:                      AssemblyMajorVersion="$(HelpersMajor)";
39:                      AssemblyMinorVersion="$(AbbreviatedVersion)"
40:                      AssemblyBuildNumberType="NoIncrement";
41:                      AssemblyBuildNumber="$(HelpersBuild)"
42:                      AssemblyRevisionType="NoIncrement";
43:                      AssemblyRevision="$(HelpersRevision)"
44:                      SkipVersioning="false"/>;
45:
46: </target>;

The above example makes use the AssemblyInfo task in the MSBuild Extension Pack to explicitly set the version number for my assembly.

License

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

Share

About the Author

Martin Jarvis
Software Developer (Senior) Freestyle Interactive Ltd
United Kingdom United Kingdom
I'm a lead developer for Freestyle Interactive Ltd where we create many wonderful websites built on Microsofts ASP.Net and Ektron CMS.

I've been developing .Net applications (both Windows and Web) since 2002.

You may also be interested in...

Pro
Pro

Comments and Discussions

 
GeneralThe problem has been fixed in the later versions Pin
Martin Jarvis28-Jun-10 10:42
memberMartin Jarvis28-Jun-10 10:42 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160826.1 | Last Updated 28 Jun 2010
Article Copyright 2010 by Martin Jarvis
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid