Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.NET 4.7.1 app experiences unexpected "Could not load file or assembly 'System.Runtime, Version=4.1.2.0" #27600

Closed
sandersaares opened this issue Oct 11, 2018 · 13 comments
Milestone

Comments

@sandersaares
Copy link

  1. Create a new .NET Framework 4.7.1 console app project.
  2. Install BenchmarkDotNet NuGet package with DependencyBehavior=Highest.
  3. Paste the following code into Program.cs and excute the app as Release build.
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;

namespace ConsoleApp3
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            BenchmarkRunner.Run<Foo>();
        }
    }

    [ClrJob]
    public class Foo
    {
        [Benchmark]
        public void Bar()
        {

        }
    }
}

Expected result: app executes and does some no-op benchmarks.

Actual result:

// Validating benchmarks:
// ***** BenchmarkRunner: Start   *****
// ***** Building 1 exe(s) in Parallel: Start   *****
// ***** Done, took 00:00:00 (0.13 sec)   *****
// Found benchmarks:
//   Foo.Bar: Clr(Runtime=Clr)

// Build Exception: Could not load file or assembly 'System.Runtime, Version=4.1.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.

Packages.config contains this:

<package id="System.Runtime" version="4.3.0" targetFramework="net471" />

App.config contains this:

<dependentAssembly>
        <assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.1.2.0" newVersion="4.1.2.0" />
      </dependentAssembly>

Inspecting what is happening with a debugger indicates that the exception occurs in the cctor of the Builder class:

Unhandled Exception: System.TypeInitializationException: The type initializer for 'BenchmarkDotNet.Toolchains.Roslyn.RoslynToolchain' threw an exception. ---> System.TypeInitializationException: The type initializer for 'BenchmarkDotNet.Toolchains.Roslyn.Builder' threw an exception. ---> System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=4.1.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified. ---> System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=4.0.20.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
   --- End of inner exception stack trace ---
   at BenchmarkDotNet.Toolchains.Roslyn.Builder..cctor()
   --- End of inner exception stack trace ---
   at BenchmarkDotNet.Toolchains.Roslyn.Builder..ctor()
   at BenchmarkDotNet.Toolchains.Roslyn.RoslynToolchain..ctor()
   at BenchmarkDotNet.Toolchains.Roslyn.RoslynToolchain..cctor()
   --- End of inner exception stack trace ---
   at BenchmarkDotNet.Toolchains.ToolchainExtensions.GetToolchain(Runtime runtime)
...

binlog.zip

I attach a solution that contains both a repro app (ConsoleApp1) and the BenchmarkDotNet package source, to allow easy inspection of a debug build:
BenchmarkDotNet.zip

Previous discussion with BenchmarkDotNet maintainers at: dotnet/BenchmarkDotNet#896. Their advice was to migrate from Visual Studio to Rider if I want to use their library in this situation but I would perfer to continue using Visual Studio if possible.

@karelz
Copy link
Member

karelz commented Oct 11, 2018

@joperezr does it ring a bell?
cc @adamsitnik

@adamsitnik
Copy link
Member

Their advice was to migrate from Visual Studio to Rider if I want to use their library in this situation but I would perfer to continue using Visual Studio if possible.

My advice was "remove the assembly binding redirects from the project"

I repeated this two times:
dotnet/BenchmarkDotNet#896 (comment)
dotnet/BenchmarkDotNet#896 (comment)

I have reported this bug a long time ago but it got ignored:
https://github.com/dotnet/corefx/issues/30642

@karelz
Copy link
Member

karelz commented Oct 11, 2018

If it is duplicate, let's resolve one of them as dupe of the other.
@adamsitnik please work with @joperezr to find out what to do as next step. I wonder if the original issue was misrouted into wrong area path ...

@AndreyAkinshin
Copy link
Member

Their advice was to migrate from Visual Studio to Rider if I want to use their library in this situation but I would perfer to continue using Visual Studio if possible.

I just wanted to highlight that it's not a problem with the .NET Framework, it's a problem with Visual Studio. Rider is an example of IDE which doesn't have this problem, so it's definitely an IDE issue.
@sandersaares you can use any IDE that you like. You can easily use BenchmarkDotNet with Visual Studio. The only known issue relates to the first-time library installation in the classic .NET project with specific NuGet settings (DependencyBehavior=Highest). The issue can be resolved in a few seconds because the workarounds are known.

@joperezr
Copy link
Member

The problem here I believe is with the VS tooling when you add the nuget package reference. It shouldn't be adding that extra binding redirect. If you instead migrate your project to use PackageReference then installing a new package won't generate binding redirects, and only at build time will we figure out if you need extra binding redirects or not, which in this case you shouldn't need any for System.Runtime as it already comes inbox for 4.7.1.

@sandersaares can you try the exact same repro but instead of adding Benchmarkdotnet package via VS, simple add this to your project:

<ItemGroup>
  <PackageReference Include="BenchmarkDotNet" Version="0.11.1" />
</ItemGroup>

@sandersaares
Copy link
Author

@joperezr I confirm that using the steps you describe results in a successful execution of the app.

@karelz
Copy link
Member

karelz commented Oct 16, 2018

Closing as resolved. Thanks for confirmation @sandersaares!

@karelz karelz closed this as completed Oct 16, 2018
@sandersaares
Copy link
Author

sandersaares commented Oct 17, 2018

Hang on a minute. That is a workaround, not a resolution to the defect in question.

I would consider this resolved if the scenario in the original post works without errors. You can't expect Visual Studio users to go digging in files that VS generates them and changing settings based on some obscure GitHub issue. That is not any sensible level of user experience. "Make framework-provided assemblies actually work" is something that I expect out of the box with default settings on all fronts.

@joperezr
Copy link
Member

I agree @sandersaares, that said, I don’t believe this is the right repo to log an issue with the vs nuget tooling, as the fix for this would most certainly not go in here. The issues on this repo are regarding bugs/problems with the framework itself. I would probably move this issue to either dotnet/sdk or nuget which are probably still not the right places but they might know how to re-route it better.

@sandersaares
Copy link
Author

sandersaares commented Oct 17, 2018

Yeah, I did spend a few minutes considering where to file this. It is not obvious to me as a user what exactly is the cause of the issue, all I see is that my .NET app development is blocked.

Here is an attempt at a differently targeted repost: https://developercommunity.visualstudio.com/content/problem/360253/net-471-app-experiences-unexpected-could-not-load.html

Hrm, looks like that site doesn't do code blocks...

@joperezr
Copy link
Member

Yeah, I totally agree that as a user when you don’t know what is going on this is a good place to post it so that we can have an initial diagnosis. Now that we know the cause of this is the nuget package manager adding wrong redirects its a good time to move it where the fix would go 😄

I think that choosing vs feedback was the right choice :)

@GSPP
Copy link

GSPP commented Oct 19, 2018

Yeah, I did spend a few minutes considering where to file this. It is not obvious to me as a user what exactly is the cause of the issue, all I see is that my .NET app development is blocked.

Microsoft has now been made aware of multiple severe bugs of this kind. Please reroute these issues internally to the right places. It is not for customers to run after Microsoft to find a place that would accept these bug reports.

@msftgits msftgits transferred this issue from dotnet/corefx Jan 31, 2020
@msftgits msftgits added this to the 3.0 milestone Jan 31, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 15, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

7 participants