问题
Is it possible to msbuild a project that was created for .NET 4.x against the .NET 5 SDK without upgrading the project in VS?
I have a .NET 4.7 library project that I compile for .NET 4.x using msbuild (and this issue is easily reproducible by creating a "Class Library (.NET Framework)" project for .NET 4.7).
I want to build it for .NET 5, so I tried
dotnet msbuild /property:TargetFrameworkVersion=net5.0 /property:TargetFrameworkIdentifier=.NETCoreApp
Microsoft (R) Build Engine version 16.8.0+126527ff1 for .NET
Copyright (C) Microsoft Corporation. All rights reserved.
C:\Program Files\dotnet\sdk\5.0.100\Roslyn\Microsoft.CSharp.Core.targets(8,31): error MSB4086: A numeric comparison was attempted on "$(_TargetFrameworkVersionWithoutV)" that evaluates to "" instead of a number, in condition "('$(TargetFrameworkIdentifier)' != '.NETCoreApp' OR '$(_TargetFrameworkVersionWithoutV)' < '3.0') AND [H:\ClassLibrary1\ClassLibrary1.csproj]
C:\Program Files\dotnet\sdk\5.0.100\Roslyn\Microsoft.CSharp.Core.targets(8,31): error MSB4086: ('$(TargetFrameworkIdentifier)' != '.NETStandard' OR '$(_TargetFrameworkVersionWithoutV)' < '2.1')". [H:\ClassLibrary1\ClassLibrary1.csproj]
That seems to want $(_TargetFrameworkVersionWithoutV)
so I tried
dotnet msbuild /property:TargetFrameworkVersion=net5.0 /property:TargetFrameworkIdentifier=.NETCoreApp /property:_TargetFrameworkVersionWithoutV=5.0
Microsoft (R) Build Engine version 16.8.0+126527ff1 for .NET
Copyright (C) Microsoft Corporation. All rights reserved.
C:\Program Files\dotnet\sdk\5.0.100\Microsoft.Common.CurrentVersion.targets(84,5): error MSB4184: The expression "[Microsoft.Build.Utilities.ToolLocationHelper]::GetPathToStandardLibraries(.NETCoreApp, net5.0, '', '', '', '')" cannot be evaluated. Input string was not in a correct format. [H:\ClassLibrary1\ClassLibrary1.csproj]
PS: I did also try comparing a pure .NET 5 library project against a .NET 4 one and based on that tried this
dotnet msbuild /property:TargetFramework=net5.0 /property:TargetFrameworkIdentifier=.NETCoreApp /property:_TargetFrameworkVersionWithoutV=5.0 /property:TargetFrameworkVersion=5.0
Microsoft (R) Build Engine version 16.8.0+126527ff1 for .NET
Copyright (C) Microsoft Corporation. All rights reserved.
C:\Program Files\dotnet\sdk\5.0.100\Microsoft.Common.CurrentVersion.targets(1180,5): error MSB3644: The reference assemblies for .NETCoreApp,Version=v5.0 were not found. To resolve this, install the Developer Pack (SDK/Targeting Pack) for this framework version or retarget your application. You can download .NET Framework Developer Packs at https://aka.ms/msbuild/developerpacks [H:\ClassLibrary1\ClassLibrary1.csproj]
回答1:
NET 5
is the new release version of Net Core rather than Net Framework.
See this document:
And NET 5
is for new-sdk style project while net framework 4.7 project in your side is non-sdk style project.
So you can not use a new-sdk style project's property for a non-sdk style project. See this document.
Also, new-sdk style projects use TargetFramework
while non-sdk style project uses TargetFrameworkVersion
. And .NET 5
should be used with TargetFramework
but your non-sdk project does not identify it.
Solution
So you should change your net framework project from non-sdk
into new-sdk
.
You could create a new net core project and then change its xxx.csproj file into:
<TargetFramework>net47</TargetFramework>
And then migrate your old project's content into the new one which means that this project is a new-sdk
style project with net framework.
See Overview of porting from .NET Framework to .NET Core and also this document.
Then, you can use
dotnet msbuild /property:TargetFramework=net5.0
来源:https://stackoverflow.com/questions/64995639/how-to-use-msbuild-to-build-an-existing-net-4-project-for-net-5