Question

I'm working with MSBuild (Framework version v4.0.30319 - 32 bit one) on Windows 2008 x64. Wanted to understand how MSBuild manages inline tasks. Will it compile once per call to the task ? Or will it compile once and re-use for each call to the task ?

I ran MSBuild with the "/m" argument and tried introducing a deliberate error into the C# code. And MSBuild pointed me to 1 text file (under a temp folder somewhere in my profile folder). No other text file was generated. However, I didn't know how to figure this if there were no errors.

My intent behind trying to figure this out: To know if this will be efficient to the same order as using a compiled dll (instead of an inline task). The miniscule overhead of compiling inline task code is acceptable if the compile happens just once (because I will save on SCM aspects of the code and the binaries).

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="default" ToolsVersion="4.0">
  <Target Name="default">
    <ItemGroup>
      <A Include="1;2;3;4;5;6;7;8;9;10"/>
    </ItemGroup>
    <MSBuild Projects="$(MSBuildProjectFullPath)"
             BuildInParallel="true"
             Targets="Echoer"
             ToolsVersion="4.0"
             StopOnFirstFailure="true"
             Properties="Prop=%(A.Identity)"/>
  </Target>
  <Target Name="Echoer">
    <MyTask WhatToEcho="$(Prop)"/>
  </Target>
  <UsingTask TaskName="MyTask" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
    <ParameterGroup>
      <WhatToEcho ParameterType="System.String" Required="True"/>
    </ParameterGroup>
    <Task>
      <Code Language="cs" Type="Fragment">
        <![CDATA[
        Log.LogMessage("Property received: "+WhatToEcho);
        ]]>
      </Code>
    </Task>
  </UsingTask>
</Project>
Was it helpful?

Solution

Running your example under ProcMon on my machine with MSBuild 4.5 reveals the following:

  1. Temporary assembly is generated by MSBuild once per the build under user %TEMP% folder. (It was actually csc.exe that created the assembly, but I think this is just a side effect on how code generation is done).
  2. The source code was in temporary .cs file, also under %TEMP%.
  3. By the end of the build all files (source files as well as output assemblies) are deleted.

In other words, you will see a perf hit on first call to the task during the build. All subsequent calls to the task will use cached assembly. After the build the cache is lost, the assembly has to be re-created again, which means that if you are after fast incremental build, you might benefit from pre-compiled DLL.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top