This recipe checks the <TargetFrameworkVersion> of a Visual Studio 2008 project file. It fails the build if the version does not match the allowed version.

This is useful if your destination environment is .NET 2.0 only and you want to prevent people from building solutions that target .NET 3.0 and 3.5.


Add to TFSBuild.proj



    <!-- TFS Build 2008.
         This snippet checks the <TargetFrameworkVersion> of a Visual Studio 2008 project file.
         It fails the build if the version does not match the allowed version.

         This should be used when you want to use VS2008, but ensure that your binaries remain as .NET 2.0

        <!-- The TargetFrameworkVersion that you want to match. e.g. v2.0, v3.0 or v3.5 -->

      <!-- Override the Team Build AfterGet target, before any compilation starts -->
      <Target Name="AfterGet" >
        <CallTarget Targets="CheckTargetFrameworkVersions" />

      <UsingTask AssemblyFile="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.dll" TaskName="MSBuild.Community.Tasks.Xml.XmlQuery" />

      <!-- Only run if the AllowedTragetFrameworkVersion property is set -->
      <Target Name="CheckTargetFrameworkVersions" Condition=" '$(AllowedTargetFrameworkVersion)' != '' ">
        <Message Text="Allowed Target Framework Version is: $(AllowedTargetFrameworkVersion)" Importance="high" />

        <!-- Get all *.csproj files in the working directory.
        Ideally this should look at the current build configuration
        and return the filenames of the projects to be built.
        But as Team Build passes SolutionToBuild to MSBuild, we can't get
        the list of projects easily.
        <CreateItem Include="$(SolutionRoot)\**\*.csproj">
          <Output TaskParameter="Include" ItemName="ProjectFiles" />

        <!-- Extract the TargetFrameworkVersion element from each of the project files -->
          <Output TaskParameter="Values" ItemName="TargetFrameworkVersions" />

        <Message Text="Found Target Framework Version: %(TargetFrameworkVersions._value)" Importance="low" />

        <!-- Throw an error if any of the target versions don't match -->
        <Error Condition="'%(TargetFrameworkVersions._value)' != '$(AllowedTargetFrameworkVersion)'"
               Text="Target Framework Version is not allowed: %(TargetFrameworkVersions._value). Allowed version: $(AllowedTargetFrameworkVersion)" />



  • The build will fail if any of the projects within the build don’t match the <AllowedTargetFrameworkVersion> that has been set.
  • You will need at least the v1.2.0.306 MSBuild Community Tasks installed/available.
  • Currently it matches all *.csproj files recursively, regardless of whether they are in the build configuration or not.
  • If you want to actually enforce this, make sure that developers can’t change the file. It might be useful to store it on the server and use an <Include>.

Last edited Nov 2, 2010 at 6:18 PM by wbarthol, version 2


No comments yet.