.Net class library compilation error on build machine

492 views Asked by At

I have a SDK styled .Net class library which compiles properly on my local machine, however fails on build server.

The contents of .csproj are

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFrameworks>net35;net40;netstandard1.0</TargetFrameworks>
    <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
    <Version>1.5.6</Version>
    <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
  </PropertyGroup>

</Project>

The error displayed is: NETSDK1045: The current .NET SDK does not support targeting .NET Standard 4.0. Either target .NET Standard 2.1 or lower, or use a version of the .NET SDK that supports .NET Standard 4.0

I have .NET Core 3.1.113 installed on build server and .NET 5 installed on local machine.

Note: If I remove netstandard1.0 from TargetFrameworks then build succeeds on both the machines. However I want my library to target .net standard 1.0 as well.

I cannot understand why v4.0 is treated as .NET Standard 4.0 on build server. Can anyone let me know what the problem might be?

2

There are 2 answers

1
Martin Ullrich On BEST ANSWER

Remove the following line from your .csproj file:

<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>

This overrides the version value that the SDK infers from TargetFramework which will interfere with your definition of net3.5 and so on - you were actually building .NET Framework 4.0 twice and then override netstandard1.0 to .NET Standard (inferred TargetFrameworkIdentifier) to 4.0. Newer versions of the SDK may have different inference logic which may be a difference between 3.1 and 5.0 SDKs but this is an error in the csproj nonetheless.

If your build logic relies on TargetFrameworkVersion being defined in the project I suggest you try to change your build logic - this is VERY DANGEROUS to have in a modern csproj file where you rely on TargetFramework and TargetFrameworks (plural). You can also try to move it into a separate <PropertyGroup> with an always-fals-condition (e.g. <PropertyGroup Condition="'$(ThisIsToWorkAroundBuildScripts)' == 'True'">)

0
Sara Liu - MSFT On

I cannot understand why v4.0 is treated as .NET Standard 4.0 on build server. Can anyone let me know what the problem might be?

That is quite strange and I have not seen that issue before. And there is no info about .NET Standard 4.0 and I did not know why VS treats v4.0(net framework 4.0) as net standard 4.0. Maybe your there is some problems about your server environment.

Please follow these:

1) First, please use dotnet --list-sdks under CMD to check if you installed net core 2.1 sdk or any other versions. And actually, net core 2.1 sdk could support the previous sdks including net standard 1.0. Maybe you would better install net core 2.1 sdk. Also, you could install the latest Net Core 3.1 Sdk.

2) check system environment variable, and find whether there is a variable called MSBuildSDKsPath, if so, please delete it. Then, restart.

3) delete bin and obj folder under the build server. Also, I suggest you would better use Build Tool for VS to build your projects on the server rather than dotnet build. dotnet cli does not contain the tool for net framework. What's more, your project is multi-targetframeworks, it could build net core and net standard, and also you have to download net framework sdk.

That is too complex, you could install the Build Tool for VS2019(more likely a lightweight cmd which integrates dotnet, net framework, all vs environment). Also, install the Net desktop build tool and net core build tool workload, also install the related sdks.

enter image description here

When you finish it, delete bin and obj folder, type this under Build Tool for VS command:

msbuild xxx\xxx.csproj -t:clean,restore,build