1

I've read about this cool Add-BindingRedirect command in the NuGet package manager console in Visual Studio that will update all your binding redirects. This sounds amazing, since our redirects are a giant mess. I ran the following:

Add-BindingRedirect *

It spits out a bunch of stuff:

Name                  : Newtonsoft.Json
Culture               : neutral
PublicKeyToken        : 30ad4fe6b2a6aeed
ProcessorArchitecture : 
NewVersion            : 12.0.0.0
OldVersion            : 0.0.0.0-12.0.0.0
AssemblyNewVersion    : 12.0.0.0
CodeBaseHref          : 
CodeBaseVersion       : 
PublisherPolicy       : 

Name                  : Microsoft.ApplicationInsights
Culture               : neutral
PublicKeyToken        : 31bf3856ad364e35
ProcessorArchitecture : 
NewVersion            : 2.13.1.12554
OldVersion            : 0.0.0.0-2.13.1.12554
AssemblyNewVersion    : 2.13.1.12554
CodeBaseHref          : 
CodeBaseVersion       : 
PublisherPolicy       : 

Name                  : System.Memory
Culture               : neutral
PublicKeyToken        : cc7b13ffcd2ddd51
ProcessorArchitecture : 
NewVersion            : 4.0.1.1
OldVersion            : 0.0.0.0-4.0.1.1
AssemblyNewVersion    : 4.0.1.1
CodeBaseHref          : 
CodeBaseVersion       : 
PublisherPolicy       : 

Name                  : System.Numerics.Vectors
Culture               : neutral
PublicKeyToken        : b03f5f7f11d50a3a
ProcessorArchitecture : 
NewVersion            : 4.1.4.0
OldVersion            : 0.0.0.0-4.1.4.0
AssemblyNewVersion    : 4.1.4.0
CodeBaseHref          : 
CodeBaseVersion       : 
PublisherPolicy       : 

Name                  : System.Runtime.CompilerServices.Unsafe
Culture               : neutral
PublicKeyToken        : b03f5f7f11d50a3a
ProcessorArchitecture : 
NewVersion            : 4.0.5.0
OldVersion            : 0.0.0.0-4.0.5.0
AssemblyNewVersion    : 4.0.5.0
CodeBaseHref          : 
CodeBaseVersion       : 
PublisherPolicy       : 

Name                  : System.Buffers
Culture               : neutral
PublicKeyToken        : cc7b13ffcd2ddd51
ProcessorArchitecture : 
NewVersion            : 4.0.3.0
OldVersion            : 0.0.0.0-4.0.3.0
AssemblyNewVersion    : 4.0.3.0
CodeBaseHref          : 
CodeBaseVersion       : 
PublisherPolicy       : 

... Tons more

However, it doesn't modify a single app.config or web.config file. I also tried just removing all binding redirects by hand, then running the command. Still doesn't modify anything. I'm running Visual Studio 2019 16.5.3. Any ideas as to what I'm doing wrong, or perhaps this doesn't work the way I was hoping? Thanks!

Mike Christensen
  • 88,082
  • 50
  • 208
  • 326

1 Answers1

4

Add-BindingRedirect in Visual Studio doesn't modify any app.config or web.config files

This is quite an abnormal behavior. And I wonder if there are some errors when you faced this issue.

In my side, I create a new project and run Add-BindingRedirect * on Package Manager Console, it can add the related binding redirects in app.config or web.config files.

Perhaps there are errors on your nuget packages, please try the following steps:

Step

1) close VS Instance, delete .vs hidden folder, bin, obj folder and then restart your project

2) first run nuget restore by Right-click on the solution-->Restore Nuget Packages in case some nuget packages are missing which will break that command line.

3) disable any third party extensions by Extensions-->Manage Extensions in case they affect it.

4) open Package Manager Console--> type these:

update-package -reinstall

Add-BindingRedirect *

Reinstalling nuget packages is to prevent the nuget packages from being referenced incorrectly.

Besides, some binding redirects need to be reinstalled with the new version of nuget to be automatically updated and added to the file.

5) create a new empty project and install several nuget packages to test whether the issue is related to the specific project itself or nuget itself.

In addition, you can refer to this link for more info.

----------------Update 1---------------

Sorry for that the above answer is based on that I did not know you use PackageReference nuget management format.

I think your solution contains lots of projects and some use Packages.config while the other use PackageReference format.

However, Add-BindingRedirect * just applies to the projects with packages.config. See this issue on GitHub. And with PackageReference format, it will not add or update any binding redirects on app.config or web.config automatically.

Besides, Because there are several projects in your solution that use packages.config format, when using this command, this defect on PackageReference will be overwritten.

To prove it, you can create a new single framework project with PackageReference, and I am sure that when you type that command, it will not work.

As a suggestion,

1) you can add this xml node in xxxx.csproj of the project

 <PropertyGroup>
 <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
 <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
</PropertyGroup>

When you build your project, it will generate the new updated binding redirects file called <project_name>.<dll/exe> xml configuration file in the output folder.

Then you can copy its content into the previous config file.(Actually, when you use this node, you do not need to do the copy and it will automatically bind redirects. See this.)

2) Or just change PackageReference to Packages.config in your project which can use that command without any errors. But it can be hard work. And it you want it, first change back to Package.config under Tools-->Options-->NuGet Package Manager-->Package Management and then you can use Martin's solution from this link.

Mr Qian
  • 21,064
  • 1
  • 31
  • 41
  • I'll give this a shot and get back to you soon! – Mike Christensen Apr 09 '20 at 15:59
  • So, the `update-package` command took 7 min to run and broke a lot of stuff in the app. The home page now crashes with `This module requires that the HttpApplication (Global Application Class) implements IContainerProviderAccessor.` - So, this command seems a bit too dangerous to run given the QA hit we'd probably take testing it! I'd agree there's probably something in our solution that breaks this. – Mike Christensen Apr 09 '20 at 21:16
  • Tons of great information, thanks! Yea, we have about 40 projects in this solution. I think everything uses the new CSPROJ format, except two "web projects" (since those are incompatible with the new format). So, I think that's what is going on. No interest in changing back to packages.config. The `AutoGenerateBindingRedirects` property is something I didn't know about, so will definitely be experimenting with that more! Thanks again.. – Mike Christensen Apr 10 '20 at 15:31