dotnet / dotnet/msbuild

AssemblyBindingRedirects not being created

Open
#12,547 1 comment 2 reactions 0 assignees View on GitHub
Area: Task: Resolve Assembly References (RAR) Priority:2 triaged
Dominant language
C#
Stars
5.5k
Forks
1.5k
Avg merge
1d 8h
Merged PRs (30d)
141

Description

### Issue Description

I've come across a situation where AssemblyBindingRedirects are not being automatically generated which leads to subsequent runtime errors when trying to load referenced assemblies.

Simple demo C# project:
```xml


Exe
net472





```

If creating the project in sequence in VisualStudio (and adding the `Microsoft.Extensions.Caching.Memory` NuGet reference last, Visual Studio will correctly _show_ upgrades for several transitive packages:

Image

Presumably all those updated packages would now also each need a binding redirect 🤷

Alas, the compiled project will *only* have binding redirects for a very limited subset of that list of packages in the respective exe's `.config`:

```xml





...






...






...

```

Subsequently exceptions happen when trying to load the unredirected assemblies.
```
System.TypeInitializationException : The type initializer for 'Microsoft.Data.SqlClient.SqlQueryMetadataCache' threw an exception.
---- System.IO.FileLoadException : Could not load file or assembly 'Microsoft.Extensions.Caching.Memory, Version=8.0.0.1, Culture=neutral, PublicKeyToken=adb9793829ddae60' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
```
(This exception is from a UnitTest that tries to instantiate a DbContext from an EF6 context in a similarly structured project, but the issue is obviously the lack of assembly binding redirect).

---
Is there a way to **force** the binding redirect(s) apart from manually adding them to the `app.config` ?

### Steps to Reproduce

C# project:
```xml


Exe
net472





```
Build the project

### Expected Behavior

`[...].config` with appropriate binding redirects for all transitive package assemblies that have been upgraded via other packages.

### Actual Behavior

`[...].config` with missing binding redirects. and subsequent load errors.

### Analysis

It's not generating the binding redirects.

### Versions & Configurations

```
MSBuild version 17.14.19+164abd434 for .NET Framework
17.14.19.38001
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.