question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

MSBuild missing automatic binding redirects when using NuGet transitive pinning

See original GitHub issue

Issue Description

MSBuild doesn’t produce binding redirects automatically for indirect nuget dependencies, leading to runtime failure of my code.

Steps to Reproduce

git clone https://github.com/microsoft/vs-servicehub.git
cd vs-servicehub
git checkout 7659b150cd580d35f3751b33341bcb50f63c32dd
Microsoft.ServiceBroker.sln

Within VS, run the Microsoft.ServiceHub.Analyzers.Tests (net472) tests.

Expected Behavior

I expect them to pass (as they do at the command line).

Actual Behavior

Most of the tests fail with this error:

System.TypeInitializationException : The type initializer for ‘Microsoft.CodeAnalysis.Testing.ReferenceAssemblies’ threw an exception. ---- System.IO.FileLoadException : Could not load file or assembly ‘NuGet.Packaging, Version=5.6.0.5, Culture=neutral, PublicKeyToken=31bf3856ad364e35’ or one of its dependencies. The located assembly’s manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

Analysis

The Directory.Packages.props file forces the NuGet.Protocol transitive dependency to 6.4.0, where it would naturally be in the 5.x range. Because this is not a direct dependency, and the indirect dependency holds PrivateAssets="compile" on it such that it isn’t passed to my compiler (or RAR?), msbuild doesn’t produce the necessary binding redirect.

When I add my own top-level dependency on NuGet.Protocol, such that it goes to RAR, the binding redirect is created and the tests pass.

I believe MSBuild should be enhanced to be aware of transitive dependencies so that it can produce the necessary binding redirects.

Versions & Configurations

.NET SDK 7.0.101 Dev17.5

Issue Analytics

  • State:closed
  • Created 8 months ago
  • Comments:14 (13 by maintainers)

github_iconTop GitHub Comments

1reaction
sharwellcommented, Feb 21, 2023

@AR-May It’s difficult to understand if the linked item will truly resolve this. In order for binding redirects to be generated, the change would need to impact the inputs to ResolveAssemblyReference such that its SuggestedRedirects output includes consideration for assemblies that are transitive dependencies of the current project but are not included as references of the current project.

I updated the resolution state to reflect that this was marked as a duplicate of external issue NuGet/NuGet.Client#4953.

1reaction
jeffklcommented, Feb 1, 2023

Just an update, I am actively investigating a fix but am on call this week. I’ll let you know when I’ve made progress on this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

NuGet; Transitive Dependencies; Binding Redirect Hell
Why aren't binding redirects being generated for transitive dependencies? · Since they're not, how do I come up with a full list to...
Read more >
Transitive NuGet dependencies: .NET Core's got your back
MSBuild seems to have issues with transitive NuGet dependencies, ... NET Core to generate the binding redirects for you automatically!
Read more >
Enable or disable autogenerated binding redirects
On the Application page, uncheck the Auto-generate binding redirects option. If you don't see the option, you'll need to manually disable the ...
Read more >
.Net Framework Nuget Packages - Solving Assembly ...
Binding redirects are used when the assembly is strong-named. A strong-named assembly is uniquely identified by its name and assembly version ...
Read more >
Binding Redirects
The fix if you're using .NET Framework · Enable <AutoGenerateBindingRedirects> (this doesn't work for web projects - it doesn't handle web.config ).
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found