Benchmarkdotnet: Fails to build when targeting .NET Core 3.0 and .NET Framework

Created on 7 Feb 2019  路  2Comments  路  Source: dotnet/BenchmarkDotNet

Created a new .NET Core 3.0 console application in VS 2019 and added BenchmarkDotNet via nuget. Changed line in proj.csproj:

<TargetFramework>netcoreapp3.0</TargetFramework>
to
<TargetFrameworks>net472;netcoreapp3.0</TargetFrameworks>

I receive the following in console output (after CLR job has been successful completed):

// Build Exception: Standard output:
completed "proj.csproj".
proj \ bin \ Release \ net472 \ 21078fb3-513f-4d7f-807a-57e8bfabfb62 \ BenchmarkDotNet.Autogenerated.csproj: error NU1201: The proj project is not compatible with **netcoreapp2.0** (.NETCoreApp, version = v2.0). The proj project supports the following:
proj \ bin \ Release \ net472 \ 21078fb3-513f-4d7f-807a-57e8bfabfb62 \ BenchmarkDotNet.Autogenerated.csproj: error NU1201: - net472 (.NET Framework, Version = v4.7.2)
proj \ bin \ Release \ net472 \ 21078fb3-513f-4d7f-807a-57e8bfabfb62 \ BenchmarkDotNet.Autogenerated.csproj: error NU1201: - netcoreapp3.0 (.NETCoreApp, Version = v3.0)
   Error recovering to "106.14 ms" for "proj \ bin \ Release \ net472 \ 21078fb3-513f-4d7f-807a-57e8bfabfb62 \ BenchmarkDotNet.Autogenerated.csproj".

For some reason it tries to compile it on .Net Core 2.0 and obviously as the project doesn't support it (target framework is set to .NET Core 3.0), it fails. However when I change the project to .NET Core 2.0 it (not much surprising) "magically" works and complete the core benchmark. Like this:

<TargetFrameworks>net472;netcoreapp2.0</TargetFrameworks>

Now I have another project using BenchmarkDotNet which targets netcoreapp3.0 only and I can run benchmarks on it so I have no idea what's different or the problem.

//edit, fyi:
Works:
<TargetFrameworks>net472;netcoreapp2.0</TargetFrameworks>
<TargetFrameworks>netcoreapp3.0</TargetFrameworks>
Fails:
<TargetFrameworks>net472;netcoreapp3.0</TargetFrameworks>

Toolchains question

Most helpful comment

Hi @Symbai

BDN generates a project with all boilerplate code that is required to avoid common benchmarking pitfalls. This project has TargetFramework property as well.

Depending on what is the target framework of your host process (the thing that you run in the console) we try to determine TargetFramework.

With .NET Framework, it's easy, because we just read the entries from Windows Registry which tells use which .NET SDK you have installed.

But when your host process is .NET Framework (dotnet run -f net472) and you want to run a .NET Core benchmark we can't determine which .NET Core version you want to target and we use .NET Core 2.0 which is our default.

To get it working you need to tell BDN which .NET Core to use:

BenchmarkRunner.Run<Program>(
    DefaultConfig.Instance
        .With(Job.Default.With(CsProjCoreToolchain.NetCoreApp30)));

or using the --runtimes console line arguments:

--runtimes net472 netcoreapp3.0
static void Main(string[] args) => BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args);

All 2 comments

Hi @Symbai

BDN generates a project with all boilerplate code that is required to avoid common benchmarking pitfalls. This project has TargetFramework property as well.

Depending on what is the target framework of your host process (the thing that you run in the console) we try to determine TargetFramework.

With .NET Framework, it's easy, because we just read the entries from Windows Registry which tells use which .NET SDK you have installed.

But when your host process is .NET Framework (dotnet run -f net472) and you want to run a .NET Core benchmark we can't determine which .NET Core version you want to target and we use .NET Core 2.0 which is our default.

To get it working you need to tell BDN which .NET Core to use:

BenchmarkRunner.Run<Program>(
    DefaultConfig.Instance
        .With(Job.Default.With(CsProjCoreToolchain.NetCoreApp30)));

or using the --runtimes console line arguments:

--runtimes net472 netcoreapp3.0
static void Main(string[] args) => BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args);

@Symbai please feel free to reopen if something is not clear

Was this page helpful?
0 / 5 - 0 ratings