dotnet publish sln having projects with multiple target frameworks fails

8.1k views Asked by At

I have a solution with many projects. Some target frameworknetcoreapp2.1, some other target framework netstandard2.0 and one project has a double target framework

<TargetFrameworks>netstandard2.0;net471</TargetFrameworks>

I'd want to have a artifact for win10 with a single command:

dotnet publish MySolution.sln -c Release -o "targetFolder" -r win10-x64

With this command I have this error while building the project with double target framework. Here's the errors:

C:\Program Files\dotnet\sdk\2.1.402\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.CrossTargeting.targets(31,5) error : The 'Publish' target is not supported without specifying a target framework. The current project targets multiple frameworks, please specify the framework for the published application.

The error is clear. At the end I find that dll compiled in the output directory and it seems like it is a netstandard2.0 dll because my application still works.

I don't like dirty things so, how can I solve my problem?

I would avoid to call N times the "dotnet publish" command if possible.

1

There are 1 answers

4
Martin Ullrich On BEST ANSWER

Don't use dotnet publish with the same output directory on a solution. Especially not with the "-r" argument.

It is dangerous because:

  • libraries don't have the right trimming behaviour for netstandard facade packages
  • libraries may have odd behaviour when publishing with "-r", especially for netstandard<2.0 dependencies. (they'd end up copying the .NET Core 1.0/1.1 implementation(!) assemblies)
  • you may end up with different NuGet dependencies in the output (transitive dependencies)
  • Copy-to-output/publish-directory items may end up overwriting each other, it may even lead to build failures

Call it individually for all application (console app, web app) projects or create an MSBuild file that publishes these applications.