Mike Christensen
Mike Christensen

Reputation: 91608

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

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!

Upvotes: 2

Views: 4141

Answers (1)

Mr Qian
Mr Qian

Reputation: 23760

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.

Upvotes: 5

Related Questions