Pre-build MSBuild task to update AssemblyInfo not in sync with built exe
Asked Answered
N

4

22

I am using a pre-build task in Visual Studio 2008 that invokes msbuild:

C:\WINDOWS\Microsoft.NET\Framework\v3.5\MSBuild.exe $(MSBuildProjectDirectory)\version.targets /p:Configuration=$(ConfigurationName)

Inside version.targets, I am updating the AssemblyInfo.cs file to replace version information:

   <FileUpdate
        Encoding="ASCII"
        Files="$(MSBuildProjectDirectory)\Properties\AssemblyInfo.cs"
        Regex="AssemblyInformationalVersion\(&quot;.*&quot;\)\]" 
        ReplacementText="AssemblyInformationalVersion(&quot;Product $(ConfigurationString) ($(buildDate))&quot;)]"
    />

When I build the project through Visual Studio 2008, it builds without any problems.

But when I look at the resulting exe's version information, it contains the previous time stamp even though the AssemblyInfo.cs has been changed with the "correct" one.

It seems that the pre-build's changes aren't seen by the main compilation task and it's always one behind.

Any ideas of what I'm doing wrong?

Nightcap answered 12/2, 2009 at 14:40 Comment(1)
I'm not crazy: social.msdn.microsoft.com/Forums/en-US/msbuild/thread/…Nightcap
D
25

I don't think you are doing anything wrong - it's a bug.

I have reported it here - check if you can reproduce it and add a validation, maybe we can get it fixed by MS.

EDIT: I tried the suggestion by "Si" to update the file in the "BeforeBuild" event - however I still get the same wrong result with Visual Studio 2008/SP1.

UPDATE/WORKAROUND: MS has responded to the bug report. As a workaround you can add

<UseHostCompilerIfAvailable>FALSE</UseHostCompilerIfAvailable>

to your csproj file.

Doctorate answered 14/3, 2009 at 16:11 Comment(2)
+1 because we use auto-generated code very heavily and after switching from VS2005 to VS2008(SP1) we suffered very heavily from this bug (which actually does not seem to be a bug but by design...poor design!). So your answer helps very much.Bordelaise
+1 solved my problem. Don't forget to put it in the top <PropertyGroup>.Deration
K
14

+1 for the <UseHostCompilerIfAvailable>FALSE</UseHostCompilerIfAvailable> trick, although neither the accepted solution nor the linked article specified that the line should be added to the first <PropertyGroup> element in the .csproj file.

It is also a problem with Visual Studio only, as invoking msbuild from the command-line on the same .csproj file (without the trick) will see the generated code files compiled right away (and not the previous versions).

Also, I would like to recommend that this topic be tagged with the following, as I had to dig a lot to find it:

  1. VisualStudio
  2. compiler
  3. BeforeBuild
  4. generated
  5. .NET
  6. c#
Kaycekaycee answered 12/2, 2009 at 14:40 Comment(0)
G
3

Interesting, I wrote my own custom task which is hooked into BeforeBuild and this is working fine. Never had a problem from VS or MSBuild via command line. So I would say explore BeforeBuild some more...

I know because we use our build server (CruiseControl.NET) build number as the "Build" part of the version, which is built into all assemblies (we share the same AssemblyInfo.cs for AssemblyVersion and AssemblyFileVersion across assemblies in the solution) and this is then pushed (via FileUpdate task) to a variable in our WiX project and also used to label the MSI file name.

<Target Name="BeforeBuild">
  <CallTarget Targets="UpdateAssemblyInfo" />
</Target>

<Target Name="UpdateAssemblyInfo" Condition="'$(CIBuildNumber)' != ''">
  <UpdateVersion Attribute="AssemblyFileVersion"
    AssemblyInfo=".\Properties\AssemblyInfo.cs" 
    BuildNumber="$(CIBuildNumber)" />
</Target>

If you do some googling you should find other examples...sorry I can't give you the UpdateVersion code, i'd need permission from my work, but if you can't find anything suitable on the net, custom tasks are easy to write and the above should help.

Gospel answered 14/3, 2009 at 16:41 Comment(2)
What version/SP of Visual Studio are you using?Doctorate
We started on VS 2005 and .NET 2.0 and are now currently using VS 2008 and .NET 3.5 (with latest hotfixes and SP's)Gospel
B
0

Use something like below to change any content for any file. Here i am changing Build date value in hallo.aspx when every time i create a build.


e.g. Hallo.aspx content

<BuildDate> 12-23.2011 </BuildDate>


<!-- regular expression to get value between html node: "[^<>]+(?=[<])" --> 
  <FileUpdate Files="$(AboutDir)Hallo.aspx" 
    Regex="Builddate[^&lt;&gt;]+(?=[&lt;])" ignoreCase="true" 
    ReplacementText="BuildDate: $(Day)-$(Month)-$(Year)" /> 

Backscratcher answered 6/9, 2011 at 8:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.