Prevent MSBuild Execution While Loading a C# Project

MSBuild is a XML-based build system “scripting language” and is the basis of all C# project files, which means, that for an IDE (e.g. Visual Studio or JetBrains Rider) to “understand” your C# project structure, build options, and properties, it has to parse and process all the *.csproj, *.targets, and *.props XML files. The build system is flexible enough for anyone to insert their own build steps, which is great, but can also cause issues, as your build steps can end up being executed when loading the project, slowing down solution loads and reloads.

If you have build steps, which should only ever get executed during the actual “build” phase, you can add the following condition to your build target or anything that implements the Condition property:

<Target Name="BeforeCompile" Condition="'$(BuildingProject)' == 'true'"/>

While your IDE is loading the project, the BuildingProject property is not set to true and thus in this case the whole target is skipped. When you then build the project, the property is set to true and the target is executed as well.

Additional tip: In order to spot potential slowdowns of MSBuild steps, you can generate MSBuild Log files and analyze them with the MSBuild Log Viewer.

References

Leave a Comment

Your email address will not be published. Required fields are marked *

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.