Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MSBuild fails when updating solution to .NET 4.7.2

I'm updating a solution to use .NET 4.7.2 and hit a problem.

I can build and run the project on dev machine just fine, but TeamCity won't.

In TeamCity build step I added /p:TargetFrameworkVersion:v4.7.2 to MSBuild step.

One of the projects target netstandard 2.0 and MSBuild gave me an error that I should add .NET Framework 4.7.2 there. I added .NET Framework 4.7.2 as a target, but that had no effect. Here's an error from the build log:

[NETSdkError] C:\Program Files\dotnet\sdk\2.1.402\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.TargetFrameworkInference.targets(150, 5): error NETSDK1045: The current .NET SDK does not support targeting .NET Standard 4.7.2. Either target .NET Standard 2.0 or lower, or use a version of the .NET SDK that supports .NET Standard 4.7.2.

It's weird since there's no .NET Standard 4.7.2, but I figured it's just a typo in the error message.

MSBuild tools 2017 and .NET 4.7.2 targeting pack are installed on the agent.

Any idea what's wrong here? May be additional MSBuild argument is the problem? Project file for the failed project looks like this:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFrameworks>net472;netstandard2.0</TargetFrameworks>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
    <PackageReference Include="Autofac" Version="4.6.1" />
    <PackageReference Include="System.Configuration.ConfigurationManager" Version="4.4.1" />
    <PackageReference Include="System.Net.Http" Version="4.3.3" />
  </ItemGroup>
  <ItemGroup>
    <Reference Include="SomeLibrary">
      <HintPath>..\packages\SomeLibrary.1.0.4\lib\net461\SomeLibrary.dll</HintPath>
    </Reference>
    <ProjectReference Include="..\ProjectName.Core\ProjectName.Core.csproj" />
    <ProjectReference Include="..\ProjectName.Domain\ProjectName.Domain.csproj" />
    <ProjectReference Include="..\ProjectName.Web\ProjectName.Web.csproj" />
  </ItemGroup>
</Project>

When I try to run MSBuild locally with the same parameters - it fails but with entirely different error, something about illegal syntax and C#7 language features. Building solution via Rider works fine.

like image 202
chester89 Avatar asked Dec 13 '22 12:12

chester89


1 Answers

If you use multi-targeting projects, use /p:TargetFramework=net472 instead.

TargetFrameworkVersion will ony change the version of the framework used, not the TargetFramework value or the TargetFrameworkMoniker that is used elsewhere in the build. So there is no .NET Standard target framework with version 4.7.2 which is causing your build issues.

Do note that setting a global property like this may only work when all the projects in the build support it, so be careful when using it on solutions containing multiple projects that may or may not support it.

like image 163
Martin Ullrich Avatar answered Dec 28 '22 20:12

Martin Ullrich